[antlr-interest] Language-Neutral Actions

Andy Tripp atripp at comcast.net
Sun Apr 17 12:44:55 PDT 2005


>
>
>I think what may work is to do untyped parameters and return values, and
>when generating code we could specify a language mapping for the types.
>

IDL is a standard way to define a language-neutral interface.
To quote from this http://www.omg.org/gettingstarted/omg_idl.htm intro:

> IDL is not a programming language - it's great for defining 
> interfaces, but it doesn't have the constructs you'd need to write a 
> program.  To do this, OMG has defined mappings from IDL to just about 
> every major programming language: C, C++, Java, Smalltalk, COBOL, Ada, 
> Lisp, PL/1, Python, and IDLscript have standard mappings.

So you could have people embed IDL as their action code, and then ANTLR 
would generate whichever
target language the user wants. The advantages of using IDL are that IDL 
is already well defined and widely used,
the tools already exist for translating it to all the popular languages, 
and the mappings between the
IDL and what gets generated is very clear and well documented. One 
disadvantage (mentioned
in the quote above) is that IDL doesn't let you really write general 
code, just interfaces really.
But I think that's really what you are getting at when you talked about 
just calling functions to
do all the work, rather than embedding lots of code.

And of course, you can just use IDL for exactly what you want: 
generating language-specific
code from language-neutral code. You don't have to use CORBA or anything.

Say I want to have this action code in my ANTLR grammar:
     doSomething(token);
Somewhere, I'd need to write my language-neutral IDL definition of the 
method:

struct Token {
int type;
string text;
...
}
void doSomething(in Token token);

When generating my code, ANTLR would just run the IDL compiler for whatever
the target language is. That IDL compiler would generate the "glue" code 
that
allows the.....uh oh....wait a minute...never mind. The ANTLR-generated code
is already in the target language and on the same machine as the called 
code,
so you don't need that. I'm just rambling, and sometimes I end up at a 
dead end :)

What you need is the thing that IDL clearly is not: a simple 
language-neutral
programming language, not a simple language-neutral interface definition 
language.
Arg.

I suppose maybe the best solution is what you said 
originally...essentially limit
the action code to function calls, and gloss over the slight differences
in semantics between languages (e.g. in C, a pionter to a struct is 
passed, while in
Java, an Object reference is passed).

To summarize, I have nothing constructive to say on this subject :)

Andy




More information about the antlr-interest mailing list