Cs160-lec3 - CMPSC 160 Translation of Programming Languages Lectures 3 Lexical Analysis(Scanning Continued Non-deterministic Finite Automata

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 160 Translation of Programming Languages Lectures 3: Lexical Analysis (Scanning) Continued Non-deterministic Finite Automata Non-deterministic 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 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 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 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 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 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.

Page1 / 28

Cs160-lec3 - CMPSC 160 Translation of Programming Languages Lectures 3 Lexical Analysis(Scanning Continued Non-deterministic Finite Automata

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online