[英]Why an empty string seems emitted instead of a custom node, sometimes, in a Treetop grammar?
关于您使用Treetop时出现的反复出现的问题,我想请您提出建议,我有时无法解决。 我可能错过了一些东西。
我怀疑你们中的许多人都有解决这个问题的正确习惯或习惯。
我通常使用Treetop如下所示:
但是,我遇到了经典错误消息,但通常无法修复:
parsetree.rb:380:in `to_ast': undefined method `to_ast' for SyntaxNode
offset=149, "":Treetop::Runtime::SyntaxNode (NoMethodError)
我在这里感到困惑,因为似乎发出了空字符串“”而不是我的自定义节点之一。
在此示例中,在第380行上,我有以下代码(关于有限状态机)
# in parsetree.rb
class Next < Tree
def to_ast
ret=Ldl::Ast::Next.new
ret.name=ns.to_ast
if cond
ret.condition=cond.c.to_ast
end
ret.actions=acts.to_ast # <==== line 380
ret
end
end
class NextActions < Tree
def to_ast
eqs.elements.collect{|eq| eq.to_ast}
end
end
我所关心的那个错误的语法是:
rule nextstate
space? 'next' space ns:identifier space? cond:('?' space? c:expression)? space
acts:next_actions? <Ldl::ParseTree::Next>
end
rule next_actions
space? eqs:equation+ space 'end' space <Ldl::ParseTree::NextActions>
end
您的问题与可选表达式的行为有关。
act:next_actions是可选的。 如果此可选元素在输入中不匹配,则不会得到NextActions节点,而是得到epsilon。 您应该通过类似以下内容来检测到这一点:
ret.actions = acts.empty? ? [] : acts.to_ast
因为由标签cond:命名的序列是可选的,所以可能会出现相同的问题。 如果输入中不存在此序列,则其内容为“ c”。 在那种情况下,“ cond”将仍然被定义,并且您的“ if”语句为true,但是cond.c.to_ast将失败。
简短的答案:使用可选表达式时,应该对其进行标记并测试标记是否为空? 在尝试使用内容之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.