A left associative packrat parser
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A recursive descent parser (including a packrat one) is right
associative by nature. This may be undesired, e.g. for a calculator
with operations _+_ and _-_.
The following grammar is for a simple packrat parser[1] to be
implemented in Scheme (e.g. Chicken with Egg _packrat_):
(define calc
(packrat-parser
expr
(expr ((a <- simple b <- tail)
(b a)))
(tail (('+ a <- simple b <- tail)
(lambda (x) (b (+ x a))))
(('- a <- simple b <- tail)
(lambda (x) (b (- x a))))
(()
values))
(simple ((a <- 'num)
a))))
Examples:
- The resulting value is 6 for this generator:
(define g (generator '((num . 19) (-) (num . 11) (-) (num . 2))))
- Or 10 for that one:
(define g (generator '((num . 19) (-) (num . 11) (+) (num . 2))))
- [1]
http://www.lshift.net/~tonyg/packrat.pdf