[英]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.