Rule precedence issue with grako

前端 未结 2 1630
猫巷女王i
猫巷女王i 2021-01-14 14:39

I\'m redoing a minilanguage I originally built on Perl (see Chessa# on github), but I\'m running into a number of issues when I go to apply semantics.

Here is the gr

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-14 15:18

    In Perl, you can use Marpa, a general BNF parser, which supports generalized precedence with associativity (and many more) out of the box, e.g.

    :start ::= Script
    Script ::= Expression+ separator => comma
    comma ~ [,]
    Expression ::=
        Number bless => primary
        | '(' Expression ')' bless => paren assoc => group
       || Expression '**' Expression bless => exponentiate assoc => right
       || Expression '*' Expression bless => multiply
        | Expression '/' Expression bless => divide
       || Expression '+' Expression bless => add
        | Expression '-' Expression bless => subtract
    

    Full working example is here. As for programming languages, there is a C parser based on Marpa.

    Hope this helps.

提交回复
热议问题