lexical analysis the scanner
play

Lexical Analysis The Scanner CSC 4181 Compiler Construction 1 - PDF document

Lexical Analysis The Scanner CSC 4181 Compiler Construction 1 Scanner 1 Introduction A scanner, sometimes called a lexical analyzer A scanner : gets a stream of characters (source program) divides it into tokens Tokens are


  1. Lexical Analysis The Scanner CSC 4181 Compiler Construction 1 Scanner 1 Introduction • A scanner, sometimes called a lexical analyzer • A scanner : – gets a stream of characters (source program) – divides it into tokens • Tokens are units that are meaningful in the source language. • Lexemes are strings which match the patterns of tokens . Scanner 2 2 1

  2. Examples of Tokens in C Tokens Lexemes identifier Age, grade,Temp, zone, q1 number 3.1416, -498127,987.76412097 string “A cat sat on a mat.”, “90183654” open parentheses ( close parentheses ) Semicolon ; reserved word if IF, if, If, iF Scanner 3 3 Scanning • When a token is found: – It is passed to the next phase of compiler. – Sometimes values associated with the token, called attributes, need to be calculated. – Some tokens, together with their attributes, must be stored in the symbol/literal table. • it is necessary to check if the token is already in the table • Examples of attributes – Attributes of a variable are name, address, type, etc. – An attribute of a numeric constant is its value. Scanner 4 4 2

  3. How to construct a scanner • Define tokens in the source language. • Describe the patterns allowed for tokens. • Write regular expressions describing the patterns. • Construct an FA for each pattern. • Combine all FA’s which results in an NFA. • Convert NFA into DFA • Write a program simulating the DFA. Scanner 5 5 Regular Expression  • a character or symbol in the alphabet  • an empty string • an empty set • if r and s are regular expressions • r | s • r s • r * • ( r ) Scanner 6 6 3

  4. Extension of regular expr. • [a ‐ z] – any character in a range from a to z • . – any character • r + – one or more repetition • r ? – optional subexpression • ~( a | b | c ), [^ abc ] – any single character NOT in the set Scanner 7 7 Examples of Patterns • ( a | A ) = the set { a , A } • [ 0 ‐ 9 ] + = ( 0 | 1 |...| 9 ) ( 0 | 1 |...| 9 )*  • [ 0 ‐ 9 ]? = ( 0 | 1 |...| 9 | ) • [ A ‐ Za ‐ z ] = ( A | B |...| Z | a | b |...| z ) • A . = the string with A following by any one symbol • ~[ 0 ‐ 9 ] = [^ 0123456789 ] = any character which is not 0 , 1 , ..., 9 Scanner 8 8 4

  5. Describing Patterns of Tokens • reservedIF = ( IF | if | If | iF ) = (I|i)(F|f) • letter = [ a ‐ zA ‐ Z ] • digit =[ 0 ‐ 9 ] • identifier = letter (letter|digit)* • numeric = ( + | ‐ )? digit + ( . digit + )? ( E ( + | ‐ )? digit + )? • Comments – { (~ } )* } // from tiny C grammar – /* ([^ * ]*[^ / ]*)* */ // C ‐ style comments – ; (~ newline )* newline // Assembly lang comments Scanner 9 9 Disambiguating Rules • IF is an identifier or a reserved word? – A reserved word cannot be used as identifier. – A keyword can also be identifier. • � � is < and � or � � � – Principle of longest substring • When a string can be either a single token or a sequence of tokens, single ‐ token interpretation is preferred. Scanner 10 10 5

  6. Nondeterministic Finite Automata A nondeterministic finite automaton (NFA) is a mathematical model that consists of 1. A set of states S A set of input symbols  2. 3. A transition function that maps state/symbol pairs to a set of states: S x {  +  }  set of S 4. A special state s 0 called the start state 5. A set of states F (subset of S) of final states INPUT: string OUTPUT: yes or no 11 11 Example NFA Transition Table:   STATE 0 1 2 3 a b a b b 0 0,1 0 3 a,b 1 2 S = {0,1,2,3} 2 3 S 0 = 0 3  = {a,b} F = {3} 12 12 6

  7. NFA Execution An NFA says ‘yes’ for an input string if there is some path from the start state to some final state where all input has been processed. NFA(int s0, int input_element) { if (all input processed and s 0 is a final state) return Yes; if (all input processed and s 0 is not a final state) return No; for all states s 1 where transition(s 0 ,table[input_element]) = s 1 if (NFA(s 1 ,input_element+1) = = Yes) return Yes; for all states s 1 where transition(s 0 ,  ) = s 1 if (NFA(s 1 ,input_element) = = Yes) return Yes; return No; } Uses backtracking to search all possible paths 13 13 Deterministic Finite Automata A deterministic finite automaton (DFA) is a mathematical model that consists of 1. A set of states S A set of input symbols  2. 3. A transition function that maps state/symbol pairs to a state: S x   S 4. A special state s 0 called the start state 5. A set of states F (subset of S) of final states INPUT: string OUTPUT: yes or no 14 14 7

  8. FA Recognizing Tokens • Identifier letter letter,digit • Numeric E � digit digit � � � � � � � � e e digit E digit digit digit • Comment � � � � � � � � 15 Scanner 15 Example • identifier = letter(letter|digit)* Scanner 16 16 8

  9. Combining FA’s • Identifiers letter letter,digit • Reserved words E,e L,l S,s E,e I,i F,f • Combined F,f I,i E,e L,l S,s E,e other letter letter,digit 17 Scanner 17 Lookahead letter, digit I,i F,f Return ID [other] Return IF 18 Scanner 18 9

  10. Implementing DFA � other � F,f Return IF I,i E,e L,l S,s E,e � other � Return ELSE � other � Return ID letter,digit • nested ‐ if • transition table Scanner 19 19 Nested IF switch (state) letter, { case 0: digit { if isletter(nxt) state=1; other elseif isdigit(nxt) 1 4 state=2; else state=3; break; } digit case 1: … 0 2 { if isletVdig(nxt) state=1; else state=4; break; } … 3 … } 20 Scanner 20 10

  11. Transition table letter, digit St 0 1 2 3 … other ch 1 4 letter 1 1 .. .. digit … digit 2 1 .. .. 0 2 … 3 4 3 … .. Scanner 21 21 11

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend