语法分析树?
这道题是我的CS作业,我不知道该怎么做。
考虑语法
S ← ( L )
S ← a
L ← L , S
L ← S
为句子绘制一个解析树 ( a , ( a , a ) )
我尝试遵循该结构,最终得到 (L,(L,L))
code> 但这似乎并不正确。有人能把我推向正确的方向吗?
This question is on my CS homework and I have no idea how to do it.
Consider the grammar
S ← ( L )
S ← a
L ← L , S
L ← S
Draw a parse tree for the sentence ( a , ( a , a ) )
I tried following the structure and I end up with (L,(L,L))
That doesn't seem to be correct though. Could anyone push me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下句子
(a, (a, a))
。它可能匹配哪一个右侧(RHS)?只有第一个,S ← ( L )
。因此,树的根将是一个带有三个子节点的S
节点:一个(
节点、一个L
节点和一个 <代码>)-节点。现在您需要弄清楚
L
节点的子节点是什么,并且它们必须与其余输入匹配:a,(a,a)
。所以看看LHS上有L
的规则。在这些规则中,哪一条的 RHS 可以匹配a,(a,a)
?Look at the sentence
(a, (a, a))
. Which of the right-hand sides (RHS's) can it possibly match? Only the first,S ← ( L )
. So the root of your tree will be anS
-node with three children: a(
-node, anL
-node, and a)
-node.Now you need to figure out what the children of the
L
-node are, and they have to match the remaining input:a,(a,a)
. So look at the rules that haveL
on the LHS. Of those rules, which one has an RHS that can matcha,(a,a)
?(a,(a,a))
的解析树可从(a,(a,a))
的最左派生获得:解析树的根是
S
。对于推导中非终结符的每次重写,在解析树中绘制适当的节点。此外,您的语法不是最佳的,除其他外,还包含链式规则。删除它们将避免必须从L
派生S
才能派生a
。The parse tree for
(a,(a,a))
is obtainable from a leftmost derivation of(a,(a,a))
:The root of your parse tree is
S
. For each rewrite of a nonterminal symbol in the derivation, draw the appropriate node in the parse tree. Also, your grammar isn't optimal and among other things, contains chain rules. Removing them would prevent having to deriveS
fromL
in order to derivea
.这是您所追求的部分内容:
现在您可以完成其余的工作了:)
Here's part of what you're after:
Now you get to do the rest of the work :)