[antlr-interest] Parsing Lisp into C++

Richard Lewis Richard.Lewis at razor-risk.com
Sun Sep 27 18:32:19 PDT 2009


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/20090928/b82d3e5a/attachment.html 


More information about the antlr-interest mailing list