Grammar ambiguity

The problem of grammar ambiguity occurs when the grammar generates sentences that have more than one parse tree, which is a hierarchical structure that graphically presents sentence derivation A root node is labelled by the start symbol. Branch nodes are labelled by non-terminal and leaf nodes by terminal or empty symbols. Branch structure represents productions. Main branch is labelled by the left production side, and branches derived from it are labelled by the right production side, in the order from left to right. 

Example 1: 

Derive the sentence 1 - 0 - 1 from the given grammar:

                                            
                                                
E:: = E – E | 0 | 1

Two different parse trees can be drawn for the sentence 1- 0 -1.  

Example 2: 

Grammar ambiguity is present also in C and Pascal languages for IF conditional instruction. It is caused by productions:

                                
                                    
S :: = if E then S
S :: if E then S else S

For the sentence if E1 then if E2 then S1 else S2 two different parse trees can be derived.  



If-else ambiguity problem in Pascal and C is solved in that way, that the key word else is combined with the latest key word if. 

In this case the considered sentence is interpreted, as it is shown in parse tree 1.