This preview shows pages 1–9. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CMPSC 160 Translation of Programming Languages Lectures 3: Lexical Analysis (Scanning) Continued Nondeterministic Finite Automata Nondeterministic Finite Automata (NFA) for the RE ( a  b ) * abb This is a little different • S 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 nondeterministic finite automaton ( NFA ) a  b S 0 S 1 S 4 S 2 S 3 ε a b b Nondeterministic Finite Automata • An NFA accepts a string x iff there exists a path though the transition graph from s to a final state and the edge labels spell x • Transitions on ε consume no input • To “run” (simulate) the NFA, – Start in s 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 ε 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 singlevalued • 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 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 wellknown and wellunderstood • 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 to a final state minimal DFA RE NFA DFA The Cycle of Constructions 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...
View
Full
Document
This note was uploaded on 02/19/2012 for the course ENGR 361 taught by Professor Drexel during the Spring '12 term at Bloomsburg.
 Spring '12
 Drexel

Click to edit the document details