next up previous
Next: How to implement the Up: How to implement a Previous: How to calculate and

Constructing an LL(1) grammar

Let's have a look at the grammar $ G$ for arithmetical expressions again. $ G=(\{E,T,F\},\{(,),a,+,*\},E,P)$ where

$\displaystyle P = \{$ $\displaystyle E \to T \mid E + T$    
$\displaystyle  $ $\displaystyle T \to F \mid T * F$    
$\displaystyle  $ $\displaystyle F \to a \mid ( E )$    

We don't need the $ \textrm {Follow}$-sets in the moment because the empty word doesn't occur in the grammar. For the nonterminal symbols we have

$\displaystyle  \textrm{First}(F)$ $\displaystyle = \{ \texttt{a},\texttt{(} \}$    
$\displaystyle  \textrm{First}(T)$ $\displaystyle = \{ \texttt{a},\texttt{(} \}$    
$\displaystyle  \textrm{First}(E)$ $\displaystyle = \{ \texttt{a},\texttt{(} \}$    

and now it is easy to see that most of the $ \textrm{Lookahead}$-sets agree, e.g.

$\displaystyle  \textrm{Lookahead}(E \to T)$ $\displaystyle = \{ \texttt{a},\texttt{(} \}$    
$\displaystyle \textrm{Lookahead}(E \to E+T)$ $\displaystyle = \{ \texttt{a},\texttt{(} \}$    
$\displaystyle \textrm{Lookahead}(T \to F)$ $\displaystyle = \{ \texttt{a},\texttt{(} \}$    
$\displaystyle \textrm{Lookahead}(T \to T*F)$ $\displaystyle = \{ \texttt{a},\texttt{(} \}$    
$\displaystyle \textrm{Lookahead}(F \to a)$ $\displaystyle = \{ \texttt{a}\}$    
$\displaystyle \textrm{Lookahead}(F \to (E) )$ $\displaystyle = \{ \texttt{(} \}$    

Hence the grammar $ G$ is not LL(1).

However, luckily there is an alternative grammar $ G'$ which defines the same language: $ G'=(\{E,E',T,T',F\},\{(,),a,+,*\},E,P')$ where

$\displaystyle P' = \{$ $\displaystyle E \to T E'$    
  $\displaystyle E' \to \texttt{+} T E' \mid \epsilon$    
  $\displaystyle T \to F T'$    
  $\displaystyle T' \to \texttt{*} F T' \mid \epsilon$    
  $\displaystyle F \to a \mid \texttt{(} E \texttt{)}$    

Since we have $ \epsilon$-productions we do need the $ \textrm {Follow}$-sets.

$\displaystyle  \textrm{First}(E) = \textrm{First}(T) = \textrm{First}(F) = \{ \texttt{a},\texttt{(} \}$    
$\displaystyle  \textrm{First}(E') = \{\texttt{+}\}$    
$\displaystyle \textrm{First}(T') = \{\texttt{*}\}$    
$\displaystyle \textrm{Follow}(E) = \textrm{Follow}(E') = \{ \texttt{)},\texttt{\$} \}$    
$\displaystyle \textrm{Follow}(T) = \textrm{Follow}(T') = \{ \texttt{+},\texttt{)},\texttt{\$} \}$    
$\displaystyle \textrm{Follow}(F) = \{ \texttt{+},\texttt{*},\texttt{)},\texttt{\$} \}$    

Now we calculate the $ \textrm{Lookahead}$-sets:

$\displaystyle  \textrm{Lookahead}(E \to T E')$ $\displaystyle = \{ \texttt{a},\texttt{(} \}$    
$\displaystyle \textrm{Lookahead}(E' \to \texttt{+} T E')$ $\displaystyle =\{ \texttt{+}\}$    
$\displaystyle \textrm{Lookahead}(E' \to \epsilon)$ $\displaystyle = \textrm{Follow}(E') = \{ \texttt{)},\texttt{\$}\}$    
$\displaystyle \textrm{Lookahead}(T \to \texttt{+} F T')$ $\displaystyle = \{ \texttt{a},\texttt{(} \}$    
$\displaystyle \textrm{Lookahead}(T' \to \texttt{*} F T')$ $\displaystyle = \{ \texttt{*} \}$    
$\displaystyle \textrm{Lookahead}(T' \to \epsilon)$ $\displaystyle =\textrm{Follow}(T') = \{ \texttt{+},\texttt{)},\texttt{\$}\}$    
$\displaystyle \textrm{Lookahead}(F \to a)$ $\displaystyle = \{ \texttt{a}\}$    
$\displaystyle \textrm{Lookahead}(F \to (E) )$ $\displaystyle = \{ \texttt{(} \}$    

Hence the grammar $ G'$ is LL(1).


next up previous
Next: How to implement the Up: How to implement a Previous: How to calculate and
Thorsten Altenkirch 2001-05-08