Finite Automata Language Theory Finite Automata 314 ALL

  • Slides: 40
Download presentation
Finite Automata & Language Theory Finite Automata : 314 ALL A recognizer that takes

Finite Automata & Language Theory Finite Automata : 314 ALL 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. Dr. Mohamed Ramadan Saady CH 3. 1

NFAs & DFAs 314 ALL Non-Deterministic Finite Automata (NFAs) easily represent regular expression, but

NFAs & DFAs 314 ALL 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 Dr. Mohamed Ramadan Saady CH 3. 2

Non-Deterministic Finite Automata An NFA is a mathematical model that consists of : 314

Non-Deterministic Finite Automata An NFA is a mathematical model that consists of : 314 ALL • 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, s 0 S, the start state • F S, a set of final or accepting states. Dr. Mohamed Ramadan Saady CH 3. 3

Representing NFAs 314 ALL Transition Diagrams : Number states (circles), arcs, final states, …

Representing NFAs 314 ALL Transition Diagrams : Number states (circles), arcs, final states, … Transition Tables: More suitable to representation within a computer We’ll see examples of both ! Dr. Mohamed Ramadan Saady CH 3. 4

Example NFA a S = { 0, 1, 2, 3 } 314 ALL start

Example NFA a S = { 0, 1, 2, 3 } 314 ALL start s 0 = 0 a 0 F={3} 1 b b 2 3 b = { a, b } What Language is defined ? What is the Transition Table ? input s t a t e a b 0 { 0, 1 } {0} 1 -- {2} 2 -- {3} Dr. Mohamed Ramadan Saady (null) moves possible i j Switch state but do not use any input symbol CH 3. 5

How Does An NFA Work ? a 314 ALL start a 0 b 1

How Does An NFA Work ? a 314 ALL start a 0 b 1 b 2 b 3 • 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 ! Dr. Mohamed Ramadan Saady -ORmove(0, a) = 0 move(0, b) = 0 move(0, a) = 1 move(1, b) = 2 move(2, b) = 3 ACCEPT ! CH 3. 6

Handling Undefined Transitions 314 ALL We can handle undefined transitions by defining one more

Handling Undefined Transitions 314 ALL We can handle undefined transitions by defining one more state, a “death” state, and transitioning all previously undefined transition to this death state. a start a 0 b b 1 2 b 3 a a a, b 4 Dr. Mohamed Ramadan Saady CH 3. 7

NFA- Regular Expressions & Compilation Problems with NFAs for Regular Expressions: 314 ALL 1.

NFA- Regular Expressions & Compilation Problems with NFAs for Regular Expressions: 314 ALL 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. Dr. Mohamed Ramadan Saady CH 3. 8

Second NFA Example Given the regular expression : (a (b*c)) | (a (b |

Second NFA Example Given the regular expression : (a (b*c)) | (a (b | c+)? ) 314 ALL Find a transition diagram NFA that recognizes it. Dr. Mohamed Ramadan Saady CH 3. 9

Second NFA Example - Solution Given the regular expression : (a (b*c)) | (a

Second NFA Example - Solution Given the regular expression : (a (b*c)) | (a (b | c+)? ) 314 ALL Find a transition diagram NFA that recognizes it. b c 2 4 start 0 a b 1 c 3 c 5 String abbc can be accepted. Dr. Mohamed Ramadan Saady CH 3. 10

Alternative Solution Strategy a (b*c) 1 b a c 2 3 314 ALL 6

Alternative Solution Strategy a (b*c) 1 b a c 2 3 314 ALL 6 a (b | c+)? 4 a 5 b c c Now that you have the individual diagrams, “or” them as follows: Dr. Mohamed Ramadan Saady 7 CH 3. 11

Using Null Transitions to “OR” NFAs 314 ALL 1 b a c 2 3

Using Null Transitions to “OR” NFAs 314 ALL 1 b a c 2 3 6 0 4 a 5 b c c 7 Dr. Mohamed Ramadan Saady CH 3. 12

Other Concepts Not all paths may result in acceptance. a 314 ALL start a

Other Concepts Not all paths may result in acceptance. a 314 ALL start a 0 1 b 2 b 3 b aabb is accepted along path : 0 0 1 2 3 BUT… it is not accepted along the valid path: 0 0 0 Dr. Mohamed Ramadan Saady CH 3. 13

Deterministic Finite Automata A DFA is an NFA with the following restrictions: 314 ALL

Deterministic Finite Automata A DFA is an NFA with the following restrictions: 314 ALL • 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 s 0 c nextchar; while c eof do s move(s, c); c nextchar; end; if s is in F then return “yes” else return “no” Dr. Mohamed Ramadan Saady CH 3. 14

Example - DFA b a 314 ALL start a 0 b 1 b 2

Example - DFA b a 314 ALL start a 0 b 1 b 2 3 a b a What Language is Accepted? Recall the original NFA: a start a 0 1 b 2 b 3 b Dr. Mohamed Ramadan Saady CH 3. 15

Conversion : NFA DFA Algorithm 314 ALL • Algorithm Constructs a Transition Table for

Conversion : NFA DFA Algorithm 314 ALL • 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 ! Dr. Mohamed Ramadan Saady CH 3. 16

Converting NFA to DFA – 1 st Look 314 ALL a 2 b 3

Converting NFA to DFA – 1 st Look 314 ALL a 2 b 3 4 0 1 5 8 6 c 7 From State 0, Where can we move without consuming any input ? This forms a new state: 0, 1, 2, 6, 8 this new state ? Dr. Mohamed Ramadan Saady What transitions are defined for CH 3. 17

The Resulting DFA a 0, 1, 2, 6, 8 314 ALL 3 a a

The Resulting DFA a 0, 1, 2, 6, 8 314 ALL 3 a a c b 1, 2, 5, 6, 7, 8 c 1, 2, 4, 5, 6, 8 c Which States are FINAL States ? a A a B b c Dr. Mohamed Ramadan Saady c a How do we handle alphabet symbols not defined for A, B, C, D ? C CH 3. 18

Algorithm Concepts NFA 314 ALL N = ( S, , s 0, F, MOVE

Algorithm Concepts NFA 314 ALL N = ( S, , s 0, F, MOVE ) -Closure(s) : s S : set of states in S that are reachable No input is from s via -moves of N that originate consumed 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. Dr. Mohamed Ramadan Saady CH 3. 19

Illustrating Conversion – An Example Start with NFA: a 2 314 ALL (a |

Illustrating Conversion – An Example Start with NFA: a 2 314 ALL (a | b)*abb 3 start 0 1 6 b 4 5 a 7 b 8 9 b 10 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. Dr. Mohamed Ramadan Saady CH 3. 20

Conversion Example – continued (1) 2 nd , we calculate : a : -closure(move(A,

Conversion Example – continued (1) 2 nd , we calculate : a : -closure(move(A, a)) and b : -closure(move(A, b)) 314 ALL 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 3 6 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. 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 5 6 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. Dr. Mohamed Ramadan Saady CH 3. 21

Conversion Example – continued (2) 3 rd , we calculate for state B on

Conversion Example – continued (2) 3 rd , 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 314 ALL 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. 4 th , 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. Dr. Mohamed Ramadan Saady CH 3. 22

Conversion Example – continued (3) 5 th , we calculate for state D on

Conversion Example – continued (3) 5 th , 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 314 ALL 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. Dr. Mohamed Ramadan Saady CH 3. 23

Conversion Example – continued (4) This gives the transition table Dtran for the DFA

Conversion Example – continued (4) This gives the transition table Dtran for the DFA of: 314 ALL Input Symbol a b Dstates A B C D E B B B C D C E C b start A a B a Dr. Mohamed Ramadan Saady b b a D b E a CH 3. 24

Algorithm For Subset Construction push all states in T onto stack; 314 ALL initialize

Algorithm For Subset Construction push all states in T onto stack; 314 ALL initialize -closure(T) to T; computing the -closure 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 Dr. Mohamed Ramadan Saady CH 3. 25

Algorithm For Subset Construction – (2) initially, -closure(s 0) is only (unmarked) state in

Algorithm For Subset Construction – (2) initially, -closure(s 0) is only (unmarked) state in Dstates; 314 ALL 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 Dr. Mohamed Ramadan Saady CH 3. 26

Regular Expression to NFA Construction We now focus on transforming a Reg. Expr. to

Regular Expression to NFA Construction We now focus on transforming a Reg. Expr. to an NFA 314 ALL 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. Dr. Mohamed Ramadan Saady CH 3. 27

Motivation: Construct NFA For: Î: 314 ALL a: b: ab: | ab : a*

Motivation: Construct NFA For: Î: 314 ALL a: b: ab: | ab : a* ( | ab )* : Dr. Mohamed Ramadan Saady CH 3. 28

Motivation: Construct NFA For: Î: 314 ALL start i f start a: b: start

Motivation: Construct NFA For: Î: 314 ALL start i f start a: b: start ab: start b A 0 a 0 1 B a 1 A b B | ab : a* ( | ab )* : Dr. Mohamed Ramadan Saady CH 3. 29

Construction Algorithm : R. E. NFA Construction Process : 314 ALL 1 st :

Construction Algorithm : R. E. NFA Construction Process : 314 ALL 1 st : Identify subexpressions of the regular expression symbols r|s rs r* 2 nd : Characterize “pieces” of NFA for each subexpression Dr. Mohamed Ramadan Saady CH 3. 30

Piecing Together NFAs 1. For in the regular expression, construct NFA 314 ALL start

Piecing Together NFAs 1. For in the regular expression, construct NFA 314 ALL start i f L( ) 2. For a in the regular expression, construct NFA start Dr. Mohamed Ramadan Saady i a f L(a) CH 3. 31

Piecing Together NFAs – continued(1) 314 ALL 3. (a) If s, t are regular

Piecing Together NFAs – continued(1) 314 ALL 3. (a) If s, t are regular expressions, N(s), N(t) their NFAs s|t has NFA: N(s) start L(s) L(t) i f N(t) 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. Dr. Mohamed Ramadan Saady CH 3. 32

Piecing Together NFAs – continued(2) 3. (b) If s, t are regular expressions, N(s),

Piecing Together NFAs – continued(2) 3. (b) If s, t are regular expressions, N(s), N(t) their NFAs st (concatenation) has NFA: 314 ALL start i N(s) N(t) f L(s) L(t) overlap Alternative: start i N(s) N(t) f 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). Dr. Mohamed Ramadan Saady CH 3. 33

Piecing Together NFAs – continued(3) 3. (c) If s is a regular expressions, N(s)

Piecing Together NFAs – continued(3) 3. (c) If s is a regular expressions, N(s) its NFA, s* (Kleene star) has NFA: 314 ALL start i N(s) f 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? ) Dr. Mohamed Ramadan Saady CH 3. 34

Properties of Construction Let r be a regular expression, with NFA N(r), then 314

Properties of Construction Let r be a regular expression, with NFA N(r), then 314 ALL 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 ! Dr. Mohamed Ramadan Saady CH 3. 35

Detailed Example See example 3. 16 in textbook for (a | b)*abb 2 nd

Detailed Example See example 3. 16 in textbook for (a | b)*abb 2 nd Example - (ab*c) | (a(b|c*)) Parse Tree for this regular expression: 314 ALL r 13 r 5 r 3 a r 12 | r 4 r 11 r 2 a r 10 ( r 7 r 0 b * c b What is the NFA? Let’s construct it ! Dr. Mohamed Ramadan Saady ) r 9 r 8 | r 6 * c CH 3. 36

Detailed Example – Construction(1) 314 ALL r 0 : b r 3 : a

Detailed Example – Construction(1) 314 ALL r 0 : b r 3 : a r 2 : c r 1 : b r 4 : r 1 r 2 b c r 5 : r 3 r 4 a b c Dr. Mohamed Ramadan Saady CH 3. 37

Detailed Example – Construction(2) 314 ALL r 7 : b r 11: a r

Detailed Example – Construction(2) 314 ALL r 7 : b r 11: a r 8 : c b c r 6 : r 9 : r 7 | r 8 c r 10 : r 9 b a r 12 : r 11 r 10 c Dr. Mohamed Ramadan Saady CH 3. 38

Detailed Example – Final Step r 13 : r 5 | r 12 314

Detailed Example – Final Step r 13 : r 5 | r 12 314 ALL a 2 3 4 b 5 6 c 7 1 b 10 a 9 11 8 17 12 13 c 14 15 16 Dr. Mohamed Ramadan Saady CH 3. 39

Direct Simulation of an NFA 314 ALL s s 0 c nextchar; while c

Direct Simulation of an NFA 314 ALL s s 0 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({s 0}) c nextchar; while c eof do S -closure(move(S, c)); c nextchar; end; if S F then return “yes” else return “no” Dr. Mohamed Ramadan Saady DFA simulation NFA simulation CH 3. 40