[antlr-interest] Parsing Lisp into C++

Ian Kaplan iank at bearcave.com
Sun Sep 27 22:03:14 PDT 2009


  The writer and Professor Umberto Eco points out that translation between
languages is translation between cultures.  The challenge in translation is
to produce a result that is understandable, useful and maybe even elegant.
To translate you must not only translate the words, but the ideas.

  Assuming that you had a magic black box that translated between Lisp and
C++, would such a translation produce something that is useful and
understandable.  You could do the translation and come up with something
that is not maintainable or understandable.  In fact, this is, I suspect,
exactly what you would get as the output from the black box.

  We know that Lisp can be compiled, so it is possible to translate Lisp
into C++ that will statically execute.  But the end result may be as useful
as reading assembly code.

  The issue with Lisp translation is not the language, but the run time,
since Lisp is by its nature a dynamic, typeless (or polymorphic) language.
There are things that you can say in Lisp that cannot be expressed directly
in C++.

  As unappealing as the idea is, it might be better to work through the
algorithms and translate them to C++ (or better, in my view, Java).

  Ian


On Sun, Sep 27, 2009 at 6:32 PM, Richard Lewis <Richard.Lewis at razor-risk.com
> wrote:

>  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
>
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090927/836d6e87/attachment.html 


More information about the antlr-interest mailing list