{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs160-lec3

# cs160-lec3 - CMPSC 160 Translation of Programming Languages...

This preview shows pages 1–10. Sign up to view the full content.

CMPSC 160 Translation of Programming Languages Lectures 3: Lexical Analysis (Scanning) Continued

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Non-deterministic Finite Automata Non-deterministic Finite Automata (NFA) for the RE ( a | b ) * abb This is a little different S 0 has a transition on ε (empty string) ε - transitions are allowed S 1 has two transitions on “a” Transition function δ : S × Σ 2 S maps (state, symbol) pairs to sets of states This is a non-deterministic finite automaton ( NFA ) a | b S 0 S 1 S 4 S 2 S 3 ε a b b
Non-deterministic Finite Automata An NFA accepts a string x iff there exists a path though the transition graph from s 0 to a final state and the edge labels spell x Transitions on ε consume no input To “run” (simulate) the NFA, Start in s 0 and take all the transitions for each character At each iteration add the states reachable by ε -transitions Why study NFAs? They are the key to automating the RE DFA construction We can paste together NFAs with ε -transitions NFA NFA becomes an NFA ε

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Review: NFA Simulation Two key functions move(q , a ) is set of states reachable by “a” from states in q ε -closure(q) is set of states reachable by ε from states in q states = ε -closure ( {s 0 } ); char = get_next_char(); while (char != EOF) { states = ε -closure(move (states,char)); char = get_next_char(); } if (states Final is not empty) report acceptance; else report failure;
Relationship between NFAs and DFAs DFA is a special case of an NFA DFA has no ε -transitions DFA’s transition function is single-valued Same rules will work DFA can be simulated with an NFA Obvious NFA can be simulated with a DFA Less obvious Simulate sets of possible states that NFA can reach with states of the DFA Possible exponential blowup in the state space Still, one state per character in the input stream

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Automating Scanner Construction To build a scanner: 1 Write down the RE that specifies the tokens 2 Translate the RE to an NFA 3 Build the DFA that simulates the NFA 4 Systematically shrink the DFA 5 Turn it into code or table Scanner generators Lex , Flex, Jlex, and Jflex work along these lines Algorithms are well-known and well-understood Interface to parser is important
Relationship between RE/NFA/DFA RE NFA ( Thompson’s construction ) Build an NFA for each term Combine them with ε -moves NFA DFA ( subset construction ) Build the simulation DFA Minimal DFA Hopcroft’s algorithm DFA RE All pairs, all paths problem Union together paths from s 0 to a final state minimal DFA RE NFA DFA The Cycle of Constructions

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
RE NFA using Thompson’s Construction Key idea NFA pattern for each symbol & each operator Join them with ε moves in precedence order S 0 S 1 a NFA for a S 0 S 1 a S 3 S 4 b NFA for ab ε NFA for a | b S 0 S 1 S 2 a S 3 S 4 b S 5 ε ε ε ε S 0 S 1 ε S 3 S 4 ε NFA for a * a ε ε Ken Thompson, C ACM , 1968
Thompson’s Construction Example Let’s try ( a | b ) a * 1. a, b 2. a | b 3. a * S 0 S 1 a S 0 S 1 b S 1 S 2 a S 3 S 4 b S 0 S 5 ε ε ε ε a S 1 S 2 S 0 S 3 ε ε ε ε

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}