scanning
play

Scanning COMP 520: Compiler Design (4 credits) Alexander Krolik - PowerPoint PPT Presentation

COMP 520 Winter 2017 Scanning (1) Scanning COMP 520: Compiler Design (4 credits) Alexander Krolik alexander.krolik@mail.mcgill.ca MWF 13:30-14:30, MD 279 COMP 520 Winter 2017 Scanning (2) Announcements (Friday, January 6th) Facebook group:


  1. COMP 520 Winter 2017 Scanning (1) Scanning COMP 520: Compiler Design (4 credits) Alexander Krolik alexander.krolik@mail.mcgill.ca MWF 13:30-14:30, MD 279

  2. COMP 520 Winter 2017 Scanning (2) Announcements (Friday, January 6th) Facebook group: • Useful for discussions/announcements • Link on myCourses or in email Milestones: • Continue picking your group (3 recommended) • Create a GitHub account, learn git as needed Midterm: • Either 1st or 2nd week after break on the Friday • 1.5 hour “in class” midterm, so either 30 minutes before/after class. Thoughts? • Tentative date: Friday, March 10th. Or the week after? Thoughts?

  3. COMP 520 Winter 2017 Scanning (3) Readings Textbook, Crafting a Compiler: • Chapter 2: A Simple Compiler • Chapter 3: Scanning–Theory and Practice Modern Compiler Implementation in Java: • Chapter 1: Introduction • Chapter 2: Lexical Analysis Flex tool: • Manual - https://github.com/westes/flex • Reference book, Flex & bison - http://mcgill.worldcat.org/title/flex-bison/oclc/457179470

  4. COMP 520 Winter 2017 Scanning (4) Scanning: • also called lexical analysis; • is the first phase of a compiler; • takes an arbitrary source file, and identifies meaningful character sequences. • note: at this point we do not have any semantic or syntactic information Overall: • a scanner transforms a string of characters into a string of tokens.

  5. COMP 520 Winter 2017 Scanning (5) An example: tVAR tIDENTIFIER: a tASSIGN tINTEGER: 5 tIF var a = 5 tLPAREN if (a == 5) tIDENTIFIER: a { tEQUALS print "success" tINTEGER: 5 } tRPAREN tLBRACE tIDENTIFIER: print tSTRING: success tRBRACE

  6. COMP 520 Winter 2017 Scanning (6) Review of COMP 330: • Σ is an alphabet , a (usually finite) set of symbols; • a word is a finite sequence of symbols from an alphabet; • Σ ∗ is a set consisting of all possible words using symbols from Σ ; • a language is a subset of Σ ∗ . An example: • alphabet: Σ ={0,1} • words: { ǫ , 0, 1, 00, 01, 10, 11, . . . , 0001, 1000, . . . } • language: – {1, 10, 100, 1000, 10000, 100000, . . . }: “1” followed by any number of zeros – {0, 1, 1000, 0011, 11111100, . . . }: ?!

  7. COMP 520 Winter 2017 Scanning (7) A regular expression: • is a string that defines a language (set of strings); • in fact, a regular language. A regular language: • is a language that can be accepted by a DFA; • is a language for which a regular expression exists.

  8. COMP 520 Winter 2017 Scanning (8) In a scanner, tokens are defined by regular expressions : • ∅ is a regular expression [the empty set: a language with no strings] • ε is a regular expression [the empty string] • a , where a ∈ Σ is a regular expression [ Σ is our alphabet] • if M and N are regular expressions, then M | N is a regular expression [alternation: either M or N ] • if M and N are regular expressions, then M · N is a regular expression [concatenation: M followed by N ] • if M is a regular expression, then M ∗ is a regular expression [zero or more occurences of M ] What are M ? and M + ?

  9. COMP 520 Winter 2017 Scanning (9) Examples of regular expressions: • Alphabet Σ ={a,b} • a* = { ǫ , a, aa, aaa, aaaa, . . . } • (ab)* = { ǫ , ab, abab, ababab, . . . } • (a|b)* = { ǫ , a, b, aa, bb, ab, ba, . . . } • a*ba* = strings with exactly 1 “b” • (a|b)*b(a|b)* = strings with at least 1 “b”

  10. COMP 520 Winter 2017 Scanning (10) We can write regular expressions for the tokens in our source language using standard POSIX notation: • simple operators: "*" , "/" , "+" , "-" • parentheses: "(" , ")" • integer constants: 0|([1-9][0-9]*) • identifiers: [a-zA-Z_][a-zA-Z0-9_]* • white space: [ \t\n]+ [. . . ] define a character class : • matches a single character from a set; • allows ranges of characters to be “alternated”; and • can be negated using “ ^ ” (i.e. [^\n] ). The wildcard character: • is represented as “.” (dot); and • matches all characters except newlines by default (in most implementations).

  11. COMP 520 Winter 2017 Scanning (11) A scanner: • can be generated using tools like flex (or lex ), JFlex , . . . ; • by defining regular expressions for each type of token. Internally, a scanner or lexer : • uses a combination of deterministic finite automata (DFA); • plus some glue code to make it work.

  12. COMP 520 Winter 2017 Scanning (12) A finite state machine (FSM): • represents a set of possible states for a system; • uses transitions to link related states. A deterministic finite automaton (DFA): • is a machine which recognizes regular languages; • for an input sequence of symbols, the automaton either accepts or rejects the string; • it works deterministically - that is given some input, there is only one sequence of steps.

  13. COMP 520 Winter 2017 Scanning (13) Background (DFAs) from textbook, “Crafting a Compiler”

  14. COMP 520 Winter 2017 Scanning (14) DFAs (for the previous example regexes): ❧ ✲ ❤ ❧ ❧ ✲ ❤ ❧ ❧ ✲ ❧ ❤ ✲ ✲ / ✲ + * ❧ ❤ ❧ ❧ ✲ ❧ ❤ ❧ ✲ ❤ ❧ - ( ) ✲ ✲ ✲ ✲ ❤ ❧ 0 ✲ ❄ ✑✑ ✸ ❧ ❧ ❤ ❧ a-zA-Z_ ✲ ✲ a-zA-Z0-9_ ◗◗ s ❄ ❤ ❧ 1-9 0-9 ❄ ❧ ❧ ❤ \t\n ✲ ✲ \t\n

  15. COMP 520 Winter 2017 Scanning (15) Try it yourself: • Design a DFA matching binary strings divisible by 3. Use only 3 states. • Design a regular expression for floating point numbers of form: {1., 1.1, .1} (a digit on at least one side of the decimal) • Design a DFA for the language above language.

  16. COMP 520 Winter 2017 Scanning (16) Background (Scanner Table) from textbook, “Crafting a Compiler”

  17. COMP 520 Winter 2017 Scanning (17) Background (Scanner Algorithm) from textbook, “Crafting a Compiler”

  18. COMP 520 Winter 2017 Scanning (18) A non-deterministric finite automaton : • is a machine which recognizes regular languages; • for an input sequence of symbols, the automaton either accepts or rejects the string; • it works non-deterministically - that is given some input, there is potentially more than one path; • an NFA accepts a string if at least one path leads to an accept. Note: DFAs and NFAs are equally powerful.

  19. COMP 520 Winter 2017 Scanning (19) Regular Expressions to NFA (1) from textbook, “Crafting a Compiler”

  20. COMP 520 Winter 2017 Scanning (20) Regular Expressions to NFA (2) from textbook, ”Crafting a Compiler"

  21. COMP 520 Winter 2017 Scanning (21) Regular Expressions to NFA (3) from textbook, ”Crafting a Compiler"

  22. COMP 520 Winter 2017 Scanning (22) How to go from regular expressions to DFAs? 1. flex accepts a list of regular expressions (regex); 2. converts each regex internally to an NFA (Thompson construction); 3. converts each NFA to a DFA (subset construction) 4. may minimize DFA See “Crafting a Compiler", Chapter 3; or “Modern Compiler Implementation in Java", Chapter 2

  23. COMP 520 Winter 2017 Scanning (23) What you should know: 1. Understand the definition of a regular language, whether that be: prose, regular expression, DFA, or NFA. 2. Given the definition of a regular language, construct either a regular expression or an automaton. What you do not need to know: 1. Specific algorithms for converting between regular language definitions. 2. DFA minimization

  24. COMP 520 Winter 2017 Scanning (24) Let’s assume we have a collection of DFAs, one for each lex rule reg_expr1 -> DFA1 reg_expr2 -> DFA2 ... reg_rexpn -> DFAn How do we decide which regular expression should match the next characters to be scanned?

  25. COMP 520 Winter 2017 Scanning (25) Given DFAs D 1 , . . . , D n , ordered by the input rule order, the behaviour of a flex -generated scanner on an input string is: while input is not empty do s i := the longest prefix that D i accepts l := max {| s i |} if l > 0 then j := min { i : | s i | = l } remove s j from input perform the j th action else (error case) move one character from input to output end end • The longest initial substring match forms the next token, and it is subject to some action • The first rule to match breaks any ties • Non-matching characters are echoed back

  26. COMP 520 Winter 2017 Scanning (26) Why the “longest match” principle? Example: keywords ... import return tIMPORT; [a-zA-Z_][a-zA-Z0-9_]* return tIDENTIFIER; ... Given a string “importedFiles” , we want the token output of the scanner to be tIDENTIFIER(importedFiles) and not tIMPORT tIDENTIFIER(edFiles) Because we prefer longer matches, we get the right result.

  27. COMP 520 Winter 2017 Scanning (27) Why the “first match” principle? Example: keywords ... continue return tCONTINUE; [a-zA-Z_][a-zA-Z0-9_]* return tIDENTIFIER; ... Given a string “continue foo” , we want the token output of the scanner to be tCONTINUE tIDENTIFIER(foo) and not tIDENTIFIER(continue) tIDENTIFIER(foo) “First match” rule gives us the right answer: When both tCONTINUE and tIDENTIFIER match, prefer the first.

Recommend


More recommend