The Grammar
Let's expand your EBNF grammar to simple BNF and assume, that b
is a terminal and <e>
is an empty string:
A -> X
X -> BX
X -> <e>
B -> b
This grammar produces strings of terminal b
's of any length.
The LL(1) Table
To construct the table, we will need to generate the first and follow sets (constructing an LL(1) parsing table).
First sets
First(α)
is the set of terminals that begin strings derived from any string of grammar symbols α
.
First(A) : b, <e>
First(X) : b, <e>
First(B) : b
Follow sets
Follow(A)
is the set of terminals a that can
appear immediately to the right of a nonterminal A
.
Follow(A) : $
Follow(X) : $
Follow(B) : b$
Table
We can now construct the table based on the sets, $
is the end of input marker.
+---+---------+----------+
| | b | $ |
+---+---------+----------+
| A | A -> X | A -> X |
| X | X -> BX | X -> <e> |
| B | B -> b | |
+---+---------+----------+
The parser action always depends on the top of the parse stack and the next input symbol.
- Terminal on top of the parse stack:
- Matches the input symbol: pop stack, advance to the next input symbol
- No match: parse error
- Nonterminal on top of the parse stack:
- Parse table contains production: apply production to stack
- Cell is empty: parse error
$
on top of the parse stack:
$
is the input symbol: accept input
$
is not the input symbol: parse error
Sample Parse
Let us analyze the input bb
. The initial parse stack contains the start symbol and the end marker A $
.
+-------+-------+-----------+
| Stack | Input | Action |
+-------+-------+-----------+
| A $ | bb$ | A -> X |
| X $ | bb$ | X -> BX |
| B X $ | bb$ | B -> b |
| b X $ | bb$ | consume b |
| X $ | b$ | X -> BX |
| B X $ | b$ | B -> b |
| b X $ | b$ | consume b |
| X $ | $ | X -> <e> |
| $ | $ | accept |
+-------+-------+-----------+
Conclusion
As you can see, rules of the form A = B*
can be parsed without problems. The resulting concrete parse tree for input bb
would be:
