[antlr-interest] Parsing Lisp into C++
Loring Craymer
lgcraymer at yahoo.com
Sun Sep 27 22:49:45 PDT 2009
This is a case where I have to ask "why". The typical Lisp compiler (not interpreter) is a Lisp to C translator with some additional glue. You can probably even find support for translating CLOS to C++ if you look around.
--Loring
>
>From: Richard Lewis <Richard.Lewis at razor-risk.com>
>To: antlr-interest at antlr.org
>Sent: Sunday, September 27, 2009 6:32:19 PM
>Subject: [antlr-interest] Parsing Lisp into C++
>
> >
>
>
>>
>I've started looking into translating a large amount of
>legacy Lisp code into C++ using Antlr. I put together a simple grammar that
>generates an AST. My question is: Where is the best place to attach semantic information?
>It seems to me that I should have a 2 pass parser, starting with the AST as
>shown below and then making an additional pass to generate another AST
>that contains semantics. Unfortunately I'm not that familiar with Lisp but it
>seems to be difficult to parse in a single pass without resorting to an ugly
>grammar definition since everything in Lisp seems to be an expression of some
>sort. This is ironic since Lisp already seems to be "parsed".
>
>Input:
>
>(defun foo (x y) (progn (+ x 1) (+ y 1)))
>
>Grammar:
>
>program: (sexpr)* -> ^(PROGRAM sexpr*);
>sexpr: QT?(list|atom) ;
>list: '(' ')' |
>'(' members ')' -> ^(LIST members);
>members: (sexpr)+;
>atom: OPERATOR | ID | num | STRING ;
>num : (n=INT|n=FLOAT)
>-> ^(NUM $n);
>
>AST Output:
>
>PROGRAM
> LIST
> defun
> foo
> LIST
> x
> y
> LIST
> progn
> LIST
> +
> x
> 1
> LIST
> +
> y
> 1
>
>Desired Output:
>
>PROGRAM
> FUNCTION
> foo
> ARGS
> x
> y
> BLOCK
> +
> x
> 1
> +
>
> y
> 1
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090927/39afb3ff/attachment.html
More information about the antlr-interest
mailing list