next up previous
Next: The language of a Up: Context free grammars Previous: Context free grammars

What is a context-free grammar?

A context-free grammar $ G=(V,\Sigma,S,P)$ is given by

As an example we define a grammar for the language of arithmetical expressions over $ a$ (using only $ +$ and $ *$), i.e. elements of this language are $ a+(a*a)$ or $ (a+a)*(a+a)$. However words like $ a++a$ or $ )(a$ are not in the language.

We define $ G=(\{E,T,F\},\{(,),a,+,*\},E,P)$ where $ P$ is given by:

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

To save space we may combine all the rules with the same left hand side, i.e. we write

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


next up previous
Next: The language of a Up: Context free grammars Previous: Context free grammars
Thorsten Altenkirch 2001-05-08