In his original article on parsing LR , Knut gives the following grammar for this language, which, in his opinion, "is the shortest possible unique grammar for this language:"
S → & Epsilon; | aAbS | bBaS
A → & Epsilon; | Aaba
B → & Epsilon; | bBaB
Intuitively, it tries to break any string of As and Bs into blocks that are fully balanced. Some blocks start with a and end with b, while others start with b and end with.
We can calculate FIRST and FOLLOW as follows:
FIRST (S) = {& epsilon ;, a, b}
FIRST (A) = {& epsilon ;, a}
FIRST (B) = {& epsilon ;, b}
FOLLOW (S) = {$}
NEXT (A) = {b}
NEXT (B) = {a}
Based on this, we obtain the following LL (1) analysis table:
| a | b | $ --+-------+-------+------- S | aAbS | bBaS | e A | aAbA | e | B | e | bBaB |
And therefore this grammar is not only LR (1), but also LL (1).
Hope this helps!
templatetypedef Mar 02 '13 at 1:44 2013-03-02 01:44
source share