E: E + T | T E : T E'
T: T * F | F ==> E': + T E' | ε
F: (E) | int T : F T'
T': * F T' | ε
F: (E) | int
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
val tok = ref (getToken())
fun advance() = tok := getToken()
fun eat(t) = if t = !tok then tok := advance() else error()
fun E() = (T(); E'())
and E'() = case !tok (* lookahead token *)
of PLUS => (eat(PLUS); T(); E'())
| ? => () (* do nothing *)
and T() = (F(); T'())
and T'() = case !tok (* lookahead token *)
of TIMES => (eat(TIMES); F(); T'())
| ? => () (* do nothing *)
and F() = case !tok (* lookahead token *)
of INT => eat(INT)
| LPAREN => (eat(LPAREN); E(); eat(RPAREN))
(int + int) * int
fun E() = (T(); E'())
and E'() = (eat(PLUS); T(); E'())
| () (* do nothing *)
and T() = (F(); T'())
and T'() = (eat(TIMES); F(); T'())
| () (* do nothing *)
and F() = eat(INT)
| (eat(LPAREN); E(); eat(RPAREN))
(int + int) * int
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
val tok = ref (getToken())
fun advance() = tok := getToken()
fun eat(t) = if t = !tok then tok := advance() else error()
fun E() = (T(); E'())
and E'() = case !tok (* lookahead token *)
of PLUS => (eat(PLUS); T(); E'())
| ? => () (* do nothing *)
and T() = (F(); T'())
and T'() = case !tok (* lookahead token *)
of TIMES => (eat(TIMES); F(); T'())
| ? => () (* do nothing *)
and F() = case !tok (* lookahead token *)
of INT => eat(INT)
| LPAREN => (eat(LPAREN); E(); eat(RPAREN))
(int +* int) * int
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
-------------------------------------------------
| int | + | * | ( | ) | $ |
-------------------------------------------------
E | T E' | | | T E' | | |
E' | | + T E' | | | ε | ε |
T | F T' | | | F T' | | |
T' | | ε | * F T'| | ε | ε |
F | int | | | (E) | | |
-------------------------------------------------
(int +* int) * int
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
------------------------------------------------- -------------------------------------------------
| int | + | * | ( | ) | $ | | int | + | * | ( | ) | $ |
------------------------------------------------- -------------------------------------------------
E | T E' | | | T E' | | | E | | | | | | |
E' | | + T E' | | | ε | ε | E' | | | | | | |
T | F T' | | | F T' | | | T | | | | | | |
T' | | ε | * F T'| | ε | ε | T' | | | | | | |
F | int | | | (E) | | | F | | | | | | |
------------------------------------------------- -------------------------------------------------
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
------------------------------------------------- -------------------------------------------------
| int | + | * | ( | ) | $ | | int | + | * | ( | ) | $ |
------------------------------------------------- -------------------------------------------------
E | T E' | | | T E' | | | E | | | | | | |
E' | | + T E' | | | ε | ε | E' | | | | | | |
T | F T' | | | F T' | | | T | | | | | | |
T' | | ε | * F T'| | ε | ε | T' | | | | | | |
F | int | | | (E) | | | F | | | | | | |
------------------------------------------------- -------------------------------------------------
T E'
start with?T E'
)E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
T E'
) = FIRST(T
) = FIRST(F T'
) = FIRST(F
) = {int, (}E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
------------------------------------------------- -------------------------------------------------
| int | + | * | ( | ) | $ | | int | + | * | ( | ) | $ |
------------------------------------------------- -------------------------------------------------
E | T E' | | | T E' | | | E | T E' | | | T E' | | |
E' | | + T E' | | | ε | ε | E' | | | | | | |
T | F T' | | | F T' | | | T | | | | | | |
T' | | ε | * F T'| | ε | ε | T' | | | | | | |
F | int | | | (E) | | | F | | | | | | |
------------------------------------------------- -------------------------------------------------
T E'
) = FIRST(T
) = FIRST(F T'
) = FIRST(F
) = {int, (}FIRST(
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
------------------------------------------------- -------------------------------------------------
| int | + | * | ( | ) | $ | | int | + | * | ( | ) | $ |
------------------------------------------------- -------------------------------------------------
E | T E' | | | T E' | | | E | T E' | | | T E' | | |
E' | | + T E' | | | ε | ε | E' | | + T E' | | | | |
T | F T' | | | F T' | | | T | F T' | | | F T' | | |
T' | | ε | * F T'| | ε | ε | T' | | | * F T'| | | |
F | int | | | (E) | | | F | int | | | (E) | | |
------------------------------------------------- -------------------------------------------------
+ T E'
) = {+}F T'
) = FIRST(F
) = {int, (}* F T'
) = {*}int
) = {int}, FIRST((E)
) = {(}E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
Consider: int + int
↑
E => T E' => F T' E' => int T' E'
E
/ \
T E'
/ \
F T'
|
int
T'
, which production should be used?E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
Consider: int + int
↑
E => T E' => F T' E' => int T' E'
E
/ \
T E'
/ \
F T'
|
int
T'
, which production should be used? εE : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
Consider: int + int
↑
E => T E' => F T' E' => int T' E'
E
/ \
T E'
/ \
F T'
|
int
T'
, which production should be used? εT'
is followed by +
.E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
------------------------------------------------- -------------------------------------------------
| int | + | * | ( | ) | $ | | int | + | * | ( | ) | $ |
------------------------------------------------- -------------------------------------------------
E | T E' | | | T E' | | | E | T E' | | | T E' | | |
E' | | + T E' | | | ε | ε | E' | | + T E' | | | | |
T | F T' | | | F T' | | | T | F T' | | | F T' | | |
T' | | ε | * F T'| | ε | ε | T' | | ε | * F T'| | | |
F | int | | | (E) | | | F | int | | | (E) | | |
------------------------------------------------- -------------------------------------------------
T'
is followed by +
; in other words, +
∈ FOLLOW(T'
)E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
------------------------------------------------- -------------------------------------------------
| int | + | * | ( | ) | $ | | int | + | * | ( | ) | $ |
------------------------------------------------- -------------------------------------------------
E | T E' | | | T E' | | | E | T E' | | | T E' | | |
E' | | + T E' | | | ε | ε | E' | | + T E' | | | ε | ε |
T | F T' | | | F T' | | | T | F T' | | | F T' | | |
T' | | ε | * F T'| | ε | ε | T' | | ε | * F T'| | ε | ε |
F | int | | | (E) | | | F | int | | | (E) | | |
------------------------------------------------- -------------------------------------------------
E'
) = {), $}T'
) = {+, ), $}E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
---------------------------------
| FIRST | FOLLOW
---------------------------------
E | |
E' | |
T | |
T' | |
F | |
---------------------------------
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
---------------------------------
| FIRST | FOLLOW
---------------------------------
E | |
E' | {+, ε} |
T | |
T' | {*, ε} |
F | {int, (} |
---------------------------------
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
---------------------------------
| FIRST | FOLLOW
---------------------------------
E | |
E' | {+, ε} |
T | |
T' | {*, ε} |
F | {int, (} |
---------------------------------
Suppose
FIRST(F
) T
) E
)
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
---------------------------------
| FIRST | FOLLOW
---------------------------------
E | {int, (} |
E' | {+, ε} |
T | |
T' | {*, ε} |
F | {int, (} |
---------------------------------
Suppose
FIRST(F
) T
) E
)
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
---------------------------------
| FIRST | FOLLOW
---------------------------------
E | {int, (} |
E' | {+, ε} |
T | {int, (} |
T' | {*, ε} |
F | {int, (} |
---------------------------------
Suppose
FIRST(F
) T
)
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
---------------------------------
| FIRST | FOLLOW
---------------------------------
E | {int, (} | {$}
E' | {+, ε} |
T | {int, (} |
T' | {*, ε} |
F | {int, (} |
---------------------------------
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
--------------------------------- ---------------------------------
| FIRST | FOLLOW | FIRST | FOLLOW
--------------------------------- ---------------------------------
E | {int, (} | {$} E | {int, (} | {$, )}
E' | {+, ε} | E' | {+, ε} |
T | {int, (} | {+} => T | {int, (} | {+}
T' | {*, ε} | T' | {*, ε} |
F | {int, (} | {*} F | {int, (} | {*}
--------------------------------- ---------------------------------
E'
) \ {ε} T
)T'
) \ {ε} F
))
) \ {ε} E
)E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
--------------------------------- ---------------------------------
| FIRST | FOLLOW | FIRST | FOLLOW
--------------------------------- ---------------------------------
E | {int, (} | {$, )} E | {int, (} | {$, )}
E' | {+, ε} | E' | {+, ε} | {$, )}
T | {int, (} | {+} => T | {int, (} | {+}
T' | {*, ε} | T' | {*, ε} | {+}
F | {int, (} | {*} F | {int, (} | {*}
--------------------------------- ---------------------------------
E
) E'
)T
) T'
)E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
--------------------------------- ---------------------------------
| FIRST | FOLLOW | FIRST | FOLLOW
--------------------------------- ---------------------------------
E | {int, (} | {$, )} E | {int, (} | {$, )}
E' | {+, ε} | {$, )} E' | {+, ε} | {$, )}
T | {int, (} | {+} => T | {int, (} | {+, $, )}
T' | {*, ε} | {+} T' | {*, ε} | {+}
F | {int, (} | {*} F | {int, (} | {*, $, )}
--------------------------------- ---------------------------------
E
) T
)T
) F
)E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
--------------------------------- ---------------------------------
| FIRST | FOLLOW | FIRST | FOLLOW
--------------------------------- ---------------------------------
E | {int, (} | {$, )} E | {int, (} | {$, )}
E' | {+, ε} | {$, )} E' | {+, ε} | {$, )}
T | {int, (} | {+, $, )} => T | {int, (} | {+, $, )}
T' | {*, ε} | {+} T' | {*, ε} | {+, $, )}
F | {int, (} | {*, $, )} F | {int, (} | {*, $, )}
--------------------------------- ---------------------------------
E
) E'
)T
) T'
) CFG
↓
|-------------|
| Parser |
| Synthesizer |
|-------------|
|
synthesizes
↓
|--------|
program → | Parser | → parse tree
|--------|
Theory: For any context-free grammar
Input
□□□□□□□□
↓
|---------| Stack
| Control | ⇔ |__|
| Unit | |__|
|---------| |__|
This does not work. Why?
NPDA -X-> DPDA
E : T E'
E': + T E' | ε
T : F T'
T': * F T' | ε
F: int | (E)
-------------------------------------------------
| int | + | * | ( | ) | $ |
-------------------------------------------------
E | T E' | | | T E' | | |
E' | | + T E' | | | ε | ε |
T | F T' | | | F T' | | |
T' | | ε | * F T'| | ε | ε |
F | int | | | (E) | | |
-------------------------------------------------
Parse `int + int`
(input, stack):
(int, E$) => (int, T E'$) => (int, F T' E'$) => (int, int T' E'$) => (+, T' E'$) => (+, E'$) => (+, + T E'$) => (int, T E'$)
=> (int, F T' E'$) => (int, int T' E'$) => ($, T' E'$) => ($, E'$) => ($, $) => accept
Input
□□□□□□□□
↓
|---------| Stack
| Control | ⇔ |__|
| Unit | |__|
|---------| |__|
-------------------------------------------------
| int | + | * | ( | ) | $ |
-------------------------------------------------
E | T E' | | | T E' | | |
E' | | + T E' | | | ε | ε |
T | F T' | | | F T' | | |
T' | | ε | * F T'| | ε | ε |
F | int | | | (E) | | |
-------------------------------------------------
Parse `int + int`
(input, stack):
(int, E$) => (int, T E'$) => (int, F T' E'$) => (int, int T' E'$) => (+, T' E'$) => (+, E'$) => (+, + T E'$) => (int, T E'$)
=> (int, F T' E'$) => (int, int T' E'$) => ($, T' E'$) => ($, E'$) => ($, $) => accept