CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall
Phases of a Syntactic compiler structure Figure 1.6, page 5 of text
Continuing from last time…
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 i-1 ⇒ * 𝜁 ) if 𝜁 ∈ FIRST(Y j ) ∀ j <= k add 𝜁 to FIRST(X) if X -> 𝜁 ∈ P, then add 𝜁 to FIRST(X)
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)
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,$]
Example G given by its productions: E -> T E' E' -> + T E' | 𝜁 T -> F T' For each production A -> 𝛽 of G: T' -> * F T' | 𝜁 For each terminal a ∈ FIRST( 𝛽 ), add F -> ( E ) | id A -> 𝛽 to M[A,a] If 𝜁 ∈ FIRST( 𝛽 ), then for each terminal b in FOLLOW(A), add A -> 𝛽 to M[A,b] If 𝜁 ∈ FIRST( 𝛽 ) and $ ∈ FOLLOW(A), add A -> 𝛽 to M[A,$]
FIRST SETS E -> T E' E' -> + T E' | 𝜁 T -> F T' T' -> * F T' | 𝜁 F -> ( E ) | id FIRST(F) = { ( , id } FIRST(T) = FIRST(F) = { ( , id } FIRST(E) = FIRST(T) = FIRST(F) = { ( , id } FIRST(E') = { + , 𝜁 } FIRST(T') = { * , 𝜁 } 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 -> 𝛽 to Y 2 … Y k ⇒ * 𝜁 ) M[A,b] If 𝜁 ∈ FIRST( 𝛽 ) and $ ∈ FOLLOW(A), if 𝜁 ∈ FIRST(Y j ) ∀ j < k add 𝜁 to FIRST(X) add A -> 𝛽 to M[A,$]
FOLLOW SETS E -> T E' E' -> + T E' | 𝜁 T -> F T' T' -> * F T' | 𝜁 F -> ( E ) | id FOLLOW(E) = { ) , $ } FOLLOW(E') = FOLLOW(E) = { ) , $ } FOLLOW(T) = { + , ) , $ } FOLLOW(T') = FOLLOW(T) = { + , ) , $ } FOLLOW(F) = { + , * , ) , $ } For each production A -> 𝛽 of G: Place $ in FOLLOW(S), where S is the start For each terminal a ∈ FIRST( 𝛽 ), add symbol ($ is an end marker) A -> 𝛽 to M[A,a] if A -> 𝛽 B 𝛾 ∈ P, then FIRST( 𝛾 ) - { 𝜁 } is in If 𝜁 ∈ FIRST( 𝛽 ), then for each FOLLOW(B) terminal b in FOLLOW(A), add A -> 𝛽 to if A -> 𝛽 B ∈ P or A -> 𝛽 B 𝛾 ∈ P where 𝜁 ∈ FIRST( 𝛾 ), M[A,b] then everything in FOLLOW(A) is in FOLLOW(B) If 𝜁 ∈ FIRST( 𝛽 ) and $ ∈ FOLLOW(A), add A -> 𝛽 to M[A,$]
Recommend
More recommend