解析树顶的分数

发布于 2024-11-29 11:31:18 字数 952 浏览 1 评论 0原文

我在解析树顶语法中的分数时遇到一些困难。

我的语法看起来像这样

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2024-12-06 11:31:18

我终于成功了,虽然不理想,但我能够正确识别分数。

    rule number 
     frac_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)  {
            def value
                [:frac_number, text_value]
            end
        }
   end

这会处理以下情况 (1/2, 1, 1 1/2)。
然后,当我拥有我的树时,我必须测试“数字”的组成,如果它有分数,我将评估该分数以获得小数,并将其添加到数字(如果它们)是一。

I finally got this working, not ideal, but I am able to recognize the fractions properly.

    rule number 
     frac_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)  {
            def value
                [:frac_number, text_value]
            end
        }
   end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文