Professional Documents
Culture Documents
Lec7 PDF
Lec7 PDF
Observation:
Our recursive descent parser encodes state
information in its run-time stack, or call
stack.
Using recursive procedure calls to implement a
stack abstraction may not be particularly ecient.
stack
6
?
source - scanner - parser - il
code 6
?
parsing
tables
optional
stack
6
?
table-
source - scanner - driven - il
code parser
6
?
parser parsing
grammar - -
generator tables
X Stack[tos]
repeat
if X is a terminal or eof then
if X = token then
pop X
token next token()
else error()
else /* X is a non-terminal */
if M [X,token] = X ! Y1Y2 Y k then
pop X
push Y ; Y ?1; ; Y1
k k
else error()
X Stack[tos]
until X = eof
htermi t ! ft t ! ft
0
{0
{ { { {
hterm i
0
{ { t ! t ! t ! *t t ! /t t !
0 0 0 0 0
hfactori f ! id f ! num { { { { {
FIRST(X )
4. if X is a non-terminal and X ::= Y1Y2 Y , k
1 < i)
rule 1 2 3 4 FIRST
To build FOLLOW(X ):
1. place eof in FOLLOW(hgoali)
2. if A ::= B , then put fFIRST( ) ? g in
FOLLOW(B )
3. if A ::= B then put FOLLOW(A) in FOLLOW(B )
4. if A ::= B and 2 FIRST( ), then put
FOLLOW(A) in FOLLOW(B )
rule 1 2 3 4 FOLLOW
Method
1. 8 production A ::= , perform steps 2{4
2. 8 terminal a in FIRST(), add A ::= to M [A; a]
3. if 2 FIRST(), add A ::= to M [A; b]
8 terminal b 2 FOLLOW(A)
4. if 2 FIRST() and eof 2 FOLLOW(A), add
A ::= to M [A; eof]
5. set each undened entry of M to error
If this fails, the grammar is not LL(1)
htermi t ! ft t ! ft
0
{ 0
{ { { {
hterm i
0
{ { t ! t ! t ! *t t ! /t t !
0 0 0 0 0
hfactori f ! id f ! num { { { { {
else error()
until X = eof
pairwise disjoint
2. if ) , then FIRST( ) T FOLLOW(A) = ;, for
i j
all 1 j n;i 6 =j .
S ::= aS 0
S 0 ::= aS 0 j
accepts the same language and is LL(1)