finite automata & language theory
DESCRIPTION
Finite Automata & Language Theory. Finite Automata :. A recognizer that takes an input string & determines whether it’s a valid sentence of the language. Non-Deterministic :. Deterministic :. Has more than one alternative action for the same input symbol. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/1.jpg)
CH3.1
314ALL
Dr. Mohamed Ramadan Saady
Finite Automata & Language TheoryFinite Automata & Language Theory
Finite Automata : A recognizer that takes an input string & determines whether it’s a valid sentence of the language
Non-Deterministic : Has more than one alternative action for the same input symbol.
Deterministic : Has at most one action for a given input symbol.
Both types are used to recognize regular expressions.
![Page 2: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/2.jpg)
CH3.2
314ALL
Dr. Mohamed Ramadan Saady
NFAs & DFAsNFAs & DFAs
Non-Deterministic Finite Automata (NFAs) easily represent regular expression, but are somewhat less precise.
Deterministic Finite Automata (DFAs) require more complexity to represent regular expressions, but offer more precision.
We’ll review both plus conversion algorithms, i.e., NFA DFA and DFA NFA
![Page 3: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/3.jpg)
CH3.3
314ALL
Dr. Mohamed Ramadan Saady
Non-Deterministic Finite AutomataNon-Deterministic Finite Automata
An NFA is a mathematical model that consists of :
• S, a set of states
• , the symbols of the input alphabet
• move, a transition function.
• move(state, symbol) set of states
• move : S {} Pow(S)
• A state, s0 S, the start state
• F S, a set of final or accepting states.
![Page 4: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/4.jpg)
CH3.4
314ALL
Dr. Mohamed Ramadan Saady
Representing NFAsRepresenting NFAs
Transition Diagrams :
Transition Tables:
Number states (circles), arcs, final states, …
More suitable to representation within a computer
We’ll see examples of both !
![Page 5: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/5.jpg)
CH3.5
314ALL
Dr. Mohamed Ramadan Saady
Example NFAExample NFA
S = { 0, 1, 2, 3 }
s0 = 0
F = { 3 }
= { a, b }
start0 3b21 ba
a
b
What Language is defined ?
What is the Transition Table ?
state
i n p u t
0
1
2
a b
{ 0, 1 }
-- { 2 }
-- { 3 }
{ 0 }
(null) moves possible
ji
Switch state but do not use any input symbol
![Page 6: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/6.jpg)
CH3.6
314ALL
Dr. Mohamed Ramadan Saady
How Does An NFA Work ?How Does An NFA Work ?
start0 3b21 ba
a
b • Given an input string, we trace moves
• If no more input & in final state, ACCEPT
EXAMPLE: Input: ababb
move(0, a) = 1
move(1, b) = 2
move(2, a) = ? (undefined)
REJECT !
move(0, a) = 0
move(0, b) = 0
move(0, a) = 1
move(1, b) = 2
move(2, b) = 3
ACCEPT !
-OR-
![Page 7: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/7.jpg)
CH3.7
314ALL
Dr. Mohamed Ramadan Saady
Handling Undefined TransitionsHandling Undefined Transitions
We can handle undefined transitions by defining one more state, a “death” state, and transitioning all previously undefined transition to this death state.
start0 3b21 ba
a
b
4
a, b
aa
![Page 8: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/8.jpg)
CH3.8
314ALL
Dr. Mohamed Ramadan Saady
NFA- Regular Expressions & CompilationNFA- Regular Expressions & Compilation
Problems with NFAs for Regular Expressions:
1. Valid input might not be accepted
2. NFA may behave differently on the same input
Relationship of NFAs to Compilation:
1. Regular expression “recognized” by NFA
2. Regular expression is “pattern” for a “token”
3. Tokens are building blocks for lexical analysis
4. Lexical analyzer can be described by a collection of NFAs. Each NFA is for a language token.
![Page 9: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/9.jpg)
CH3.9
314ALL
Dr. Mohamed Ramadan Saady
Second NFA ExampleSecond NFA Example
Given the regular expression : (a (b*c)) | (a (b | c+)?)
Find a transition diagram NFA that recognizes it.
![Page 10: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/10.jpg)
CH3.10
314ALL
Dr. Mohamed Ramadan Saady
Second NFA Example - SolutionSecond NFA Example - Solution
Given the regular expression : (a (b*c)) | (a (b | c+)?)
Find a transition diagram NFA that recognizes it.
0
42
1
3 5
start
c
b
c
b
c
a
String abbc can be accepted.
![Page 11: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/11.jpg)
CH3.11
314ALL
Dr. Mohamed Ramadan Saady
Alternative Solution StrategyAlternative Solution Strategy
32b
ca1
6
5
7
c
a
c
4 b
a (b*c)
a (b | c+)?
Now that you have the individual diagrams, “or” them as follows:
![Page 12: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/12.jpg)
CH3.12
314ALL
Dr. Mohamed Ramadan Saady
Using Null Transitions to “OR” NFAsUsing Null Transitions to “OR” NFAs
32b
ca1
6
5
7
c
a
c
4 b
0
![Page 13: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/13.jpg)
CH3.13
314ALL
Dr. Mohamed Ramadan Saady
Other ConceptsOther Concepts
start0 3b21 ba
a
b
Not all paths may result in acceptance.
aabb is accepted along path : 0 0 1 2 3
BUT… it is not accepted along the valid path:
0 0 0 0 0
![Page 14: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/14.jpg)
CH3.14
314ALL
Dr. Mohamed Ramadan Saady
Deterministic Finite Automata Deterministic Finite Automata
A DFA is an NFA with the following restrictions:
• moves are not allowed
• For every state s S, there is one and only one path from s for every input symbol a .
Since transition tables don’t have any alternative options, DFAs are easily simulated via an algorithm.
s s0
c nextchar;while c eof do s move(s,c); c nextchar;end;if s is in F then return “yes” else return “no”
![Page 15: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/15.jpg)
CH3.15
314ALL
Dr. Mohamed Ramadan Saady
Example - DFAExample - DFA
start0 3b21 ba
a
b
start0 3b21 ba
b
ab
aa
What Language is Accepted?
Recall the original NFA:
![Page 16: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/16.jpg)
CH3.16
314ALL
Dr. Mohamed Ramadan Saady
Conversion : NFA Conversion : NFA DFA Algorithm DFA Algorithm
• Algorithm Constructs a Transition Table for DFA from NFA
• Each state in DFA corresponds to a SET of states of the NFA
• Why does this occur ?
• moves
• non-determinism
Both require us to characterize multiple situations that occur for accepting the same string.
(Recall : Same input can have multiple paths in NFA)
• Key Issue : Reconciling AMBIGUITY !
![Page 17: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/17.jpg)
CH3.17
314ALL
Dr. Mohamed Ramadan Saady
Converting NFA to DFA – 1Converting NFA to DFA – 1stst Look Look
0 85
4
7
3
6
2
1
ba
c
From State 0, Where can we move without consuming any input ?
This forms a new state: 0,1,2,6,8 What transitions are defined for this new state ?
![Page 18: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/18.jpg)
CH3.18
314ALL
Dr. Mohamed Ramadan Saady
The Resulting DFAThe Resulting DFA
Which States are FINAL States ?
1, 2, 5, 6, 7, 81, 2, 4, 5, 6, 8
0, 1, 2, 6, 8 3
c
ba
a
a
c
c
DC
AB
c
baa
a
c
c
How do we handle alphabet symbols not defined for A, B, C, D ?
![Page 19: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/19.jpg)
CH3.19
314ALL
Dr. Mohamed Ramadan Saady
Algorithm ConceptsAlgorithm Concepts
NFA N = ( S, , s0, F, MOVE )
-Closure(s) : s S
: set of states in S that are reachable
from s via -moves of N that originate
from s.
-Closure(T) : T S
: NFA states reachable from all t T
on -moves only.
move(T,a) : T S, a : Set of states to which there is a
transition on input a from some t T
These 3 operations are utilized by algorithms / techniques to facilitate the conversion process.
No input is consumed
![Page 20: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/20.jpg)
CH3.20
314ALL
Dr. Mohamed Ramadan Saady
Illustrating Conversion – An ExampleIllustrating Conversion – An Example
First we calculate: -closure(0) (i.e., state 0)
-closure(0) = {0, 1, 2, 4, 7} (all states reachable from 0 on -moves)Let A={0, 1, 2, 4, 7} be a state of new DFA, D.
0 1
2 3
54
6 7 8 9
10
a
a
b
b
b
start
Start with NFA: (a | b)*abb
![Page 21: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/21.jpg)
CH3.21
314ALL
Dr. Mohamed Ramadan Saady
Conversion Example – continued (1)Conversion Example – continued (1)
b : -closure(move(A,b)) = -closure(move({0,1,2,4,7},b))
adds {5} ( since move(4,b)=5)
From this we have : -closure({5}) = {1,2,4,5,6,7}(since 56 1 4, 6 7, and 1 2 all by -moves)
Let C={1,2,4,5,6,7} be a new state. Define Dtran[A,b] = C.
2nd , we calculate : a : -closure(move(A,a)) and b : -closure(move(A,b))
a : -closure(move(A,a)) = -closure(move({0,1,2,4,7},a))}adds {3,8} ( since move(2,a)=3 and move(7,a)=8)
From this we have : -closure({3,8}) = {1,2,3,4,6,7,8}(since 36 1 4, 6 7, and 1 2 all by -moves)
Let B={1,2,3,4,6,7,8} be a new state. Define Dtran[A,a] = B.
![Page 22: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/22.jpg)
CH3.22
314ALL
Dr. Mohamed Ramadan Saady
Conversion Example – continued (2)Conversion Example – continued (2)
3rd , we calculate for state B on {a,b}
a : -closure(move(B,a)) = -closure(move({1,2,3,4,6,7,8},a))}= {1,2,3,4,6,7,8} = B
Define Dtran[B,a] = B.
b : -closure(move(B,b)) = -closure(move({1,2,3,4,6,7,8},b))}= {1,2,4,5,6,7,9} = D
Define Dtran[B,b] = D.
4th , we calculate for state C on {a,b}
a : -closure(move(C,a)) = -closure(move({1,2,4,5,6,7},a))}= {1,2,3,4,6,7,8} = B
Define Dtran[C,a] = B.
b : -closure(move(C,b)) = -closure(move({1,2,4,5,6,7},b))}= {1,2,4,5,6,7} = C
Define Dtran[C,b] = C.
![Page 23: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/23.jpg)
CH3.23
314ALL
Dr. Mohamed Ramadan Saady
Conversion Example – continued (3)Conversion Example – continued (3)
5th , we calculate for state D on {a,b}
a : -closure(move(D,a)) = -closure(move({1,2,4,5,6,7,9},a))}= {1,2,3,4,6,7,8} = B
Define Dtran[D,a] = B.
b : -closure(move(D,b)) = -closure(move({1,2,4,5,6,7,9},b))}= {1,2,4,5,6,7,10} = E
Define Dtran[D,b] = E.
Finally, we calculate for state E on {a,b}
a : -closure(move(E,a)) = -closure(move({1,2,4,5,6,7,10},a))}= {1,2,3,4,6,7,8} = B
Define Dtran[E,a] = B.
b : -closure(move(E,b)) = -closure(move({1,2,4,5,6,7,10},b))}= {1,2,4,5,6,7} = C
Define Dtran[E,b] = C.
![Page 24: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/24.jpg)
CH3.24
314ALL
Dr. Mohamed Ramadan Saady
Conversion Example – continued (4)Conversion Example – continued (4)
DstatesInput Symbol
a b A B C B B D C B C
E B C D B E
A
C
B D Estart bb
b
b
b
aa
a
a
This gives the transition table Dtran for the DFA of:
![Page 25: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/25.jpg)
CH3.25
314ALL
Dr. Mohamed Ramadan Saady
Algorithm For Subset ConstructionAlgorithm For Subset Construction
push all states in T onto stack;
initialize -closure(T) to T;
while stack is not empty do begin
pop t, the top element, off the stack;
for each state u with edge from t to u labeled do
if u is not in -closure(T) do begin
add u to -closure(T) ;
push u onto stack
end
end
computing the-closure
![Page 26: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/26.jpg)
CH3.26
314ALL
Dr. Mohamed Ramadan Saady
Algorithm For Subset Construction – (2)Algorithm For Subset Construction – (2)
initially, -closure(s0) is only (unmarked) state in Dstates;
while there is unmarked state T in Dstates do begin
mark T;
for each input symbol a do begin
U := -closure(move(T,a));
if U is not in Dstates then
add U as an unmarked state to Dstates;
Dtran[T,a] := U
end
end
![Page 27: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/27.jpg)
CH3.27
314ALL
Dr. Mohamed Ramadan Saady
Regular Expression to NFA ConstructionRegular Expression to NFA Construction
We now focus on transforming a Reg. Expr. to an NFA
This construction allows us to take:
• Regular Expressions (which describe tokens)
• To an NFA (to characterize language)
• To a DFA (which can be “computerized”)
The construction process is component-wise
Builds NFA from components of the regular expression in a special order with particular techniques.
NOTE: Construction is “syntax-directed” translation, i.e., syntax of regular expression is determining factor for NFA construction and structure.
![Page 28: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/28.jpg)
CH3.28
314ALL
Dr. Mohamed Ramadan Saady
Motivation: Construct NFA For:Motivation: Construct NFA For:
:
a :
b:
ab:
| ab :
a*
( | ab )* :
![Page 29: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/29.jpg)
CH3.29
314ALL
Dr. Mohamed Ramadan Saady
Motivation: Construct NFA For:Motivation: Construct NFA For:
start i f
astart 0 1
b A Bastart 0 1
bstart A B
:
a :
b:
ab:
| ab :
a*
( | ab )* :
![Page 30: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/30.jpg)
CH3.30
314ALL
Dr. Mohamed Ramadan Saady
Construction Algorithm : R.E. Construction Algorithm : R.E. NFA NFA
Construction Process :
1st : Identify subexpressions of the regular expression
symbols
r | s
rs
r*
2nd : Characterize “pieces” of NFA for each subexpression
![Page 31: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/31.jpg)
CH3.31
314ALL
Dr. Mohamed Ramadan Saady
Piecing Together NFAsPiecing Together NFAs
2. For a in the regular expression, construct NFA
astart i f L(a)
1. For in the regular expression, construct NFA
L()start i f
![Page 32: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/32.jpg)
CH3.32
314ALL
Dr. Mohamed Ramadan Saady
Piecing Together NFAs – continued(1)Piecing Together NFAs – continued(1)
where i and f are new start / final states, and -moves are introduced from i to the old start states of N(s) and N(t) as well as from all of their final states to f.
3.(a) If s, t are regular expressions, N(s), N(t) their NFAs s|t has NFA:
start i f
N(s)
N(t)
L(s) L(t)
![Page 33: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/33.jpg)
CH3.33
314ALL
Dr. Mohamed Ramadan Saady
Piecing Together NFAs – continued(2)Piecing Together NFAs – continued(2)
3.(b) If s, t are regular expressions, N(s), N(t) their NFAs st (concatenation) has NFA:
starti fN(s) N(t) L(s) L(t)
Alternative:
overlap
N(s)start i fN(t)
where i is the start state of N(s) (or new under the alternative) and f is the final state of N(t) (or new). Overlap maps final states of N(s) to start state of N(t).
![Page 34: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/34.jpg)
CH3.34
314ALL
Dr. Mohamed Ramadan Saady
Piecing Together NFAs – continued(3)Piecing Together NFAs – continued(3)
fN(s)start i
where : i is new start state and f is new final state
-move i to f (to accept null string)
-moves i to old start, old final(s) to f
-move old final to old start (WHY?)
3.(c) If s is a regular expressions, N(s) its NFA, s* (Kleene star) has NFA:
![Page 35: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/35.jpg)
CH3.35
314ALL
Dr. Mohamed Ramadan Saady
Properties of Construction Properties of Construction
1. N(r) has #of states 2*(#symbols + #operators) of r
2. N(r) has exactly one start and one accepting state
3. Each state of N(r) has at most one outgoing edge
a or at most two outgoing ’s
4. BE CAREFUL to assign unique names to all states !
Let r be a regular expression, with NFA N(r), then
![Page 36: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/36.jpg)
CH3.36
314ALL
Dr. Mohamed Ramadan Saady
Detailed ExampleDetailed Example
r13
r12r5
r3 r11r4
r9
r10
r8r7
r6
r0
r1 r2
b
*c
a a
|
( )
b
|
*
c
See example 3.16 in textbook for (a | b)*abb2nd Example - (ab*c) | (a(b|c*))
Parse Tree for this regular expression:
What is the NFA? Let’s construct it !
![Page 37: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/37.jpg)
CH3.37
314ALL
Dr. Mohamed Ramadan Saady
Detailed Example – Construction(1)Detailed Example – Construction(1)
r3: a
r0: b
r2: c
b
r1:
r4 : r1 r2b
c
r5 : r3 r4
b
a c
![Page 38: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/38.jpg)
CH3.38
314ALL
Dr. Mohamed Ramadan Saady
Detailed Example – Construction(2)Detailed Example – Construction(2)
r11: a
r7: b
r6: c
c
r9 : r7 | r8
b
r10 : r9
c
r8:
c
r12 : r11 r10
b
a
![Page 39: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/39.jpg)
CH3.39
314ALL
Dr. Mohamed Ramadan Saady
Detailed Example – Final StepDetailed Example – Final Step
r13 : r5 | r12
b
a c
c
b
a
1
6543
8
2
10
9 12 13 14
11
15
7
16
17
![Page 40: Finite Automata & Language Theory](https://reader035.vdocuments.net/reader035/viewer/2022081501/568148f7550346895db61ae9/html5/thumbnails/40.jpg)
CH3.40
314ALL
Dr. Mohamed Ramadan Saady
Direct Simulation of an NFADirect Simulation of an NFA
s s0
c nextchar;while c eof do s move(s,c); c nextchar;end;if s is in F then return “yes” else return “no”
S -closure({s0})
c nextchar;while c eof do S -closure(move(S,c)); c nextchar;end;if SF then return “yes” else return “no”
DFAsimulation
NFAsimulation