cse443 compilers
play

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - PowerPoint PPT Presentation

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http:/ /www.cse.buffalo.edu/faculty/alphonce/SP17 /CSE443/index.php https:/ /piazza.com/class/iybn4ndqa1s3ei Phases of a Syntactic compiler structure Figure 1.6,


  1. CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http:/ /www.cse.buffalo.edu/faculty/alphonce/SP17 /CSE443/index.php https:/ /piazza.com/class/iybn4ndqa1s3ei

  2. Phases of a Syntactic compiler structure Figure 1.6, page 5 of text

  3. Context Free Grammars and parsing O(n 3 ) algorithms to parse any CFG exist Programming language constructs can generally be parsed in O(n)

  4. Top-down & bottom-up A top-down parser builds a parse tree from root to the leaves easier to construct by hand A bottom-up parser builds a parse tree from leaves to root handle a larger class of grammars tools (yacc/bison) build bottom-up parsers

  5. look-ahead The current symbol being scanned in the input is called the lookahead symbol.

  6. Top-down parsing Start from grammar's start symbol Build parse tree so its yield matches input predictive parsing: a simple form of recursive descent parsing

  7. FIRST( 𝛽 ) If 𝛽∈ (NUT)* then FIRST( 𝛽 ) is "the set of terminals that appear as the first symbols of one or more strings of terminals generated from 𝛽 ." [p. 64] Ex: If A -> a 𝛾 then FIRST(A) = {a} Ex. If A -> a 𝛾 | B then FIRST(A) = {a} ∪ FIRST(B)

  8. FIRST( 𝛽 ) First sets are considered when there are two (or more) productions to expand A ∈ N: A -> 𝛽 | 𝛾 Predictive parsing requires that FIRST( 𝛽 ) ∩ FIRST( 𝛾 ) = ∅

  9. 𝜁 productions If lookahead symbol does not match first set, use 𝜁 production to not advance lookahead symbol but instead "discard" non-terminal: optexpt -> expr | 𝜁 "While parsing optexpr, if the lookahead symbol is not in FIRST(expr), then the 𝜁 production is used" [p. 66]

  10. Left recursion Grammars with left recursion are problematic for top-down parsers, as they lead to infinite regress.

  11. Left recursion example expr Grammar: expr + term expr -> expr + term | term term -> id expr + term FIRST sets for rule alternatives are not disjoint: term + term FIRST(expr) = id 𝛾 𝛽 𝛽 𝛽 FIRST(term) = id

  12. Rewriting grammar to remove left recursion expr rule is of form A -> A 𝛽 | 𝛾 Rewrite as two rules A -> 𝛾 R R -> 𝛽 R | 𝜁

  13. Back to example expr R term Grammar is re- written as R + term expr -> term R + term R R -> + term R | 𝜁 + term 𝜁 𝛾 𝛽 𝛽 𝛽

Recommend


More recommend