问题
I am rather new to grammars, and was wondering if someone could help me determine using a parse tree how this grammar below is ambiguous? I know that it needs to have two different strings that can be created.
S -> (S)|SS|()
I can def convert it to chomsky normal form and greibach, but ambiguity is perplexing to me with these.
回答1:
The easiest way to prove a grammar ambiguous is to find a sentence with two different parse trees. (Or two different rightmost derivations, which is exactly the same thing. Or, if you prefer, two different leftmost derivations.)
S → S S | X
is always ambiguous (for any X
), because the sentence X X X
has two different parse trees:
S S
/ \ / \
/ S S \
/ / \ / \ \
X X X X X X
来源:https://stackoverflow.com/questions/43403182/how-to-prove-a-grammar-is-ambiguous-s-sss