Professional Documents
Culture Documents
Theory of Automata Theory of Automata
Theory of Automata Theory of Automata
Theory of Automata Theory of Automata
Fouzia Jabeen
Chapter 3 cont…
Table Encoding of FA
Transition b
table a a
0 1 2
a b
0 1 err
1 2 1
2 err err
2
RE → Finite Automata
Can we build a finite
automaton for every regular
expression?
Yes, – build FA inductively
based on the definition of
Regular Expression
3
NFA
Nondeterministic Finite
Automaton (NFA)
Can have multiple
transitions for one input
in a given state
Can have - moves
4
Epsilon Moves
ε – moves
machine can move from state
A to state B without consuming
input
A B
5
NFA
operation of the automaton is not
completely defined by input
1
0 1
A B C
7
Acceptance of NFA
NFA can get into multiple states
Rule: NFA accepts if it can get
in a final state
1
0 1
A B C
0
8
DFA and NFA
Deterministic Finite Automata
(DFA)
One transition per input per
state.
No - moves
9
Execution of FA
A DFA
can take only one path
through the state graph.
Completely determined by
input.
10
NFA vs DFA
NFAs and DFAs recognize
the same set of languages
(regular languages)
DFAs are easier to
implement – table driven.
11
NFA vs DFA
For a given language, the
NFA can be simpler than
the DFA.
DFA can be exponentially
larger than NFA.
12
NFA vs DFA
NFAs are the key to
automating RE → DFA
construction.
13
RE → NFA Construction
Thompson’s construction
(CACM 1968)
Build an NFA for each RE
term.
Combine NFAs with
-moves.
14
RE → NFA Construction
Key idea:
NFA pattern for each
symbol and each operator.
Join them with -moves in
precedence order.
15
RE → NFA Construction
a
s0 s1
NFA for a
a b
s0 s1 s3 s4
NFA for ab
16
RE → NFA Construction
a
NFA for a s0 s1
17
RE → NFA Construction
a
NFA for a s0 s1
b
NFA for b s3 s4
18
RE → NFA Construction
a
NFA for a s0 s1
b
NFA for b s3 s4
a b
s0 s1 s3 s4
19
RE → NFA Construction
a
NFA for a s0 s1
b
NFA for b s3 s4
a b
s0 s1 s3 s4
NFA for ab
20
RE → NFA Construction
a
s1 s2
s0 s5
b
s3 s4
NFA for a | b
21
RE → NFA Construction
a
s1 s2
NFA for a
22
RE → NFA Construction
a
s1 s2
b
s3 s4
NFA for a | b
24
RE → NFA Construction
s1
a
s0 s2 s4
NFA for a*
25
RE → NFA Construction
a
s1 s2
NFA for a
26
RE → NFA Construction
s1
a
s0 s2 s4
NFA for a*
27
Lexical Analyzers
Lexical analyzers (scanners)
use the same mechanism
but they:
• Have multiple RE descriptions
for multiple tokens
• Have a character stream at the
input
28
Lexical Analyzer Generators
The lexical analysis process
can automated
We only need to specify
• Regular expressions for tokens
• Rule priorities for multiple
longest match cases
29
Lexical Analyzer Generators
Flex
generates lexical analyzer in C
or C++
Jlex
written in Java. Generates
lexical analyzer in Java
30
Using Flex
Provide a specification file
Flex reads this file and
produces C or C++ output file
contains the scanner.
The file consists of three
sections
31
Lexical Analyzers
R1…R2 RE=>NFA
NFA=>DFA
Min. DFA
character Simulate Token
stream DFA stream
32
Assignment # 2 : Try to make NFA for the following RE
1. (a+b)+ aa
2. aa (a+b)bb
3. b+ (a+b)
4. (a+b)* (a+b)(a+b)
5. a(a+b)*b
6. (ab+ba)ba
7. b* (b+a)a*
8. L(L|d)*
9. (0-9)+
10. (a+b)? aab
33