LL(1) parsing is also called predictive parsing. To construct an LL(1) parser for some given grammar, we need to follow these steps -
A grammar is said to be left recursive if it contains some production rule with a non terminal
For some left recursive grammar
Example for left recursion elimination:
Rewrting the above as described, we will get:
Left factoring is done to transform the grammar into a form that is more suitable for recursively parsing.
For a production rule of the form
Remove any possible left recursion and left factoring before calculating the first and follow for any given grammar.
first(X)
is the set of terminals that are the beginnings of strings that are derived from X. If X derives first(X)
. The following rules have to be applied until no further elements can be added to first(X)
.
first(X)
is first(X)
first(X)
first(X)
.
follow(Y)
is the set of terminals that occur immediately to the right of follow(Y)
.
follow
follow(X)
to follow(Y)
follow(Y)
first(B)
does not contain follow(Y)
first(B)
without follow(X)
to follow(Y)
. This makes sense, as To construct the predictive parsing table, the following rules have to be used -