Treetop ruby 解析器 - 无法解析有序选择
我定义了使用 Treetop 解析字符串和数字的简单语法,如下所示。
grammar Simple
rule value
number / string
end
rule string
word space string
/
word
end
rule word
[0-9a-zA-Z]+
end
rule number
[1-9] [0-9]*
end
rule space
' '+
end
end
Ruby:
parser = SimpleParser.new
parser.parse('123abc wer') # => nil
我希望解析器返回字符串节点,但看起来解析器无法理解输入。任何想法将不胜感激。
I have defined simple grammar for parsing string and number using Treetop as below.
grammar Simple
rule value
number / string
end
rule string
word space string
/
word
end
rule word
[0-9a-zA-Z]+
end
rule number
[1-9] [0-9]*
end
rule space
' '+
end
end
Ruby:
parser = SimpleParser.new
parser.parse('123abc wer') # => nil
I expect the parser to return string node but look like the parser could not understand the input. Any idea would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Treetop(实际上是一般的 PEG)中,选择运算符是有序的,这与大多数其他解析形式不同。
因此,
您告诉 Treetop 您更喜欢
数字
而不是字符串
。您的输入以
1
开头,它与number
和string
匹配两者(通过word
),但您告诉 Treetop 更喜欢number
解释,因此它将其解析为number
。当涉及到输入中的a
时,它没有更多的规则可以应用,因此它什么也没有返回(nil
),因为在Treetop中,不这样做是一个错误消耗整个输入流。如果您只是反转选择的顺序,整个输入将被解释为
string
而不是number
:或者,您可以保持原样,但允许要多次匹配的
value
规则。插入一个新的顶级规则,如下所示:或修改
value
规则,如下所示:这将为您提供大致如下所示的 AST:
In Treetop (and PEGs in general, actually) the choice operator is ordered, unlike most other parsing formalisms.
So, in
you are telling Treetop that you prefer
number
overstring
.Your input starts with
1
, which matches bothnumber
andstring
(throughword
), but you told Treetop to prefer thenumber
interpretation, so it parses it as anumber
. When it comes to thea
in the input, it has no more rules to apply, and thus it returns nothing (nil
), because in Treetop it is an error to not consume the entire input stream.If you simply reverse the order of the choice, the entire input will interpreted as a
string
instead of anumber
:Or, you could keep the order as it is, but allow the
value
rule to be matched multiple times. Either insert a new top-level rule like this:or modify the
value
rule like this:Which will give you an AST roughly like this: