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 Phases of a Syntactic compiler structure Figure 1.6, page 5 of text FIRST(X) if X T then FIRST(X) = { X } if X N and X -> Y 1 Y 2 Y k P for k


  1. CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall

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

  3. FIRST(X) if X ∈ T then FIRST(X) = { X } if X ∈ N and X -> Y 1 Y 2 … Y k ∈ P for k ≥ 1, then add a ∈ T to FIRST(X) if ∃ i s.t. a ∈ FIRST(Y i ) and 𝜁 ∈ FIRST(Y j ) ∀ j < i (i.e. Y 1 Y 2 … Y k ⇒ * 𝜁 ) if 𝜁 ∈ FIRST(Y j ) ∀ j < k add 𝜁 to FIRST(X)

  4. E -> T E' FIRST SETS E' -> + T E' | 𝜁 FIRST(F) = { ( , id } T -> F T' F -> ( E ) here's where the parenthesis comes from T' -> * F T' | 𝜁 F -> id here's where id comes from F -> ( E ) | id FIRST(T) = FIRST(F) = { ( , id } T -> F T' (T' not relevant since 𝜁 not in FIRST(F)) FIRST(E) = FIRST(T) = FIRST(F) = { ( , id } E -> T E' (E' not relevant since 𝜁 not in FIRST(T)) FIRST(E') = { + , 𝜁 } E' -> + T E' here's where + comes from E' -> 𝜁 here's where 𝜁 comes from FIRST(T') = { * , 𝜁 } T' -> * F T' here's where * comes from T' -> 𝜁 here's where 𝜁 comes from For each production A -> 𝛽 of G: if X ∈ T then FIRST(X) = { X } if X ∈ N and X -> Y 1 Y 2 … Y k ∈ P for k ≥ 1, then For each terminal a ∈ FIRST( 𝛽 ), add A -> 𝛽 to M[A,a] add a ∈ T to FIRST(X) if ∃ i s.t. a ∈ If 𝜁 ∈ FIRST( 𝛽 ), then for each FIRST(Y i ) and 𝜁 ∈ FIRST(Y j ) ∀ j < i (i.e. Y 1 terminal b in FOLLOW(A), add A - Y 2 … Y k ⇒ * 𝜁 ) > 𝛽 to M[A,b] If 𝜁 ∈ FIRST( 𝛽 ) and $ ∈ if 𝜁 ∈ FIRST(Y j ) ∀ j < k add 𝜁 to FIRST(X) FOLLOW(A), add A -> 𝛽 to M[A,$]

  5. FOLLOW(X) Place $ in FOLLOW(S), where S is the start symbol ($ is an end marker) if A -> 𝛽 B 𝛾 ∈ P, then FIRST( 𝛾 ) - { 𝜁 } is in FOLLOW(B) if A -> 𝛽 B ∈ P or A -> 𝛽 B 𝛾 ∈ P where 𝜁 ∈ FIRST( 𝛾 ), then everything in FOLLOW(A) is in FOLLOW(B)

  6. E -> T E' FOLLOW SETS E' -> + T E' | 𝜁 FOLLOW(E) = { ) , $ } T -> F T' E is our start symbol, so by rule 1 we add $ to FOLLOW(E). T' -> * F T' | 𝜁 E appears in right hand side (RHS) of one production: F -> ( E ) By rule 2, we add ) to FOLLOW(E) since ) follows E in this production. F -> ( E ) | id FOLLOW(E') = FOLLOW(E) = { ) , $ } E' appears in RHS of two productions: E -> T E' and E' -> + T E' By rule 3, we add everything from FOLLOW(E) = { ) , $ } to FOLLOW(E'). FOLLOW(T) = { + , ) , $ } T appears in RHS of two productions: E -> T E' and E' -> + T E' By rule 2, we add everything from FIRST(E') = { + , 𝜁 } - { 𝜁 } = { + } to FOLLOW(T). By rule 3, since 𝜁 ∈ FIRST(E'), add everything from FOLLOW(E) = FOLLOW(E') ={ ) , $ } to FOLLOW(T). FOLLOW(T') = FOLLOW(T) = { + , ) , $ } T' appears in RHS of two productions: T -> F T' and T' -> * F T' By rule 3, we add everything from FOLLOW(T) = { + , ) , $ } to FOLLOW(T'). FOLLOW(F) = { + , * , ) , $ } F appears in RHS of two productions: T -> F T' and T' -> * F T' By rule 2, we add everything from FIRST(T') = { * , 𝜁 } - { 𝜁 } = { * } to FOLLOW(F). By rule 3, since 𝜁 ∈ FIRST(T'), add everything from FOLLOW(T) = FOLLOW(T') ={ + , ) , $ } to FOLLOW(F). For each production A -> 𝛽 of G: 1. Place $ in FOLLOW(S), where S is the start symbol For each terminal a ∈ FIRST( 𝛽 ), ($ is an end marker) add A -> 𝛽 to M[A,a] 2. if A -> 𝛽 B 𝛾 ∈ P, then FIRST( 𝛾 ) - { 𝜁 } is in If 𝜁 ∈ FIRST( 𝛽 ), then for each FOLLOW(B) terminal b in FOLLOW(A), add A - 3. if A -> 𝛽 B ∈ P or A -> 𝛽 B 𝛾 ∈ P where 𝜁 ∈ FIRST( 𝛾 ), > 𝛽 to M[A,b] then everything in FOLLOW(A) is in FOLLOW(B) If 𝜁 ∈ FIRST( 𝛽 ) and $ ∈ FOLLOW(A), add A -> 𝛽 to M[A,$]

  7. Table-driven predictive parsing Algorithm 4.32 (p. 224) INPUT: Grammar G = (N,T,P,S) OUTPUT: Parsing table M For each production A -> 𝛽 of G: 1. For each terminal a ∈ FIRST( 𝛽 ), add A -> 𝛽 to M[A,a] 2. If 𝜁 ∈ FIRST( 𝛽 ), then for each terminal b in FOLLOW(A), add A -> 𝛽 to M[A,b] 3. If 𝜁 ∈ FIRST( 𝛽 ) and $ ∈ FOLLOW(A), add A -> 𝛽 to M[A,$]

  8. Parse-table M NON id + * ( ) $ TERMINALS E E -> T E' E -> T E' E' E' -> 𝜁 E' -> 𝜁 E' -> + T E' T T -> F T' T -> F T' T' T' -> 𝜁 T' -> 𝜁 T' -> 𝜁 T' -> * F T' F F -> id F -> ( E ) For each production A -> 𝛽 of G: FIRST(E) = FIRST(T) = FIRST(F) = { ( , id } FIRST(E') = { + , 𝜁 } For each terminal a ∈ FIRST( 𝛽 ), add FIRST(T') = { * , 𝜁 } E -> T E' A -> 𝛽 to M[A,a] If 𝜁 ∈ FIRST( 𝛽 ), then for each E' -> + T E' | 𝜁 terminal b in FOLLOW(A), add A -> 𝛽 to T -> F T' FOLLOW(E') = FOLLOW(E) = { ) , $ } M[A,b] T' -> * F T' | 𝜁 FOLLOW(T') = FOLLOW(T) = { + , ) , $ } If 𝜁 ∈ FIRST( 𝛽 ) and $ ∈ FOLLOW(A), F -> ( E ) | id FOLLOW(F) = { + , * , ) , $ } add A -> 𝛽 to M[A,$]

  9. 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 input $ 𝜕 stack S M parser $ output

  10. 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 -> Y 1 Y 2 … Y k ) { output X -> Y 1 Y 2 … Y k pop the stack push Y k … Y 2 Y 1 onto the stack } Let X be the top stack symbol } Accept if a == X == $

  11. INPUT (a) STACK (X) OUTPUT id + id * id $ E $ E -> T E' if (X == a) { pop, advance a in 𝜕 } id + * ( ) $ else if (X is a terminal) { error } E E -> T E' E -> T E' else if (M[X,a] is blank) { error } else if (M[X,a] is X -> Y 1 Y 2 … Y k ) { E' E' -> 𝜁 E' -> 𝜁 E' -> + T E' output X -> Y 1 Y 2 … Y k T T -> F T' T -> F T' pop push Y k … Y 2 Y 1 T' T' -> 𝜁 T' -> 𝜁 T' -> 𝜁 T' -> * F T' } Let X be the top stack symbol F F -> id F -> ( E )

  12. INPUT (a) STACK (X) OUTPUT id + id * id $ E $ E -> T E' id + id * id $ T E' $ T -> F T' if (X == a) { pop, advance a in 𝜕 } id + * ( ) $ else if (X is a terminal) { error } E E -> T E' E -> T E' else if (M[X,a] is blank) { error } else if (M[X,a] is X -> Y 1 Y 2 … Y k ) { E' E' -> 𝜁 E' -> 𝜁 E' -> + T E' output X -> Y 1 Y 2 … Y k T T -> F T' T -> F T' pop push Y k … Y 2 Y 1 T' T' -> 𝜁 T' -> 𝜁 T' -> 𝜁 T' -> * F T' } Let X be the top stack symbol F F -> id F -> ( E )

  13. 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 if (X == a) { pop, advance a in 𝜕 } id + * ( ) $ else if (X is a terminal) { error } E E -> T E' E -> T E' else if (M[X,a] is blank) { error } else if (M[X,a] is X -> Y 1 Y 2 … Y k ) { E' E' -> 𝜁 E' -> 𝜁 E' -> + T E' output X -> Y 1 Y 2 … Y k T T -> F T' T -> F T' pop push Y k … Y 2 Y 1 T' T' -> 𝜁 T' -> 𝜁 T' -> 𝜁 T' -> * F T' } Let X be the top stack symbol F F -> id F -> ( E )

  14. 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' $ if (X == a) { pop, advance a in 𝜕 } id + * ( ) $ else if (X is a terminal) { error } E E -> T E' E -> T E' else if (M[X,a] is blank) { error } else if (M[X,a] is X -> Y 1 Y 2 … Y k ) { E' E' -> 𝜁 E' -> 𝜁 E' -> + T E' output X -> Y 1 Y 2 … Y k T T -> F T' T -> F T' pop push Y k … Y 2 Y 1 T' T' -> 𝜁 T' -> 𝜁 T' -> 𝜁 T' -> * F T' } Let X be the top stack symbol F F -> id F -> ( E )

  15. 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' -> 𝜁 if (X == a) { pop, advance a in 𝜕 } id + * ( ) $ else if (X is a terminal) { error } E E -> T E' E -> T E' else if (M[X,a] is blank) { error } else if (M[X,a] is X -> Y 1 Y 2 … Y k ) { E' E' -> 𝜁 E' -> 𝜁 E' -> + T E' output X -> Y 1 Y 2 … Y k T T -> F T' T -> F T' pop push Y k … Y 2 Y 1 T' T' -> 𝜁 T' -> 𝜁 T' -> 𝜁 T' -> * F T' } Let X be the top stack symbol F F -> id F -> ( E )

  16. 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' if (X == a) { pop, advance a in 𝜕 } id + * ( ) $ else if (X is a terminal) { error } E E -> T E' E -> T E' else if (M[X,a] is blank) { error } else if (M[X,a] is X -> Y 1 Y 2 … Y k ) { E' E' -> 𝜁 E' -> 𝜁 E' -> + T E' output X -> Y 1 Y 2 … Y k T T -> F T' T -> F T' pop push Y k … Y 2 Y 1 T' T' -> 𝜁 T' -> 𝜁 T' -> 𝜁 T' -> * F T' } Let X be the top stack symbol F F -> id F -> ( E )

Recommend


More recommend