1 / 72

Chapter 2: Scanning

This chapter discusses the scanning phase of the compiler, also known as the lexical analyzer. Topics covered include the interface between the source and compiler, input buffering, tokens, patterns, lexemes, regular expressions, and finite automata.

lorriej
Download Presentation

Chapter 2: Scanning

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2: Scanning 朱治平

  2. Scanner (or Lexical Analyzer) • the interface between source & compiler • could be a separate pass and places its output on an intermediate file. • more commonly, it is a routine called by parser. • scans character stream from where it left off and returns next token to parser. Actually the token's lexical category is returned in the form of a simple index # and a value for the token is left in the global variables. For some tokens only token type is returned.

  3. Input Buffering • Why input buffer is needed? ==> we can identify some token only when many characters beyond the token have been examined. • Two pointers (one marks the beginning of the token & the other one is a lookahead pointer) delimit the context of the token string. • However, the lookahead is limited.

  4. a [index] = 4 + 2 a [ i n d e x ] = 4 + 2 Beginner pointer Lookahead pointer a [ i n d e x ] = 4 + 2 Lookahead pointer Beginner pointer

  5. Token, Pattern & Lexeme • A token is a sequence of characters that represents a unit of information in the source program. • In general, there is a set of strings in the input for which the same token is produced as output. • This set of strings is described by a rule called a pattern associated with the token. The pattern is said to match each specific string in the set. • A lexeme is a sequence of characters in the source program that is matched by the pattern for a token.

  6. Pattern vs. Regular Expression Regular Expression: A notation suitable for describing tokens (patterns). Each pattern is a regular language, i.e., it can be described by a regular expression

  7. Two kinds of token • specific string ( e.g., "if" "," "=="), that is, a single string. • class of string (e.g., identifier, number, label), that is, a multiple strings.

  8. Common Lexical Categories • identifiers • literals • keywords (not necessarily to be a reserved word) - What is the difference between keyword and reserved word? • operators • numbers • punctuation symbols: e.g. '(‘ , ';‘ , ','

  9. The lexical categories are regular languages and they can be described by giving regularexpressions.

  10. An instance: Scanning E = M * C ** 2 // return token type (an index) and value ==> < id, pointer to symbol table entry for E > < assign_op > < id, pointer to symbol table entry for M > < mult_op > < id, pointer to symbol table entry for C > < expo_op > < num, integer value 2 >

  11. The compiler may store the character string that forms a number in a symbol table and let the attribute of token const be a pointer to the table entry.

  12. Regular Expression vs. Finite Automata • Regular Expression :A notation suitable for describing tokens (patterns). • Regular Expression can be converted into Finite Automata • Finite Automata : Formal specifications of transition diagrams

  13. Definitions • symbol: undefined entity. e.g. digits, letters • alphabet (character class): any finite set of symbols. (Usually it is denoted as the symbol ) e.g. the set {0,1} is an alphabet • string (sentence, word): a finite sequence of symbols.  e.g. 001, 0, 111

  14. Operations of string (I) • Length e.g., |0| = 1, |x| = the total number of symbols in string x, empty string denoted  || = 0 (note: {}  ) • Concatenation e.g., x.y = xy, x = x, x = x, x1 = x, x0 = , x2 = xx • Prefix - any # of leading symbols at the string • proper prefix (shown below)

  15. Operations of string (II) • Suffix - any # of trailing symbols at the string • proper suffix (explained below) • Substring - any string obtained by deleting a prefix and a suffix. • proper substring (explained below)

  16. A nonempty string y is a proper prefix, suffix, sub-string of x if it is a prefix, suffix, sub-string of x and x  y.

  17. Definition • language: any set of strings formed from a specific alphabet. e.g.  , {} , {abc, a}, the set of all Fortran programs, the set of all English sentences. (Note: this definition does not assign any meaning to strings in the language.)

  18. Three major operations for languages let L and M be languages 1. concatenation ==> LM = { xy | x is in L and y is in M } e.g., L{} = L, {}L = L, L0 = {}, Li = LLLL...L (i times) L = L =  2. union ==> L  M = { x | x is in L or x is in M } e.g., L  = L,

  19. 3. kleen closure (means 'any number of instances') ==> L* = L0  L1  L2  ... e.g., if D is a language and D = {0, 1, ..., 9} then D* is all strings of digits. * 4. positive closure (means 'one or more instances of') ==> L+ = L1  L2  ...

  20. Regular Expression vs. Regular Language • Regular Expression is the notation we use to describe regular sets (regular languages). e.g. identifier = letter (letter|digit)* where | means 'or', i.e. union (Note: Regular expressions over some alphabet ) • Each regular expression denotes a (regular) language. Let r be a regular expression, then L(r) be the (regular) language generated by r.

  21. letter = 'a'|'b'|..|'z'|'A'|'B'|..|'Z' digit = '0'|'1'|'2'|..|'9' identifier = letter (letter|digit)*

  22. Rules for constructing regular expressions 1.  is a regular expression denoting { }. i.e., L() = { } 2.  is a regular expression denoting {}, the language containing only empty string . L() = {} 3. For each a in , a is a regular expression denoting {a}, a single string a. i.e., L(a) = { a } 4. If R and S are regular expressions then (R)|(S), (R)(S), (R)* are regular expressions denoting union, concatenation, kleen closure of R and S.

  23. Rules of Regular Expressions 1. R|S = S|R 2. R|(S|T) = (R|S)|T 3. R(S|T) = RS|RT (S|T)R = SR|TR 4. R(ST) = (RS)T 5. R = R = R * Precedence: ( ), *, ● , |

  24. What do the following regular expressions mean? Let alphabet = {a,b} // Thus the regular expression a denotes {a} which is different from just the string a. 1. a* ? 2. (a|b)* ? // all strings of a's and b's, including the empty string. 3. a|ba* ? 4. (aa|ab|ba|bb)* ? // all strings of even length 5. |a|b ?// all strings of length 0 or 1 6. (a|b)(a|b)(a|b)(a|b)* and |a|b|(a|b)(a|b)(a|b)* ?

  25. Question: Show how to denote all strings of length not 2. • Question: Show how to denote the comments of C language.

  26. Finite Automata (Finite state machine) • Depict FA as a directed graph • each vertex corresponds to a state • there is an edge from the vertex corresponding to state qi to vertex corresponding to state qj iff the FA has a transition from state qi to qj on input σ. • i.e., qi qj σ

  27. Finite Automata (Finite state machine) • The language defined by an F. A. (Finite Automata) is the set of input string it accepts. • regular expression --> non-deterministic F. A. --> deterministic F. A. --> regular expression

  28. Deterministic Finite Automata • It has no transition on input  • For each state s and input symbol a, there is at most one edge labeled a leaving s. • M = (Q, , , q0, F), where Q = a finite set of states  = a set of finite characters  = transition function  : Q  Q q0 = starting state F = a set of accepting states (a subset of Q i.e. F  Q )

  29. e.g. DFA accepting (a|b)*abb • Q ={q0, q1, q2 ,q3 } • a  ={a, b} • b a q0 = starting state • q0 a q1 b q2 b q3 F = {q3 } (q0 ,a) = q1 (q0 ,b) = q0 (q1 ,a) = q1 (q1 ,b) = q2 (q2 ,a) = q1 (q2 ,b) = q3 (q3 ,a) = q1 (q3 ,b) = q0 b a

  30. That is, a string x is accepted by a F.A. if  (q0, x)  F • So, the language accepted by M is L(M) = { x |  (q0, x)  F}

  31. Non-deterministic Finite Automata • M' = (Q', , ', q0, F), where Q’ = a finite set of states  = a set of finite characters ' = transition function  ': Q'  ( )  2Q‘ q0 = starting state F = a set of accepting states, F  Q * Given a string x belonging to * if there exists a path in a NFA M’ s.t. M’ is in a final state after reading x, then x is accepted by M’. i.e., if ’( q0, x) =  Q' and ∩ F ≠  then x belongs to L(M’).

  32. e.g. NFA accepting (a|b)*abb a b b q0 q1 q2 q3 a, b Q ={q0, q1, q2 ,q3 }  ={a, b} q0 = starting state F = {q3 } (q0 ,a) = {q0, q1 } (q0 ,b) = {q0} (q1 ,b) = {q2} (q2 ,b) = {q3 }

  33. Regular Definition • Give names to regular expressions and then define regular expressions using these names as if they were symbols. A sequence of definitions of the form d1 = r1 d2 = r2 ………. dn = rn where di is a distinct name and ri is a regular expression over the symbols in  {d1 , d2, ..., di-1}

  34. An Example e.g. letter = 'a'|'b'|..|'z'|'A'|'B'|..|'Z' digit = '0'|'1'|'2'|..|'9' id = letter (letter|digit)*

  35. Lexical Analyzer: May return token's lexical category (an index) and value (in global variable) or token's lexical category only e.g. letter(letter|digit)* {yylval = install(); return ID;} digit+ {yylval = install(); return NUM;} "<" {yylval = LT; return RELOP;}

  36. Trick for differentiating keyword and identifier ==> install all keywords in symbol table first

  37. Theorem The followings are equivalent: • regular expression • NFA • DFA • regular language • regular grammar

  38. Algorithm: Constructing an NFA from a regular expression (Thompson’s construction) 1. For  we construct the NFA ==> q0q1 2. For a in  we construct the NFA ==> q0 a q1 3. Proceed to combine them in ways that correspond to the way compound regular expressions are formed, i.e., transition diagram for , . |  * +

  39.  |         *    

  40. In the process of constructing NFA the reason why we make each state have no more than two transitions out is that we thus can represent each state by a triple of the form (symbol, nextstate1, nextstate2).

  41. Example : Construct the r.e. (a|b)*abb based on the above algorithm.

  42. e.g. for r.e. (a|b)*abb  a   2 3  a b b  6 7 8 9 10 0 1 b   4 5 

  43. How to construct a DFA based on an equivalent NFA? • Each state of the DFA is a set of states which the NFA could be in after reading some sequence of input symbols. • The initial state of the DFA is the set consisting of S0, the initial state of NFA, together with all states of NFA that can be reached from S0 by means of -transition.

  44. -closure(s) Def. The set of states that can be reached from s on -transition alone. Method: 1. s is added to -closure(s). 2. if t is in -closure(s), and there is an edge labeled  from t to u then u is added to -closure(s) if u is not already there.

  45. -closure(T) : The union over all states s in T of -closure(s). (Note: T is a set of states) move(T,a): a set of NFA states to which there is a transition on input symbol a from some state s in T.

  46. Algorithms 1. Computation of -closure 2. The subset construction Given a NFA M, construct a corresponding DFA M’ (1)Compute the -closure(s), s is the start state of M. Let it be S. S is the start state of M’. (2) …..

  47. Algorithms (continued) Let M = (Q, , , q0, F) be a NFA accepting L, construct a DFA M' = (Q', , ', q0', F') as follows: Q'  2Q, q0' = -closure (q0) = [q0, ... , qi] '([q0,..., qi], a) = -closure({p1,...,pi}) = [p1,...,pi] iff ({q0, ..., qi},a) = { p1, ... ,pi } ….. Continue the above process until no new states are created. F'= {q'  Q' |q'  F }

  48. e.g. for r.e. (a|b)*abb a D B A = [0 1 2 4 7] 1 2 3 4 6 7 8 b 1 2 4 5 6 7 9 B = [1 2 3 4 6 7 8] a C = [1 2 4 5 6 7] 0 1 2 4 7 a D = [1 2 4 5 6 7 9] a a b E = [1 2 4 5 6 7 10] 1 2 4 5 6 7 b 1 2 4 5 6 7 10 b A C E b D F A

  49. e.g. for r.e. (a|b)*abb  a   2 3  a b b  6 7 8 9 10 0 1 b   4 5  N F A

  50. Minimizing the number of states of DFA Principle: only the states in a subset of NFA that have a non epsilon-transition determine the DFA state to which it goes on input symbol. - Two subsets can be identified as one state of the DFA, provided:   1. they have the same non--transition-only states 2. they either both include or both exclude accepting states of the NFA

More Related