[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Chicken-users] Re: packrat example
From: |
Thomas Hafner |
Subject: |
[Chicken-users] Re: packrat example |
Date: |
04 Dec 2006 00:28:39 +0100 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 |
Zbigniew <address@hidden> wrote/schrieb <address@hidden>:
> Try
>
> (require-extension syntax-case)
> (require-extension packrat)
> (import packrat)
That's sufficient for the original example. But as soon as I played
around with my own first grammar, I had to add also:
(require-extension srfi-1)
It's obviously needed by the packrat egg. Shouldn't srfi-1 then be
provided automatically by just using the packrat egg?
BTW the grammar mentioned above is for a calculator with infix
operators ``+'' and ``-'' with left associativity. I gave myself this
task because a naive solution with a recursive descent parsers
(including a packrat one) is right associative by nature.
Here goes my grammar:
(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))))
E.g. 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))))
Which is both Ok. The empty tail (last clause of ``tail'' definition
above) helped a lot to simplify the grammar: without it, every
operator would need two variants, one with tail and the other one
without.
Maybe somebody can provide yet a simpler solution?
Regards
Thomas