Académique Documents
Professionnel Documents
Culture Documents
Semantic
Input Lexical Tokens
Parser
AST Analysis (Type
Program Analyzer Checking,)
Three Intermediate
Address Representation (IR)
Output Code Format Code
Program Generation Optimizations
LL(k) Parsers
Table driven
Generated using parser generators
For every LL(k) grammar there is a corresponding LL(k) parser
Left-factored grammar
ETX X+E|e
T ( E ) | int Y Y*T|e
The LL(1) parsing table: next input token
int * + ( ) $
E TX TX
X +E e e
T int Y (E)
Y *T e e e
rhs of production to use
leftmost non-terminal
Alex Aiken Lecture (Mods by Vijay Ganesh) 12
Using Parsing Tables
int * + ( ) $
E TX TX
X +E e e
T int Y (E)
Y *T e e e
int * + ( ) $
E TX TX
X +E e e
T int Y (E)
Y *T e e e
If a * t b
a can derive a t in the first position
We say that t First(a)
If A a and a * e and S * b A t d
Useful if stack has A, input is t, and A cannot derive t
In this case only option is to get rid of A (by deriving e)
Can work only if t can follow A in at least one derivation
We say t Follow(A)
Definition
First(X) = { t | X * ta} {e | X * e}
Algorithm sketch:
1. First(t) = { t }
2. e First(X)
if X e
if X A1 An and e First(Ai) for all 1 i n
3. First(a) First(X) if X A1 An a
and e First(Ai) for all 1 i n
First sets
First( ( ) = { ( } First( T ) = {int, ( }
First( ) ) = { ) } First( E ) = {int, ( }
First( int) = { int } First( X ) = {+, e }
First( + ) = { + } First( Y ) = {*, e }
First( * ) = { * }
Definition:
Follow(X) = { t | S * b X t d }
Intuition
If X A B then First(B) Follow(A) and
Follow(X) Follow(B)
if B * e then Follow(X) Follow(A)
Algorithm sketch:
1. $ Follow(S)
2. First(b) - {e} Follow(X)
For each production A a X b
3. Follow(A) Follow(X)
For each production A a X b where e First(b)
Follow sets
Follow( + ) = { int, ( } Follow( * ) = { int, ( }
Follow( ( ) = { int, ( } Follow( E ) = {), $}
Follow( X ) = {$, ) } Follow( T ) = {+, ) , $}
Follow( ) ) = {+, ) , $} Follow( Y ) = {+, ) , $}
Follow( int) = {*, +, ) , $}
First sets
First( ( ) = { ( } First( T ) = {int, ( }
First( ) ) = { ) } First( E ) = {int, ( }
First( int) = { int } First( X ) = {+, e }
First( + ) = { + } First( Y ) = {*, e }
First( * ) = { * }
Follow sets
Follow( + ) = { int, ( } Follow( * ) = { int, ( }
Follow( ( ) = { int, ( } Follow( E ) = {), $}
Follow( X ) = {$, ) } Follow( T ) = {+, ) , $}
Follow( ) ) = {+, ) , $} Follow( Y ) = {+, ) , $}
Follow( int) = {*, +, ) , $}
Left-factored grammar
ETX X+E|e
T ( E ) | int Y Y*T|e
The LL(1) parsing table: next input token
int * + ( ) $
E TX TX
X +E e e
T int Y (E)
Y *T e e e
rhs of production to use
leftmost non-terminal
Alex Aiken Lecture (Mods by Vijay Ganesh) 31
Notes on LL(1) Parsing Tables