问题
I'm trying to figure out how to do parse trees correctly to show whether a grammar is ambiguous or not.
my grammar is S -> xSy | ySx | SS | e
any help would be great. thanks in advance
below is my crack at it...
S
/ \
/ \
x y
/ \ / \
x x y y
回答1:
As a hint, pretty much any grammar with a production of the form
S → SS
will be ambiguous, because if you want to produce three S nonterminals you can do so in two ways:
S S
/ \ / \
S S S S
/ \ / \
S S S S
Assuming those S's can actually produce strings of terminals, these two "gadgets" can be put into the parse tree to derive the same string in two different ways.
Hope this helps!
来源:https://stackoverflow.com/questions/19281360/parse-tree-of-context-free-grammar