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

cse443 compilers
SMART_READER_LITE
LIVE PREVIEW

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,


slide-1
SLIDE 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

slide-2
SLIDE 2

Phases of a compiler

Figure 1.6, page 5 of text

Syntactic structure

slide-3
SLIDE 3

Context Free Grammars and parsing

O(n3) algorithms to parse any CFG exist Programming language constructs can generally be parsed in O(n)

slide-4
SLIDE 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

slide-5
SLIDE 5

look-ahead

The current symbol being scanned in the input is called the lookahead symbol.

slide-6
SLIDE 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

slide-7
SLIDE 7

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)

FIRST(𝛽)

slide-8
SLIDE 8

FIRST(𝛽)

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

slide-9
SLIDE 9

𝜁 productions

If lookahead symbol does not match first set, use 𝜁 production to not advance lookahead symbol but instead "discard" non-terminal:

  • ptexpt -> expr | 𝜁

"While parsing optexpr, if the lookahead symbol is not in FIRST(expr), then the 𝜁 production is used" [p. 66]

slide-10
SLIDE 10

Left recursion

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

slide-11
SLIDE 11

Left recursion example

Grammar: expr -> expr + term | term term -> id FIRST sets for rule alternatives are not disjoint: FIRST(expr) = id FIRST(term) = id

expr + term expr + term expr + term term

𝛾 𝛽 𝛽 𝛽

slide-12
SLIDE 12

Rewriting grammar to remove left recursion

expr rule is of form A -> A 𝛽 | 𝛾 Rewrite as two rules A -> 𝛾 R R -> 𝛽 R | 𝜁

slide-13
SLIDE 13

Back to example

Grammar is re- written as expr -> term R R -> + term R | 𝜁

expr R + term term

𝛾 𝛽 𝛽 𝛽

R + term R + term 𝜁