1 / 58

Lexical Analyzer

Lexical Analyzer. Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Fall 2012. Lexical Analyzer. Lexical Analyzer reads the source program character by character to produce tokens. strips out comments and whitespaces

Download Presentation

Lexical Analyzer

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. Lexical Analyzer Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Fall 2012

  2. Lexical Analyzer • Lexical Analyzerreads the source program character by character to produce tokens. • strips out comments and whitespaces • returns a token when the parser asks for • correlates error messages with the source program

  3. Token • A token is a pair of a token name and an optional attribute value. • Token name specifies the pattern of the token • Attribute stores the lexeme of the token • Tokens • Keyword: “begin”, “if”, “else”, … • Identifier: string of letters or digits, starting with a letter • Integer: a non-empty string of digits • Punctuation symbol: “,”, “;”, “(”, “)”, … • Regular expressionsare widely used to specify patterns of the tokens.

  4. Token Example

  5. Terminology of Languages • Alphabet: a finite set of symbols • ASCII • Unicode • String: a finite sequence of symbols on an alphabet •  is the empty string • |s| is the length of string s • Concatenation: xy represents x followed by y • Exponentiation: sn = s s s .. s ( n times) s0 =  • Language: a set of strings over some fixed alphabet •  the empty set is a language • The set of well-formed C programs is a language

  6. Operations on Languages Union: L1 L2 = { s| s L1 or s L2 } Concatenation: L1L2 = { s1s2 | s1  L1 and s2  L2 } (Kleene) Closure: Positive Closure:

  7. Example L1 = {a,b,c,d} L2 = {1,2} L1  L2 = {a,b,c,d,1,2} L1L2 = {a1,a2,b1,b2,c1,c2,d1,d2} L1* = all strings using letters a,b,c,d including the empty string L1+ = all strings using letters a,b,c,d without the empty string

  8. Regular Expressions Regular expression is a representation of a language that can be built from the operators applied to the symbols of some alphabet. A regular expression is built up of smaller regular expressions (using defining rules). Each regular expression r denotes a language L(r). A language denoted by a regular expression is called as a regular set.

  9. Regular Expressions (Rules) Regular expressions over alphabet  Reg. ExprLanguage it denotes  L() = {} a  L(a) = {a} (r1) | (r2) L(r1)  L(r2) (r1) (r2) L(r1) L(r2) (r)* (L(r))* (r) L(r) Extension (r)+= (r)(r)* (L(r))+ (r)?= (r) |  L(r) {} zero or one instance [a1-an] L(a1|a2|…|an) character class

  10. Regular Expressions (cont.) • We may remove parentheses by using precedence rules: • * highest • concatenation second highest • | lowest • (a(b)*)|(c)  ab*|c • Example: •  = {0,1} • 0|1 => {0,1} • (0|1)(0|1) => {00,01,10,11} • 0*=> {,0,00,000,0000,....} • (0|1)*=> all strings with 0 and 1, including the empty string

  11. Regular Definitions previously defined symbols alphabet • We can give names to regular expressions, and use these names as symbols to define other regular expressions. • A regular definitionis a sequence of the definitions of the form: d1 r1 where di is a innovative symbol and d2 r2 ri is a regular expression over symbols … in {d1,d2,...,di-1} dn rn

  12. Regular Definitions Example • Example: Identifiers in Pascal letter  A | B | ... | Z | a | b | ... | z digit  0 | 1 | ... | 9 id  letter (letter | digit ) * • If we try to write the regular expression representing identifiers without using regular definitions, that regular expression will be complex. (A|...|Z|a|...|z) ( (A|...|Z|a|...|z) | (0|...|9) ) *

  13. Grammar Regular Definitions

  14. Transition Diagram • State: represents a condition that could occur during scanning • start/initial state: • accepting/final state: lexeme found • intermediate state: • Edge: directs from one state to another, labeled with one or a set of symbols

  15. Transition Diagram for relop Transition Diagram for ``relop  < | > |< = | >= | = | <>’’

  16. Transition-Diagram-Based Lexical Analyzer Implementation of relop transition diagram

  17. Transition Diagram for Others A transition diagram for id's and keywords A transition diagram for unsigned numbers

  18. Practice a|b b a a a a a 1 2 3 1 2 3 b • Draw the transition diagram for recognizing the following regular expression a(a|b)*a

  19. Finite Automata • A finite automaton is a recognizer that takes a string, and answers “yes” if the string matches a pattern of a specified language, and “no” otherwise. • Two kinds: • Nondeterministic finite automaton (NFA) • no restriction on the labels of their edges • Deterministic finite automaton (DFA) • exactly one edge with a distinguished symbol goes out of each state • Both NFA and DFA have the same capability • We may use NFA or DFA as lexical analyzer

  20. Nondeterministic Finite Automaton (NFA) • A NFA consists of: • S: a set of states • Σ: a set of input symbols (alphabet) • A transition function: maps state-symbol pairs to sets of states • s0: a start (initial) state • F: a set of accepting states (final states) • NFA can be represented by a transition graph • Accepts a string x, if and only if there is a path from the starting state to one of accepting states such that edge labels along this path spell out x. • Remarks • The same symbol can label edges from one state to several different states • An edge may be labeled by ε, the empty string

  21. NFA Example (1) The language recognized by this NFA is (a|b) * a b

  22. NFA Example (2) NFA accepting aa* |bb*

  23. Implementing an NFA { set of all states can be accessible from a state in S by a transition on c} S  -closure({s0}) { set all of states can be accessible from s0 by -transitions } c  nextchar() while (c != eof) { begin S  -closure(move(S,c)) c  nextchar end if (SF != ) then { if S contains an accepting state } return “yes” else return “no”

  24. Deterministic Finite Automaton (DFA) start The language recognized by this DFA is also (a|b) * a b • A Deterministic Finite Automaton (DFA) is a special form of a NFA. • No state has ε- transition • For each symbol a and state s, there is at most one a labeled edge leaving s.

  25. Implementing a DFA s  s0 { start from the initial state } c  nextchar { get the next character from the input string } while (c != eof) do { do until the end of the string } begin s  move(s,c) { transition function } c  nextchar end if (s in F) then { if s is an accepting state } return “yes” else return “no”

  26. NFA vs. DFA • DFAs are widely used to build lexical analyzers. NFA DFA The language recognized 0*1*

  27. NFA vs. DFA • DFAs are widely used to build lexical analyzers. NFA DFA The language recognized (a|b) * a b

  28. Test Yourself 1) What are the languages presented by the two FAs? 1 (a) 1 0 0 1 2 3 4 5 1 0 1 0 0 0 0 6 7 8 9 1 1 1 (b) a a a a 1 2 3 4 5 a 2) For a language only accepting characters from {0,1}, please design a DFA which represents all strings containing three ‘0’s.

  29. Regular Expression  NFA • McNaughton-Yamada-Thompson (MYT) construction • Simple and systematic • Guarantees the resulting NFA will have exactly one final state, and one start state. • Construction starts from the simplest parts (alphabet symbols). • For a complex regular expression, sub-expressions are combined to create its NFA.

  30. MYT Construction i i f f start start  a • Basic rules: for subexpressions with no operators • For expression  • For a symbol a in the alphabet 

  31. MYT Construction Cont’d f N(r1) N(r2) start   i   • Inductive rules: for constructing larger NFAs from the NFAs of subexpressions (Let N(r1) and N(r2) denote NFAs for regular expressions r1 and r2, respectively) • For regular expression r1 | r2

  32. MYT Construction Cont’d f f N(r1) N(r) i start N(r2)  start   i  • For regular expression r1r2 • For regular expression r*

  33. Example: (a|b)*a b b:   a   b   a       b a     a   b  a a: (a|b): (a|b)*:  (a|b)*a:

  34. Properties of the Constructed NFA • N(r) has at most twice as many states as there are operators and operands in r. • This bound follows from the fact that each step of the algorithm creates at most two new states. • N(r) has one start state and one accepting state. The accepting state has no outgoing transitions, and the start state has no incoming transitions. • Each state of N(r) other than the accepting state has either one outgoing transition on a symbol in  or two outgoing transitions, both on .

  35. Conversion of an NFA to a DFA • Approach: Subset Construction • each state of the constructed DFA corresponds to a set of NFA states • Details • Create transition table Dtran for the DFA • Insert -closure(s0) to Dstates as initial state • Pick a not visited state T in Dstates • For each input symbol a, Create state -closure(move(T, a)), and add it to Dstates and Dtran • Repeat step (3) and (4) until all states in Dstates are vistited

  36. The Subset Construction

  37. NFA to DFA Example NFA for (a|b) * abb Transition table for DFA Equivalent DFA 4

  38. Regular Expression  DFA • First, augment the given regular expression by concatenating a special symbol # r  r# augmented regular expression • Second, create a syntax tree for the augmented regular expression. • All leaves are alphabet symbols (plus # and the empty string) • All inner nodes are operators • Third, number each alphabet symbol (plus #) (position numbers)

  39. Regular Expression  DFA Cont’d F   # 4 * a 3 | a b 1 2 (a|b)*a  (a|b)*a# augmented regular expression  a   1   a # 3 4  b  2  Syntax tree of (a|b)*a# • each symbol is at a leaf • each symbol is numbered (positions) • inner nodes are operators

  40. followpos Then we define the function followposfor the positions (positions assigned to leaves). followpos(i)-- the set of positions which can follow the position i in the strings generated by the augmented regular expression. Example: ( a | b) * a # 1 2 3 4 followpos(1) = {1,2,3} followpos(2) = {1,2,3} followpos(3) = {4} followpos(4) = {} followpos() is just defined for leaves, not defined for inner nodes.

  41. firstpos, lastpos, nullable • To compute followpos, we need three more functions defined for the nodes (not just for leaves) of the syntax tree. • firstpos(n)-- the set of the positions of the first symbols of strings generated by the sub-expression rooted by n. • lastpos(n)-- the set of the positions of the last symbols of strings generated by the sub-expression rooted by n. • nullable(n)-- true if the empty string is a member of strings generated by the sub-expression rooted by n; false otherwise

  42. Usage of the Functions   # 4 * a 3 | a b 1 2 (a|b)*a  (a|b)*a# augmented regular expression n nullable(n) = false nullable(m) = true firstpos(n) = {1, 2, 3} lastpos(n) = {3} m Syntax tree of (a|b)*a#

  43. Computing nullable, firstpos, lastpos

  44. How to evaluate followpos • Two-rules define the function followpos: • If n is concatenation-node with left child c1 and right child c2, and i is a position in lastpos(c1), then all positions in firstpos(c2) are in followpos(i). • If n is a star-node, and i is a position in lastpos(n), then all positions in firstpos(n) are in followpos(i). • If firstpos and lastpos have been computed for each node, followpos of each position can be computed by making one depth-first traversal of the syntax tree.

  45. Example -- ( a | b) * a # {1,2,3} {4} {1,2,3} {3} {4} {4}  {1,2} {1,2} {3} {3}  # 4 * {1,2} {1,2} a 3 {1} {1} {2} {2} | a b 1 2 red – firstpos blue – lastpos Then we can calculate followpos followpos(1) = {1,2,3} followpos(2) = {1,2,3} followpos(3) = {4} followpos(4) = {} • After we calculate follow positions, we are ready to create DFA for the regular expression.

  46. Algorithm (RE  DFA) • Create the syntax tree of (r) # • Calculate nullable, firstpos, lastpos, followpos • Put firstpos(root) into the states of DFA as an unmarked state. • while (there is an unmarked state S in the states of DFA) do • mark S • for each input symbol ado • let s1,...,snare positions in S and symbols in those positions are a • S’ followpos(s1)  ...  followpos(sn) • Dtran[S,a]  S’ • if (S’ is not in the states of DFA) • put S’ into the states of DFA as an unmarked state. • the start state of DFA is firstpos(root) • the accepting states of DFA are all states containing the position of #

  47. Example -- ( a | b) * a # 1 2 3 4 a b a S1 S2 b followpos(1)={1,2,3} followpos(2)={1,2,3} followpos(3)={4} followpos(4)={} S1=firstpos(root)={1,2,3}  mark S1 a: followpos(1)  followpos(3)={1,2,3,4}=S2 Dtran[S1,a]=S2 b: followpos(2)={1,2,3}=S1 Dtran[S1,b]=S1  mark S2 a: followpos(1)  followpos(3)={1,2,3,4}=S2 Dtran[S2,a]=S2 b: followpos(2)={1,2,3}=S1 Dtran[S2,b]=S1 start state: S1 accepting states: {S2}

  48. Example -- ( a | ) b c* # 1 2 3 4 followpos(1)={2} followpos(2)={3,4} followpos(3)={3,4} followpos(4)={} S1=firstpos(root)={1,2}  mark S1 a: followpos(1)={2}=S2 Dtran[S1,a]=S2 b: followpos(2)={3,4}=S3 Dtran[S1,b]=S3  mark S2 b: followpos(2)={3,4}=S3 Dtran[S2,b]=S3  mark S3 c: followpos(3)={3,4}=S3 Dtran[S3,c]=S3 start state: S1 accepting states: {S3} S2 a b S1 b c S3

  49. Minimizing Number of DFA States • For any regular language, there is always a unique minimum state DFA, which can be constructed from any DFA of the language. • Algorithm: • Partition the set of states into two groups: • G1 : set of accepting states • G2 : set of non-accepting states • For each new group G • partition G into subgroups such that states s1 and s2 are in the same group iff for all input symbols a, states s1 and s2 have transitions to states in the same group. • Start state of the minimized DFA is the group containing the start state of the original DFA. • Accepting states of the minimized DFA are the groups containing the accepting states of the original DFA.

  50. Minimizing DFA – Example (1) 2 a G1 = {2} G2 = {1,3} G2 cannot be partitioned because Dtran[1,a]=2 Dtran[1,b]=3 Dtran[3,a]=2 Dtran[3,b]=3 2 a 1 b a b 3 b So, the minimized DFA (with minimum states) is a b a 1 b

More Related