0
votes

I'm learning about ambiguity in grammars and I need a little help to understand better. Here is a grammar:

<S> ::= if <S> then <S>
<S> ::= if <S> then <S> else <S>
<S> ::= a

Using a parse tree or left-most derivation, how can I show that this grammar is ambiguous?

1
Try nesting if statements in different combinations, and for each one think of whether you can put "parentheses" on it multiple conflicting ways - Alex Knauth

1 Answers

1
votes

Consider the following:

if a then if a then a else a

You could consider grouping it either of the following two ways:

(if a then (if a then a else a))

or

(if a then (if a then a) else a)

Both are possible with the grammar you provide, so it's ambiguous.