Search code examples
grammarrakunqp

It is possible to write NQP's precedence parser in Raku


I'm trying to figure out how I can rewrite NQP's Precedence Parser in Raku :

The Precedence Parser is implemented here: https://github.com/Raku/nqp/blob/master/src/HLL/Grammar.nqp#L384 NQP should be a subset of Raku but the Grammar part seems to be specialized.

If I want to rewrite the Precedence Parser in EXPR() in Raku instead, what would be the Raku Grammar primitives to use? I.e. What would cursor_start_cur() translate to? is there a cursor in a Raku Grammar? How can I set pos of a Raku Match object ? What would $termcur.MATCH() translate to, etc...

I am not searching for different ways of writing a Precedence Parser, but rather want to know whether it can be done in Raku in the same way that NQP does it.


Solution

  • jnthn wrote in IRC:

    rule EXPR { <termish> [<infix> <termish>]* } 
    token termish { <prefix>* <term> <postfix>* }
    

    and then done the precedence sorting in an action method.

    There is an example https://github.com/Apress/perl-6-regexes-and-grammars/blob/master/chapter-13-case-studies/operator-precedence-parser-class.p6 from the book https://www.apress.com/us/book/9781484232279 that implement the same structure.