0% found this document useful (0 votes)
4 views

Unit 1.2

The document covers Nondeterministic Finite Automata (NFA) and their formal definitions, including examples and methods for constructing NFAs from regular expressions. It discusses the closure properties of regular languages under concatenation and star operations, as well as the conversion of NFAs to Deterministic Finite Automata (DFA). Additionally, it addresses the minimization of finite automata, including algorithms for state elimination and the identification of unreachable states.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 1.2

The document covers Nondeterministic Finite Automata (NFA) and their formal definitions, including examples and methods for constructing NFAs from regular expressions. It discusses the closure properties of regular languages under concatenation and star operations, as well as the conversion of NFAs to Deterministic Finite Automata (DFA). Additionally, it addresses the minimization of finite automata, including algorithms for state elimination and the identification of unreachable states.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 93

Automata and Compiler Design (CSE 304 )

Unit 1: Part 2

Dr. Murali Krishna Enduri


Department of CSE
1
Nondeterministic Finite Automata:

2
Nondeterministic Finite Automata:
o Generalize FAs by adding nondeterminism, allowing several alternative computations
on the same input string.
o Ordinary deterministic FAs follow one path on each input.

3
Nondeterministic Finite Automata:

By continuing to experiment in this way, you will see that N1 accepts all
strings that contain either 101 or 11 as a substring.

4
Nondeterministic Finite Automata:

5
Formal Definition Nondeterministic Finite Automata:

6
Nondeterministic Finite Automata:

7
Nondeterministic Finite Automata:
NFA Example

8
Nondeterministic Finite Automata:
NFA Example

9
Nondeterministic Finite Automata:
Draw NFA which accept 00 or 11 at the end of a string where symbols are 0 and 1.

10
Nondeterministic Finite Automata:
Draw a NFA which accept a string containing “for” anywhere in a string of {a-z}

Construct NFA a language L(M) = { (10)n : n > 0 }

11
Concatenation of two Regular Languages:
Theorem 3: The class of regular languages are closed under concatenation.
In other words, If L1 and L2 are regular languages then L1o L2 is also a regular language

12
Star operation of Regular Language:
Theorem 3: The class of regular languages are closed under star operation.
In other words, If L is a regular language then L* is also a regular language

13
DFA VS
NFA

14
Converting NFA to
DFA

15
Converting NFA to
DFA

16
Converting NFA to
DFA

17
Converting NFA to
DFA

18
Converting NFA to
DFA

19
Converting NFA to
DFA

20
Converting NFA to
DFA

21
Converting NFA to
DFA

22
Converting NFA to
DFA

23
24
25
Converting NFA to
DFA

26
REGULAR EXPRESSIONS

27
REGULAR EXPRESSIONS
• Regular expressions
• describe regular languages

• Example:

• describes the language

28
REGULAR EXPRESSIONS
Recursive Definition

Given regular expressions and

Are regular expressions

29
REGULAR EXPRESSIONS

A regular expression:

Not a regular expression:

30
REGULAR EXPRESSIONS

: language of regular expression

Example

31
REGULAR EXPRESSIONS

• For regular expressions and

32
REGULAR EXPRESSIONS
• Regular expression:

33
REGULAR EXPRESSIONS

• Regular expression

• Regular expression

= { all strings containing substring 00 }

34
35
REGULAR EXPRESSIONS

36
REGULAR EXPRESSIONS

37
REGULAR EXPRESSIONS to NFA

38
REGULAR EXPRESSIONS to NFA Thompson’s construction

39
REGULAR EXPRESSIONS to NFA

40
41
42
Constructing RE for given DFA using Arden's theorem
In order to find out a regular expression of a Finite Automaton, we use Arden’s Theorem
along with the properties of regular expressions.
Statement −
Let P and Q be two regular expressions.
If P does not contain null string, then R = Q + RP has a unique solution that is R = QP*
Proof −
R = Q + (Q + RP)P [After putting the value R = Q + RP]
= Q + QP + RPP
When we put the value of R recursively again and again, we get the following equation −
R = Q + QP + QP2 + QP3…..
R = Q (ε + P + P2 + P3 + …. )
R = QP* [As P* represents (ε + P + P2 + P3 + ….) ]

43
The equations for the three states q1, q2, and q3 are as follows −
q1 = q1a + q3a + ε (ε move is because q1 is the initial state0
q2 = q 1 b + q 2 b + q 3 b
q3 = q 2 a

Now, we will solve these three equations −


q2 = q 1b + q 2b + q 3b
= q1b + q2b + (q2a)b (Substituting value of q3)
= q1b + q2(b + ab)
Arden's theorem = q1b (b + ab)* (Applying Arden’s Theorem)
If P does not contain null string, q1 = q 1a + q 3a + ε
then R = Q + RP has a unique
solution that is R = QP* = q1a + q2aa + ε (Substituting value of q3)
= q1a + q1b(b + ab*)aa + ε (Substituting value of q2)
= q1(a + b(b + ab)*aa) + ε
= ε (a+ b(b + ab)*aa)*
= (a + b(b + ab)*aa)*
Hence, the regular expression is (a + b(b + ab)*aa)*.
44
Here the initial state is q1 and the final state is q2
Now we write down the equations −
q1 = q 10 + ε
q2 = q 11 + q 20
q3 = q 21 + q 30 + q 31
Now, we will solve these three equations −
q1 = ε0* [As, εR = R]
So, q1 = 0*
Arden's theorem q2 = 0*1 + q20
If P does not contain null So, q2 = 0*1(0)* [By Arden’s theorem]
string, then R = Q + RP has a Hence, the regular expression is 0*10*.
unique solution that is R = QP*

45
State Elimination Method

Step-01:

Step-02:

46
State Elimination Method
Problem-01:

47
State Elimination Method
Problem-02:

48
State Elimination Method
Problem-03:

49
State Elimination Method

Problem-04:

Problem-05:

50
https://www.youtube.com/watch?v=mpyCeSvGh-M
https://www.guru99.com/linux-regular-expressions.html

51
Minimization of Finite Automata
1. State minimization problem

2. There can be more than one DFA that accepts the same language
Among these equivalent DFAs, it is often useful to find the smallest,
that is, the DFA with the minimum possible number of states

3. This is especially important when DFAs are used for designing


computer hardware circuits

52
Minimization of Finite Automata

Equivalent finite automata

53
Minimization of Finite Automata
Unreachable states
1. A DFA sometimes contains states that cannot possibly be reached from the initial
state
2. These can easily be identified and they can be removed without changing the
language accepted by the DFA

54
Minimization of Finite Automata
Equate & collapse states having same behavior.
Build equivalence relation on states:
p≡q ↔ (∀z∈Σ*, (p,z)∈F ↔ (q,z)∈F)

I.e., iff for every string z, one of the following is true:

z z
p p
or
z z
q q

55
56
DFA Minimization: Algorithm

57
DFA Minimization: Example

58
DFA Minimization: Example

59
Watch Video: 60
61
DFA Minimization: Example

62
DFA Minimization: Algorithm
1. Delete inaccessible ( or unreachable) states.

2. Initialize all entries as unmarked & with no dependences.

3. Mark all pairs of a final & nonfinal state.

4. For each unmarked pair p,q & input symbol a:


1. Let r=δ(p,a), s=δ(q,a).
2. If (r,s) unmarked, add (p,q) to (r,s)’s dependences,
3. Otherwise mark (p,q), and recursively mark all dependences of newly-marked entries.

5. Combine unmarked pairs of states.

63
DFA Minimization: Example
0 1

0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0

0
d

e
2. Initialize table entries:
Unmarked, empty list f

a b c d e f g

64
DFA Minimization: Example
0 1

0 1 0
a b c d
1 0 1 b
0 1
1 1 0
e f g h c
1 0

0 d

e
3. Mark pairs of final &
nonfinal states f

h
a b c d e f g

65
DFA Minimization: Example
0 1

0 1 0
a b c d δ(b,0) ≡? δ(a,0)
0 1 b
1 δ(b,1) ≡? δ(a,1)
0 1
1 1 0
e f g h c
1 0

0
d

e
4. For each unmarked
pair & symbol, … f

a b c d e f g
Build equivalence relation on states:
p≡q ↔ (∀z∈Σ*, (p,z)∈F ↔ (q,z)∈F) 66
DFA Minimization: Example
0 1

0 1 0
a b c d g ≡? b Maybe.
0 1 b
1 c ≡? f No!
0 1
1 1 0
e f g h c
1 0

0
d

e
3. For each unmarked
pair & symbol, … f

h
a b c d e f g
Build equivalence relation on states:
p≡q ↔ (∀z∈Σ*, (p,z)∈F ↔ (q,z)∈F) 67
DFA Minimization: Example
0 1

0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0

0
d

e
4. For each unmarked
pair & symbol, … f

a b c d e f g

68
DFA Minimization: Example
0 1

0 1 0
a b c d δ(e,0) ≡? δ(a,0)
0 1 b
1 δ(e,1) ≡? δ(a,1)
0 1
1 1 0
e f g h c
1 0

0
d

e
4. For each unmarked
f
pair & symbol, …
g

a b c d e f g

69
DFA Minimization: Example
0 1

0 1 0
a b c d h ≡? b Maybe.
0 1 b
1 f ≡? f Yes.
0 1
1 1 0
e f g h c
1 0

0
d

e
4. For each unmarked
f
pair & symbol, …
g

h
(a,e)
a b c d e f g

70
DFA Minimization: Example
0 1

0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0

0
d

e
4. For each unmarked
f
pair & symbol, … (g,a)

g
(g,a)

h
(a,e)
a b c d e f g

71
DFA Minimization: Example
0 1

0 1 0
a b c d b Need to mark.
1 0 1
0 1 So, mark (g,a) also.
1 1 0
e f g h c
1 0

0
d

4. For each unmarked f


(g,a)
pair & symbol, …
g
(g,a)

h
(a,e)
a b c d e f g

72
DFA Minimization: Example
0 1

0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0

0
d

e
4. For each unmarked
pair & symbol, … f
(g,a)

h
(a,e)
a b c d e f g

73
DFA Minimization: Example
0 1

0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0

0
d

e
5. Combine unmarked
pairs of states. f
0 1

a 0 b 1 g
a≡e c
e 1 h 0
b≡h 0
d≡f d 1 h
g (a,e)
1 f
0 a b c d e f g

74
DFA Minimization: Example

75
DFA Minimization: Correctness

Why is new DFA no larger than old DFA?


Only removes states, never introduces new states.
Obvious.

Why is new DFA equivalent to old DFA?


Only identify states that provably have same behavior.
Could prove x∈L(M) ↔ x∈L(M’) by inductions on derivations.

76
DFA Minimization: Notes

Order of selecting state pairs was arbitrary.


• All orders give same ultimate result.
• But, may record more or fewer dependences.
• Choosing states by working backwards from known non-equivalent states
produces fewest dependences.

This algorithm: O(n2) time; Huffman (1954), Moore (1956).


• Constant work per entry: initial mark test & possibly later chasing of its
dependences.
• More efficient algorithms exist, e.g., Hopcroft (1971).

77
DFA Minimization

78
Structure of Compiler

79
Lexical Analysis

• Initial part of reading and analyzing the program text.


• Text is read and divided into tokens, each of which corresponds to
a symbol in the programming language.
• Ex: Variable, keyword, delimiters or digits.

80
Lexical Analysis- Example

81
Lexical Analysis- Example

82
Example of TOKENS

83
Example of TOKENS

84
85
86
Example of NON TOKENS

87
Tasks Lexical Analyzer

Separation of the input source code into tokens.

» Stripping out the unnecessary white spaces from


the source code.

» Removing the comments from the source text.

» Keeping track of line numbers while scanning the new line characters. These
line numbers are used by the error handler to print the error messages.

» Preprocessing of macros

88
Identify tokens and lexemes?

1. x=x*(acc+123)

89
Applications of Finite Automata to Lexical Analysis

90
Applications of Finite Automata to Lexical Analysis

91
Applications of Finite Automata to Lexical Analysis

92
Applications of Finite Automata to Lexical Analysis

93

You might also like