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

Table-driven predictive parsing

Here we trade the implicit stack that the recursing parsing algorithm uses for an explicit stack.

slide-4
SLIDE 4

Recall the parse-table M

NON TERMINALS

id + * ( ) $ E

E -> T E' E -> T E'

E'

E' -> + T E' E' -> 𝜁 E' -> 𝜁

T

T -> F T' T -> F T'

T'

T' -> 𝜁 T' -> * F T T' -> 𝜁 T' -> 𝜁

F

F -> id F -> ( E )

slide-5
SLIDE 5

Algorithm 4.34 [p. 226]

INPUT: A string 𝜕 and a parsing table M for a grammar G=(N,T,P,S). OUTPUT: If 𝜕∈𝓜(G), a leftmost derivation of 𝜕, error otherwise

$ S $ 𝜕

stack input

  • utput

parser M

slide-6
SLIDE 6

Algorithm 4.34 [p. 226]

Let a be the first symbol of 𝜕 Let X be the top stack symbol while (X ≠ $) { if (x == a) { pop the stack, advance a in 𝜕 } else if (X is a terminal) { error } else if (M[X,a] is blank) { error } else if (M[X,a] is X -> Y1 Y2 … Yk) {

  • utput X -> Y1 Y2 … Yk

pop the stack push Yk … Y2 Y1 onto the stack } Let X be the top stack symbol } Accept if a == X == $

slide-7
SLIDE 7

INPUT (a) STACK (X) OUTPUT id + id * id $ E $ E -> T E' id + id * id $ T E' $ T -> F T' id + id * id $ F T' E' $ F -> id id + id * id $ id T' E' $ id + id * id $ T' E' $ T' -> 𝜁 id + id * id $ E' $ E' -> + T E' id + id * id $ + T E' $ id + id * id $ T E' $ T -> F T' id + id * id $ F T' E' $ F -> id id + id * id $ id T' E' $ id + id * id $ T' E' $ T' -> * F T' id + id * id $ * F T' E' $ id + id * id $ F T' E' $ F -> id id + id * id $ id T' E' $ id + id * id $ T' E' $ T' -> 𝜁 id + id * id $ E' $ E' -> 𝜁 id + id * id $ $ Since both a and X are $, accept