简体   繁体   English

如何在 PEG 语法中定义十进制数?

[英]How do you define a decimal number in a PEG grammar?

I have the following grammar我有以下语法

Arithmetic:
    Term     < Factor (Add / Sub)*
    Add      < "+" Factor
    Sub      < "-" Factor
    Factor   < Primary (Mul / Div)*
    Mul      < "*" Primary
    Div      < "/" Primary
    Primary  < Parens / Neg / Pos / Number / Variable
    Parens   < "(" Term ")"
    Neg      < "-" Primary
    Pos      < "+" Primary

    Dot      < "."
    Decimal  < ~(digit+ Dot? digit*)
    Integer  < digits
    Number   < Integer / Decimal

    Function < identifier (space+ Primary)+
    Variable <- identifier

Most everything works, except when I try to parse a decimal (such as 0.5 ) it does not work.大多数一切都有效,除非我尝试解析小数(例如0.5 )它不起作用。 What is the proper syntax to define a Decimal parser in PEG?在 PEG 中定义 Decimal 解析器的正确语法是什么?

I am using the Pegged library in d-lang.我在 d-lang 中使用 Pegged 库。 See here for the docs.有关文档,请参见此处

Since PEG alternatives are ordered, you need to write:由于订购了 PEG 替代品,因此您需要编写:

Number   < Decimal / Integer

As written, Integer / Decimal will always match the Integer at the beginning of a number, so Decimal will never be tried.如所写, Integer / Decimal将始终与数字开头的Integer匹配,因此永远不会尝试Decimal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM