stmt → if (expr) stmt else stmt | while ( expr) stmt | { stmt_list }
Input G a grammar Output M a parse table
- for each production A→ α in G do
- for each terminal a ∈ FIRST(α) do
- M[A,a] ← M[A,a] + A → α
- if ε ∈ FIRST(α) then
- for each terminal b ∈ FOLLOW(A) do
- M[A,b] ← M[A,b] + A → α
- if ε ∈ FIRST(α) and $ ∈ FOLLOW(A) then
- M[A,$] ← M[A,$] + A → α
- if any M[A,a] is empty then
- M[A,a] → error
E | → | TE' |
E' | → | +TE' | ε |
T | → | FT' |
T' | → | *FT' | ε |
F | → | (E) | id |
Symbol | FIRST | FOLLOW | Symbol | FIRST | FOLLOW |
E | {(,id} | {),$} | ( | {(} | |
E' | {+,ε} | {),$} | ) | {)} | |
T | {(,id} | {+,),$} | id | {id} | |
T' | {*,ε} | {+,),$} | + | {+} | |
F | {(,id} | {+,*,),$} | * | {*} |
E → TE' For terminal in FIRST(T) {(,id} M[E,(] ← T→ TE' M[E,id] ← T→ TE' E' → +TE' For terminal in FIRST(+) {+} M[E',+] ← E' → +TE' E' → ε No terminals For each terminal in FOLLOW(E') {),$} M[E',)] ← E' → ε M[E',$] ← E' → ε
id | + | * | ( | ) | $ | |
---|---|---|---|---|---|---|
E | E → TE' | E→ TE' | ||||
E' | E' → +TE' | E' → ε | E' → ε | |||
T | T → FT' | T → FT' | ||||
T' | T' → ε | T' → *FT' | T'→ ε | T'→ ε | ||
F | F→ id | F → (E) |
- s.push($), s.push(S)
- a ← first (w)
- X ← s.top()
- while X ≠ $ do
- if X= a then
- s.pop()
- a ← next(w)
- else if X ∈ terminals
- error()
- else if M[X,a] = error
- error()
- else if M[X,a] = X ← Y1Y2 ... Yn
- Output X ← Y1Y2 ... Yn
- s.pop()
- for i ← n downto 1 do
- s.push(Yi)
- X ← s.pop()