Parsing: Ambiguity05-04-ambiguity.mp405-04-q13
vertLeft1
1,205-04-q1.pngAmbiguous grammars have multiple parse trees for the same string. The first grammar is ambiguous because the string "aba" has two parse trees: one where S -*> [a][ba] and another where S -*> [ab][a]. Similarly, the second grammar is ambiguous because "id+id+id" can be parsed as either E -*> [id]+[id+id] or E -*> [id+id]+[id]. It is not possible to produce multiple parse trees for any string for the third and fourth grammars, which means they are unambiguous.05-04-q23
block2x2-1
305-04-q2.pngThe top right grammar is the only unambiguous option that generates the same language as the given grammar. The top left grammar incorrectly matches the empty string. The bottom left grammar only matches the strings 'a' and 'b'. The bottom right grammar generates no strings at all.