Regular language and Regular expression

41
Automata Theory and Logic Regular Language & Regular Expression A TUTORIAL BY ANIMESH CHATURVEDI AT INDIAN INSTITUTE OF TECHNOLOGY INDORE (IIT-I)

Transcript of Regular language and Regular expression

Page 1: Regular language and Regular expression

Automata Theory and LogicRegular Language & Regular Expression

A TUTORIAL

BY

ANIMESH CHATURVEDI

AT

INDIAN INSTITUTE OF TECHNOLOGY INDORE ( I IT - I )

Page 2: Regular language and Regular expression

DFA, NFA, Regular Expression (RegEx) and Regular Language (RegLang)

A DFA represent a Regular Expression language

Page 3: Regular language and Regular expression

Regular Expression Regular Language

(0 + 10∗)

(0∗ 10∗)

(0 + ε)(1 + ε)

(a + b)*

(a + b)* abb

(11)*

(aa)*(bb)*b

(aa + ab + ba +bb)*

Language of given Regular Expression?

https://www.tutorialspoint.com/automata_theory/regular_expressions.htm

Page 4: Regular language and Regular expression

Regular Expression Regular Language

(0 + 10∗) L = { 0, 1, 10, 100, 1000, 10000, … }

(0∗ 10∗) L = {1, 01, 10, 010, 0010, …}

(0 + ε)(1 + ε) L = {ε, 0, 1, 01}

(a + b)*

(a + b)* abb

(11)*

(aa)*(bb)*b

(aa + ab + ba +bb)*

Language of given Regular Expression?

https://www.tutorialspoint.com/automata_theory/regular_expressions.htm

Page 5: Regular language and Regular expression

Regular Expression Regular Language

(0 + 10∗) L = { 0, 1, 10, 100, 1000, 10000, … }

(0∗ 10∗) L = {1, 01, 10, 010, 0010, …}

(0 + ε)(1 + ε) L = {ε, 0, 1, 01}

(a + b)* Set of strings of a’s and b’s of any length including the null string. So L = { ε, a, b,aa , ab , bb , ba, aaa…….}

(a + b)* abb Set of strings of a’s and b’s ending with the string abb. So L = {abb, aabb, babb,aaabb, ababb, …………..}

(11)*

(aa)*(bb)*b

(aa + ab + ba +bb)*

Language of given Regular Expression?

https://www.tutorialspoint.com/automata_theory/regular_expressions.htm

Page 6: Regular language and Regular expression

Regular Expression Regular Language

(0 + 10∗) L = { 0, 1, 10, 100, 1000, 10000, … }

(0∗ 10∗) L = {1, 01, 10, 010, 0010, …}

(0 + ε)(1 + ε) L = {ε, 0, 1, 01}

(a + b)* Set of strings of a’s and b’s of any length including the null string. So L = { ε, a, b,aa , ab , bb , ba, aaa…….}

(a + b)* abb Set of strings of a’s and b’s ending with the string abb. So L = {abb, aabb, babb,aaabb, ababb, …………..}

(11)* Set consisting of even number of 1’s including empty string, So L= {ε, 11, 1111,111111, ……….}

(aa)*(bb)*b Set of strings consisting of even number of a’s followed by odd number of b’s , so L = {b, aab, aabbb, aabbbbb, aaaab, aaaabbb, …………..}

(aa + ab + ba +bb)* String of a’s and b’s of even length can be obtained by concatenating anycombination of the strings aa, ab, ba and bb including null, so L = {aa, ab, ba, bb, aaab, aaba, ………..}

Language of given Regular Expression?

https://www.tutorialspoint.com/automata_theory/regular_expressions.htm

Page 7: Regular language and Regular expression

Number of states for a given languageDefinition of a language L with alphabet {a} is given as followingL = {ank | k > 0, and n is a positive integer constant}What is the minimum number of states needed in a DFA to recognize L?

Computer Science GATE 2011

Page 8: Regular language and Regular expression

Number of states for a given languageDefinition of a language L with alphabet {a} is given as followingL = {ank | k > 0, and n is a positive integer constant}What is the minimum number of states needed in a DFA to recognize L?

n+1 states are needed in a DFA to recognize L

Let n = 3 and k=1

3+1 = 4 states

Computer Science GATE 2011

Page 9: Regular language and Regular expression

Minimal DFA for a given languageDraw a minimal DFA which accepts the same language as a DFA with alphabet ∑ = {0, 1} is given below {0, 1} ∪ {x ∈ {0, 1}∗ | len(x) ≥ 3}

Konrad Slind Notes on Computation Theory, September 21, 2010

Page 10: Regular language and Regular expression

Minimal DFA for a given languageDraw a minimal DFA which accepts the same language as a DFA with alphabet ∑ = {0, 1} is given below {0, 1} ∪ {x ∈ {0, 1}∗ | len(x) ≥ 3}

Konrad Slind Notes on Computation Theory, September 21, 2010

Page 11: Regular language and Regular expression

Minimal DFA for a given languageDraw a minimal DFA which accepts the same language as a DFA with alphabet ∑ = {0, 1} is given below {0, 1} ∪ {x ∈ {0, 1}∗ | len(x) ≥ 3}

Konrad Slind Notes on Computation Theory, September 21, 2010

Page 12: Regular language and Regular expression

Minimal DFA for a given languageDraw a minimal DFA which accepts the same language as a DFA with alphabet ∑ = {0, 1} is given below {0, 1} ∪ {x ∈ {0, 1}∗ | len(x) ≥ 3}

Konrad Slind Notes on Computation Theory, September 21, 2010

Page 13: Regular language and Regular expression

Minimal DFA for a given languageDraw a minimal DFA which accepts the same language as a DFA with alphabet ∑ = {0, 1} is given below {0n | ∃k. n = 3k + 1}

Konrad Slind Notes on Computation Theory, September 21, 2010

Page 14: Regular language and Regular expression

Minimal DFA for a given languageDraw a minimal DFA which accepts the same language as a DFA with alphabet ∑ = {0, 1} is given below {0n | ∃k. n = 3k + 1}

Konrad Slind Notes on Computation Theory, September 21, 2010

Page 15: Regular language and Regular expression

Minimal DFA for a given language Draw a minimal DFA which accepts the same language as a DFA with alphabet ∑ = {0, 1} is given below {0n | ∃k. n = 3k + 1}

Konrad Slind Notes on Computation Theory, September 21, 2010

Page 16: Regular language and Regular expression

Convert NFA to DFA for a given RegExConstruct DFA to accept 00(0+1)*

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 17: Regular language and Regular expression

Convert NFA to DFA for a given RegExConstruct DFA to accept 00(0+1)*

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 18: Regular language and Regular expression

Convert NFA to DFA for a given RegExConstruct DFA to accept (0+1)*11

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 19: Regular language and Regular expression

Convert NFA to DFA for a given RegExConstruct DFA to accept (0+1)*11

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 20: Regular language and Regular expression

Convert NFA to DFA for a given RegExConstruct DFA to accept 00(0+1)*11

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 21: Regular language and Regular expression

Convert NFA to DFA for a given RegExConstruct DFA to accept 00(0+1)*11

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 22: Regular language and Regular expression

Convert NFA to DFA for a given RegLangConstruct DFA to accept L(M)=ε

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 23: Regular language and Regular expression

Convert NFA to DFA for a given RegLangConstruct DFA to accept L(M)=ε

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 24: Regular language and Regular expression

Convert NFA to DFA for a given RegLangConstruct DFA to accept L(M)=Ǿ

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 25: Regular language and Regular expression

Convert NFA to DFA for a given RegLangConstruct DFA to accept L(M)=Ǿ

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 26: Regular language and Regular expression

Convert NFA to DFA for a given RegLangConstruct DFA to accept L(M)=(0+1)*

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 27: Regular language and Regular expression

Convert NFA to DFA for a given RegLangConstruct DFA to accept L(M)=(0+1)*

Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Page 28: Regular language and Regular expression

Regular ExpressionWhat is the language accepted by the NFA for one literal {a} show below?Assume ε is the empty string

Computer Science GATE 2012

Page 29: Regular language and Regular expression

Regular ExpressionWhat is the language accepted by the NFA for one literal {a} show below?Assume ε is the empty string

Language accepted by NFA is a+, so complement of this language is {є}

Computer Science GATE 2012

Page 30: Regular language and Regular expression

What is the DFA & Regular expression?

30Lexical Analysis by Prof. O. Nierstrasz

Example: the set of strings containing an even number of zeros and an even number of ones

Page 31: Regular language and Regular expression

What is the DFA & Regular expression?

31Lexical Analysis by Prof. O. Nierstrasz

Example: the set of strings containing an even number of zeros and an even number of ones

Page 32: Regular language and Regular expression

What is the DFA & Regular expression?

32Lexical Analysis by Prof. O. Nierstrasz

Example: the set of strings containing an even number of zeros and an even number of ones

The RE is (0011)*((0110)(0011)*(0110)(0011)*)*

Page 33: Regular language and Regular expression

Draw NFA for given regular expressions

33Lexical Analysis by Prof. O. Nierstrasz

For the RE (a|b)*abb ?

Page 34: Regular language and Regular expression

Draw NFA for given regular expressions

34Lexical Analysis by Prof. O. Nierstrasz

For the RE (a|b)*abb ?

Page 35: Regular language and Regular expression

Draw NFA for given regular expressions

35

For the RE (a|b)*abb ?

State s0 has multiple transitions on a! It is a non-deterministic finite automaton

Lexical Analysis by Prof. O. Nierstrasz

Page 36: Regular language and Regular expression

36

A language is regular if it is recognized by a deterministic finite automatonWhether L = { w | w contains 001} is regular or not?

Check it by building an automaton accepts all and only those strings that contain 001

Steven Rudich: www.cs.cmu.edu/~rudich rudich0123456789

Page 37: Regular language and Regular expression

37

q q00

1 0

1

q0 q001

0 0 1

0,1

A language is regular if it is recognized by a deterministic finite automatonWhether L = { w | w contains 001} is regular or not?

Check it by building an automaton accepts all and only those strings that contain 001

Steven Rudich: www.cs.cmu.edu/~rudich rudich0123456789

Page 38: Regular language and Regular expression

A language is regular if it is recognized by a deterministic finite automatonWhether L = { w | w has an even number of 1s} is regular or not?

Check it by building an automaton accepts all and only those strings that has an even number of 1s

Steven Rudich: www.cs.cmu.edu/~rudich rudich0123456789

Page 39: Regular language and Regular expression

A language is regular if it is recognized by a deterministic finite automaton

q0q1

0 0

1

1

Whether L = { w | w has an even number of 1s} is regular or not?

Check it by building an automaton accepts all and only those strings that has an even number of 1s

Steven Rudich: www.cs.cmu.edu/~rudich rudich0123456789

Page 40: Regular language and Regular expression

Referenceshttps://www.tutorialspoint.com/automata_theory/regular_expressions.htm

Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University http://www.cs.tau.ac.il/~orilahav/CompModelFall10/Compute2-print.pdf

Slide by: Dr. Harry H. Porter http://web.cecs.pdx.edu/~harry/compilers/slides/LexicalPart3.pdf

Slide by Dr. Ding-Zhu Du http://www.utdallas.edu/~dzdu/cs4384/lect7.ppt

Prof. Shunichi Toida http://www.cs.odu.edu/~toida/nerzic/390teched/regular/fa/nfa-2-dfa.html

Konrad Slind Notes on Computation Theory, September 21, 2010

Lexical Analysis by Prof. O. Nierstrasz

GATE (Graduate Aptitude Test of Engineering) jointly conducted by IIT’s

Page 41: Regular language and Regular expression