Beruflich Dokumente
Kultur Dokumente
and Construction
Implementing a Parser
LL(1) and LALR Grammars
FBI Noon Dining Hall Vicki Anderson Recruiter
Announcements
HW due Friday
HW6 posted, due next Friday
LL(1)
1 token of look ahead
Do a left most derivation
Scan input from left to right
LALR(1)
one token of look-ahead
do a rightmost derivation in reverse
scan the input left-to-right
LA means "look-ahead
(nothing to do with the number of tokens)
LALR(1)
Parse Table:
S
EOF
(S)
[S]
Is Grammar LL(1)
Non-LL(1) Grammars
If a grammar is left-recursive
Left-Recursion
x * x
Recursive
x * x
Left recursive
x * x
Right recursive
A is a nonterminal
You Try it
Left Factored
Left Factoring
Change to:
A A
A 1 | 2
So for grammar
exp
( exp ) | ( )
It becomes
exp
exp
( exp
exp ) | )
You Try It
FIRST set
FIRST()
is some sequence of terminals and nonterminals
FIRST() is set of terminals that begin the
strings derivable from
if can derive , then is in FIRST()
FIRST ( ) t |
t is terminal and * t
t and *
FIRST(X)
{ INTLITERAL, ( }
term exp
{ /, }
- term exp |
{ INTLITERAL, ( }
factor term
{ -, }
/ factor term |
INTLITERAL | ( exp ) {INTLITERAL, ( }
1.
2.
3.
4.
=
=
=
=
)=
=
=
=
{ INTLITERAL, ( }
{-}
{ }
{ INTLITERAL, ( }
{/}
{ }
{ INTLITERAL }
{(}
A
A
FOLLOW(A) sets
Calculating FOLLOW(A)
You Try It
Parse Table
Current
Token
a
S
Non-terminals
A
X
R
Rule bodies
{ }
Parse Table
a
S
Bc
DB
Bc
DB
DB
B
D
EOF