Parse Tree Of Context Free Grammar

馋奶兔 提交于 2019-12-11 20:26:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!