Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

grammar

raku

nqp

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.

like image 395
Konrad Eisele Avatar asked Jul 19 '20 15:07

Konrad Eisele


People also ask

How the parsing is performed in operator precedence parsing?

Operator precedence parsers use precedence functions that map terminal symbols to integers, and the precedence relations between the symbols are implemented by numerical comparison. The parsing table can be encoded by two precedence functions f and g that map terminal symbols to integers.

Which of the following grammar is operator precedence?

A grammar is said to be operator precedence grammar if it has two properties: No R.H.S. of any production has a∈. No two non-terminals are adjacent.


1 Answers

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.

like image 185
Konrad Eisele Avatar answered Oct 25 '22 21:10

Konrad Eisele