Presentation is loading. Please wait.

Presentation is loading. Please wait.

0 The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language? Is the program well-formed.

Similar presentations


Presentation on theme: "0 The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language? Is the program well-formed."— Presentation transcript:

1 0 The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language? Is the program well-formed (semantically) ? Build an IR version of the code for the rest of the compiler The front end deals with form (syntax) & meaning (semantics) Source code Front End Errors Machine code Back End IR

2 1 The Front End Implementation Strategy Source code Scanner IR Parser Errors tokens ScanningParsing Specify Syntaxregular expressions context-free grammars Implement Recognizer deterministic finite automaton push-down automaton Perform WorkActions on transitions in automaton

3 2 The Front End Why separate the scanner and the parser? Scanner classifies words Parser constructs grammatical derivations Parsing is harder and slower Separation simplifies the implementation Scanners are simple Scanner leads to a faster, smaller parser token is a pair stream of characters Scanner IR + annotations Parser Errors stream of tokens microsyntaxsyntax Scanner is only pass that touches every character of the input.

4 3 The Big Picture The front end deals with syntax parts of speech Language syntax is specified with parts of speech, not words Syntax checking matches parts of speech against a grammar 1. goal  expr 2. expr  expr op term 3. | term 4. term  number 5. | id 6. op  + 7. | – S = goal T = { number, id, +, - } N = { goal, expr, term, op } P = { 1, 2, 3, 4, 5, 6, 7 } parts of speech syntactic variables Simple expression grammar The scanner turns a stream of characters into a stream of words, and classifies them with their part of speech.

5 4 The Big Picture Why study automatic scanner construction? Avoid writing scanners by hand Harness theory Goals: To simplify specification & implementation of scanners To understand the underlying techniques and technologies Scanner Generator specifications Scanner source codeparts of speech & words Specifications written as “regular expressions” Represent words as indices into a global table tables or code design time compile time

6 5 Regular Expressions We constrain programming languages so that the spelling of a word always implies its part of speech The rules that impose this mapping form a regular language Regular expressions ( RE s) describe regular languages Regular Expression (over alphabet  )  is a RE denoting the set {  } If a is in , then a is a RE denoting {a} If x and y are RE s denoting L(x) and L(y) then —x | y is an RE denoting L(x)  L(y) —xy is an RE denoting L(x)L(y) —x * is an RE denoting L(x)* Precedence is closure, then concatenation, then alternation

7 6 Regular Expressions How do these operators help? Regular Expression (over alphabet  )  is a RE denoting the set {  } If a is in , then a is a RE denoting {a}  the spelling of any specific word is an RE If x and y are RE s denoting L(x) and L(y) then —x |y is an RE denoting L(x)  L(y)  any finite list of words can be written as an RE ( w 0 | w 1 | … | w n ) —xy is an RE denoting L(x)L(y) —x * is an RE denoting L(x)*  we can use concatenation & closure to write more concise patterns and to specify infinite sets that have finite descriptions

8 7 Examples of Regular Expressions Identifiers : Letter  (a|b|c| … |z|A|B|C| … |Z) Digit  (0|1|2| … |9) Identifier  Letter ( Letter | Digit ) * Numbers : Integer  (+|-|  ) (0| (1|2|3| … |9)(Digit * ) ) Decimal  Integer. Digit * Real  ( Integer | Decimal ) E (+|-|  ) Digit * Complex  ( Real, Real ) Numbers can get much more complicated! underlining indicates a letter in the input stream

9 8 Regular Expressions We use regular expressions to specify the mapping of words to parts of speech for the lexical analyzer Using results from automata theory and theory of algorithms, we can automate construction of recognizers from REs  We study RE s and associated theory to automate scanner construction !  Fortunately, the automatic techiques lead to fast scanners  used in text editors, URL filtering software, …

10 9 Consider the problem of recognizing ILOC register names Register  r (0|1|2| … | 9) (0|1|2| … | 9) * Allows registers of arbitrary number Requires at least one digit RE corresponds to a recognizer (or DFA ) Transitions on other inputs go to an error state, s e Example S0S0 S2S2 S1S1 r (0|1|2| … 9) Recognizer for Register

11 10 DFA operation Start in state S 0 & make transitions on each input character DFA accepts a word x iff x leaves it in a final state (S 2 ) So, r17 takes it through s 0, s 1, s 2 and accepts r takes it through s 0, s 1 and fails a takes it straight to s e Example ( continued ) S0S0 S2S2 S1S1 r (0|1|2| … 9) Recognizer for Register

12 11 Example ( continued ) To be useful, the recognizer must be converted into code  r 0,1,2,3,4, 5,6,7,8,9 All others s0s0 s1s1 sese sese s1s1 sese s2s2 sese s2s2 sese s2s2 sese sese sese sese sese Char  next character State  s 0 while (Char  EOF ) State   (State,Char) Char  next character if (State is a final state ) then report success else report failure Skeleton recognizer Table encoding the RE O(1) cost per character (or per transition)

13 12 Example ( continued ) We can add “actions” to each transition  r 0,1,2,3,4, 5,6,7,8,9 All others s0s0 s 1 start s e error s e error s1s1 s e error s 2 add s e error s2s2 s e error s 2 add s e error sese s e error s e error s e error Char  next character State  s 0 while (Char  EOF ) Next   (State,Char) Act   (State,Char) perform action Act State  Next Char  next character if (State is a final state ) then report success else report failure Skeleton recognizerTable encoding RE Typical action is to capture the lexeme

14 13 r Digit Digit * allows arbitrary numbers Accepts r00000 Accepts r99999 What if we want to limit it to r0 through r31 ? Write a tighter regular expression —Register  r ( (0|1|2) (Digit |  ) | (4|5|6|7|8|9) | (3|30|31) ) —Register  r0|r1|r2| … |r31|r00|r01|r02| … |r09 Produces a more complex DFA DFA has more states DFA has same cost per transition (or per character) DFA has same basic implementation What if we need a tighter specification? More states implies a larger table. The larger table might have mattered when computers had 128 KB or 640 KB of RAM. Today, when a cell phone has megabytes and a laptop has gigabytes, the concern seems outdated.

15 14 Tighter register specification (continued) The DFA for Register  r ( (0|1|2) (Digit |  ) | (4|5|6|7|8|9) | (3|30|31) ) Accepts a more constrained set of register names Same set of actions, more states S0S0 S5S5 S1S1 r S4S4 S3S3 S6S6 S2S2 0,1,20,1,2 3 0,10,1 4,5,6,7,8,94,5,6,7,8,9 (0|1|2| … 9)

16 15 Tighter register specification (continued)  r0, All others s0s0 s1s1 sese sese sese sese sese s1s1 sese s2s2 s2s2 s5s5 s4s4 sese s2s2 sese s3s3 s3s3 s3s3 s3s3 sese s3s3 sese sese sese sese sese sese s4s4 sese sese sese sese sese sese s5s5 sese s6s6 sese sese sese sese s6s6 sese sese sese sese sese sese sese sese sese sese sese sese sese Table encoding RE for the tighter register specification

17 16 Tighter register specification (continued) State Action r0,123 4,5,6 7,8,9 other 0 1 start eeeee 1e 2 add 2 add 5 add 4 add e 2e 3 add 3 add 3 add 3 add e exit 3,4eeeee e exit 5e 6 add eee e exit 6eeeee e exit eeeeeee S0S0 S5S5 S1S1 r S4S4 S3S3 S6S6 S2S2 0,1,20,1,2 3 0,10,1 4,5,6,7,8,94,5,6,7,8,9 (0|1|2| … 9)

18 17 Table-Driven Scanners Common strategy is to simulate DFA execution Table + Skeleton Scanner —So far, we have used a simplified skeleton In practice, the skeleton is more complex —Character classification for table compression —Building the lexeme —Recognizing subexpressions  Practice is to combine all the REs into one DFA  Must recognize individual words without hitting EOF state  s 0 ; while (state  exit) do char  NextChar( )// read next character state   (state,char);// take the transition r s0s0 sfsf 0 … 90 … 9 0 … 90 … 9

19 18 Table-Driven Scanners Character Classification Group together characters by their actions in the DFA —Combine identical columns in the transition table,  —Indexing  by class shrinks the table Idea works well in ASCII (or EBCDIC ) —compact, byte-oriented character sets —limited range of values Not clear how it extends to larger character sets ( unicode ) state  s 0 ; while (state  exit) do char  NextChar( )// read next character cat  CharCat(char)// classify character state   (state,cat)// take the transition

20 19 Table-Driven Scanners Building the Lexeme Scanner produces syntactic category ( part of speech ) —Most applications want the lexeme (word), too This problem is trivial —Save the characters state  s 0 lexeme  empty string while (state  exit) do char  NextChar( )// read next character lexeme  lexeme + char// concatenate onto lexeme cat  CharCat(char)// classify character state   (state,cat)// take the transition

21 20 Table-Driven Scanners Choosing a Category from an Ambiguous RE We want one DFA, so we combine all the REs into one —Some strings may fit RE for more than 1 syntactic category  Keywords versus general identifiers  Would like to encode them into the RE & recognize them —Scanner must choose a category for ambiguous final states  Classic answer: specify priority by order of REs (return 1 st ) Alternate Implementation Strategy ( Quite popular ) Build hash table of keywords & fold keywords into identifiers Preload keywords into hash table Makes sense if —Scanner will enter all identifiers in the table —Scanner is hand coded Othersise, let the DFA handle them ( O(1) cost per character ) Separate keyword table can make matters worse

22 21 Table-Driven Scanners Scanning a Stream of Words Real scanners do not look for 1 word per input stream —Want scanner to find all the words in the input stream, in order —Want scanner to return one word at a time —Syntactic Solution: can insist on delimiters  Blank, tab, punctuation, …  Do you want to force blanks everywhere? in expressions? —Implementation solution  Run DFA to error or EOF, back up to accepting state Need the scanner to return token, not boolean —Token is pair —Use a map from DFA’s state to Part of Speech (PoS)

23 22 Table-Driven Scanners Handling a Stream of Words // recognize words state  s 0 lexeme  empty string clear stack push (bad) while (state  s e ) do char  NextChar( ) lexeme  lexeme + char if state ∈ S A then clear stack push (state) cat  CharCat(char) state   (state,cat) end; // clean up final state while (state ∉ S A and state ≠ bad) do state ← pop() truncate lexeme roll back the input one character end; // report the results if (state ∈ S A ) then return else return invalid Need a clever buffering scheme, such as double buffering to support roll back

24 Avoiding Excess Rollback Some REs can produce quadratic rollback —Consider ab | (ab)* c and its DFA —Input “ababababc”  s 0, s 1, s 3, s 4, s 3, s 4, s 3, s 4, s 5 —Input “abababab”  s 0, s 1, s 3, s 4, s 3, s 4, s 3, s 4, rollback 6 characters  s 0, s 1, s 3, s 4, s 3, s 4, rollback 4 characters  s 0, s 1, s 3, s 4, rollback 2 characters  s 0, s 1, s 3 This behavior is preventable —Have the scanner remember paths that fail on particular inputs —Simple modification creates the “maximal munch scanner” 23 a s0s0 s1s1 s2s2 s5s5 s3s3 s4s4 b c a a c b DFA for ab | (ab)* c c Not too pretty

25 24 Maximal Munch Scanner // recognize words state  s 0 lexeme  empty string clear stack push (bad,bad) while (state  s e ) do char  NextChar( ) InputPos  InputPos + 1 lexeme  lexeme + char if Failed[state,InputPos] then break; if state ∈ S A then clear stack push (state,InputPos) cat  CharCat(char) state   (state,cat) end // clean up final state while (state ∉ S A and state ≠ bad) do Failed[state,InputPos)  true 〈 state,InputPos 〉 ← pop() truncate lexeme roll back the input one character end // report the results if (state ∈ S A ) then return else return invalid InitializeScanner() InputPos  0 for each state s in the DFA do for i  0 to |input| do Failed[s,i]  false end;

26 Maximal Munch Scanner Uses a bit array Failed to track dead-end paths —Initialize both InputPos & Failed in InitializeScanner() —Failed requires space ∝ |input stream|  Can reduce the space requirement with clever implementation Avoids quadratic rollback —Produces an efficient scanner —Can your favorite language cause quadratic rollback?  If so, the solution is inexpensive  If not, you might encounter the problem in other applications of these technologies 25 Thomas Reps, “`Maximal munch’ tokenization in linear time”, ACM TOPLAS, 20(2), March 1998, pp

27 26 Table-Driven Versus Direct-Coded Scanners Table-driven scanners make heavy use of indexing Read the next character Classify it Find the next state Branch back to the top Alternative strategy: direct coding Encode state in the program counter —Each state is a separate piece of code Do transition tests locally and directly branch Generate ugly, spaghetti-like code More efficient than table driven strategy —Fewer memory operations, might have more branches state  s 0 ; while (state  exit) do char  NextChar( ) cat  CharCat(char ) state   (state,cat); state  s 0 ; while (state  exit) do char  NextChar( ) cat  CharCat(char ) state   (state,cat); index Code locality as opposed to random access in 

28 27 Table-Driven Versus Direct-Coded Scanners Overhead of Table Lookup Each lookup in CharCat or  involves an address calculation and a memory operation —CharCat(char) 0 + char x w w is sizeof(el’t of CharCat) —  (state,cat)  0 + (state x cols + cat) x w cols is # of columns in  w is sizeof(el’t of  ) The references to CharCat and  expand into multiple ops Fair amount of overhead work per character Avoid the table lookups and the scanner will run faster

29 28 Building Faster Scanners from the DFA A direct-coded recognizer for r Digit Digit start: accept  s e lexeme  “” count  0 goto s 0 s 0 : char  NextChar lexeme  lexeme + char count++ if (char = ‘r’) then goto s 1 else goto s out s 1 : char  NextChar lexeme  lexeme + char count++ if (‘0’  char  ‘9’) then goto s 2 else goto s out s 2 : char  NextChar lexeme  lexeme + char count  0 accept  s 2 if (‘0’  char  ‘9’) then goto s 2 else goto s out s out : if (accept  s e ) then begin for i  1 to count RollBack() report success end else report failure Fewer (complex) memory operations No character classifier Use multiple strategies for test & branch

30 29 Building Faster Scanners from the DFA A direct-coded recognizer for r Digit Digit start: accept  s e lexeme  “” count  0 goto s 0 s 0 : char  NextChar lexeme  lexeme + char count++ if (char = ‘r’) then goto s 1 else goto s out s 1 : char  NextChar lexeme  lexeme + char count++ if (‘0’  char  ‘9’) then goto s 2 else goto s out s 2 : char  NextChar lexeme  lexeme + char count  1 accept  s 2 if (‘0’  char  ‘9’) then goto s 2 else goto s out s out : if (accept  s e ) then begin for i  1 to count RollBack() report success end else report failure If end of state test is complex (e.g., many cases), scanner generator should consider other schemes Table lookup (with classification?) Binary search Direct coding the maximal munch scanner is easy, too.

31 30 What About Hand-Coded Scanners? Many (most?) modern compilers use hand-coded scanners Starting from a DFA simplifies design & understanding Avoiding straight-jacket of a tool allows flexibility —Computing the value of an integer  In LEX or FLEX, many folks use sscanf() & touch chars many times  Can use old assembly trick and compute value as it appears —Combine similar states ( serial or parallel ) Scanners are fun to write —Compact, comprehensible, easy to debug, … —Don’t get too cute ( e.g., perfect hashing for keywords )

32 31 Building Scanners The point All this technology lets us automate scanner construction Implementer writes down the regular expressions Scanner generator builds NFA, DFA, minimal DFA, and then writes out the (table-driven or direct-coded) code This reliably produces fast, robust scanners For most modern language features, this works You should think twice before introducing a feature that defeats a DFA-based scanner The ones we’ve seen (e.g., insignificant blanks, non-reserved keywords) have not proven particularly useful or long lasting


Download ppt "0 The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language? Is the program well-formed."

Similar presentations


Ads by Google