解析树顶的分数
我在解析树顶语法中的分数时遇到一些困难。
我的语法看起来像这样
grammar Numbers
rule number
regular_number optional_frac {
def value
[:number, text_value]
end
}
end
rule optional_frac
frac_number? {
def value
[:optional_frac, text_value]
end
}
end
rule frac_number
(s? regular_number '/' regular_number)? / ('/' regular_number)? {
def value
[:frac_number, text_value]
end
}
end
rule regular_number
[0-9\.\/-]+ {
def value
text_value
end
}
end
end
当我尝试从 I 获取值时,
2 => number
1/2 => number, frac_number
2 2/4 => not recognized
尽管规则
s? regular_number '/' regular_number
将以分数形式返回。知道我的语法有什么问题吗?
I'm having a bit of difficulty parsing fractions in my treetop grammar.
My grammar looks like this
grammar Numbers
rule number
regular_number optional_frac {
def value
[:number, text_value]
end
}
end
rule optional_frac
frac_number? {
def value
[:optional_frac, text_value]
end
}
end
rule frac_number
(s? regular_number '/' regular_number)? / ('/' regular_number)? {
def value
[:frac_number, text_value]
end
}
end
rule regular_number
[0-9\.\/-]+ {
def value
text_value
end
}
end
end
When I try to get the values from
2 => number
1/2 => number, frac_number
2 2/4 => not recognized
I though the rule
s? regular_number '/' regular_number
would return as a fraction. any idea what is wrong in my grammar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于成功了,虽然不理想,但我能够正确识别分数。
这会处理以下情况 (1/2, 1, 1 1/2)。
然后,当我拥有我的树时,我必须测试“数字”的组成,如果它有分数,我将评估该分数以获得小数,并将其添加到数字(如果它们)是一。
I finally got this working, not ideal, but I am able to recognize the fractions properly.
This takes care of the following cases (1/2, 1, 1 1/2).
Then, when I have my tree, I'll have to test for the make-up of 'number', and if it has a fraction, I'll eval the fraction to get a decimal, and add it to the number if their is one.