Parsing 89
Example. Consider the following grammar.
S ::= Aa | b
A ::= Sc | d
In step 1, we can enumerate the non-terminals using subscripts to record the numbering:
S
1
and A
2
. This gives us a new set of rules:
S
1
::= A
2
a | b
A
2
::= S
1
c | d
In the first iteration of step 2 (i = 1), no rules apply. In the second iteration (i = 1, j = 2),
the rule
A
2
::= S
1
c
applies. We replace it with two rules, expanding S
1
, to yield
S
1
::= A
2
a | b
A
2
::= A
2
ac | bc | d
We then use the transformation (3.26) to produce the grammar
S
1
::= A
2
a | b
A
2
::= bcA’
2
| dA’
2
A’
2
::= acA’
2
A’
2
::=
Or, removing the subscripts,
S ::= Aa | b
A ::= bcA
0
| dA
0
A
0
::= acA
0
A
0
::=
Left factoring
Another common property of grammars that violates the LL(1) property is when two
or more rules defining a non-terminal ...