packrat parser

Any other programming language that is not covered in the Web Scripting category.

packrat parser

Postby hafner on Mon Dec 04, 2006 12:36 am

Hi, my first simple packrat parser according to <http://www.lshift.net/~tonyg/packrat.pdf> works, using Chicken Scheme. It's a calculator with infix operators ``+'' and ``-'' with left associativity. I gave myself this task because a naive solution with a recursive descent parser (including a packrat one) is right associative by nature.

Here goes the grammar:
Code: Select all
  (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:
Code: Select all
(define g (generator '((num . 19) (-) (num . 11) (-) (num . 2))))
Or 10 for that one:
Code: Select all
(define g (generator '((num . 19) (-) (num . 11) (+) (num . 2))))
Regards
Thomas
hafner
Contributer
Contributer
 
Posts: 73
Joined: Tue Mar 07, 2006 2:18 pm
Location: München (Germany)

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron