{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Ch3 - 1 Lexical Analysis and Lexical Analyzer Generators...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lexical Analysis and Lexical Analyzer Generators Chapter 3 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011
Image of page 1

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

View Full Document Right Arrow Icon
2 The Reason Why Lexical Analysis is a Separate Phase Simplifies the design of the compiler LL(1) or LR(1) parsing with 1 token lookahead would not be possible (multiple characters/tokens to match) Provides efficient implementation Systematic techniques to implement lexical analyzers by hand or automatically from specifications Stream buffering methods to scan input Improves portability Non-standard symbols and alternate character encodings can be normalized (e.g. trigraphs)
Image of page 2
3 Interaction of the Lexical Analyzer with the Parser Lexical Analyzer Parser Source Program Token, tokenval Symbol Table Get next token error error
Image of page 3

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

View Full Document Right Arrow Icon
4 Attributes of Tokens Lexical analyzer < id , “ y ”> < assign , > < num , 31> < + , > < num , 28> < * , > < id , “ x ”> y := 31 + 28*x Parser token tokenval (token attribute)
Image of page 4
5 Tokens, Patterns, and Lexemes • A token is a classification of lexical units For example: id and num Lexemes are the specific character strings that make up a token For example: abc and 123 Patterns are rules describing the set of lexemes belonging to a token For example: “ letter followed by letters and digits” and non-empty sequence of digits
Image of page 5

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

View Full Document Right Arrow Icon
6 Specification of Patterns for Tokens: Definitions • An alphabet ! is a finite set of symbols (characters) • A string s is a finite sequence of symbols from ! " s " denotes the length of string s # denotes the empty string, thus "#" = 0 • A language is a specific set of strings over some fixed alphabet !
Image of page 6
7 Specification of Patterns for Tokens: String Operations • The concatenation of two strings x and y is denoted by xy • The exponentation of a string s is defined by s 0 = # s i = s i- 1 s for i > 0 note that s # = # s = s
Image of page 7

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

View Full Document Right Arrow Icon
8 Specification of Patterns for Tokens: Language Operations Union L $ M = { s " s % L or s % M } Concatenation LM = { xy " x % L and y % M } Exponentiation L 0 = { # }; L i = L i -1 L Kleene closure L * = $ i =0,…, & L i Positive closure L + = $ i =1,…, & L i
Image of page 8
9 Specification of Patterns for Tokens: Regular Expressions Basis symbols: # is a regular expression denoting language { # } a % ! is a regular expression denoting { a } • If r and s are regular expressions denoting languages L ( r ) and M ( s ) respectively, then r " s is a regular expression denoting L ( r ) $ M ( s ) rs is a regular expression denoting L ( r ) M ( s ) r * is a regular expression denoting L ( r ) * – ( r ) is a regular expression denoting L ( r ) A language defined by a regular expression is called a regular set
Image of page 9

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

View Full Document Right Arrow Icon
10 Specification of Patterns for Tokens: Regular Definitions Regular definitions introduce a naming convention: d 1 ' r 1 d 2 ' r 2 d n ' r n where each r i is a regular expression over ! $ { d 1 , d 2 , …, d i -1 } • Any d j in r i can be textually substituted in r i to obtain an equivalent set of definitions
Image of page 10
11 Specification of Patterns for Tokens: Regular Definitions • Example: letter ' A " B " " Z " a " b " " z digit ' 0 " 1 " " 9 id ' letter ( letter " digit ) * Regular definitions are not recursive: digits ' digit digits " digit wrong!
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern