[antlr-interest] code translation

Benoit Fouletier benblo+ANTLR at gmail.com
Wed Jun 24 02:46:42 PDT 2009


Hi!Newbie to ANTLR here, hello all =).

I'm trying to make a translator from Unity's JavaScript (sometimes called
UnityScript) to C#. This particular JS implementation is made to compile to
IL and run on Mono. It's actually based on Boo, the grammar was
written by Rodrigo
B. de Oliveira with ANTLR (don't know which version).
So the 2 languages are *very* similar, they don't differ in principle, only
in syntax. I've already achieved a lot with regular expressions but that
just doesn't seem like the right tool for the job.

90% of the differences are in variable & function declaration, the core is
the same.
Basically, I need to translate this:

*function MyFunction(i : int, b : boolean) : String
{
    return i + " " + b;
}*

to this:

*private string MyFunction(int i, boolean b)
{
    return i + " " + b;
}*


I have access to the compiled lexer & parser (but not the original grammar).
I've tried to go through the token stream and manually rearrange the
declarations (which works but again, doesn't feel like the best way to do
it).
The thing is, I want to retain comments & formatting during translation, but
the lexer doesn't give me the hidden tokens* (and anyway my little fiddlings
would probably explode!).
Also, I don't need a complete compiler: all the code I want to translate
already compiles so I know it's valid.

It seems to me like my needs are fairly simple (!), but I don't know what
approach to take. If I were to rewrite the grammar (or just modify the
ECMAScript grammar found here <http://www.antlr.org/grammar/list>), are
there ways to define translate rules, in the fashion of regex
replacement? Or should I be able to get away with using the compiled lexer I
have?

Sorry my post is so long, I hope I've made the context clear. Cheers =),

        Ben

* I've read the token streams <http://www.antlr2.org/doc/streams.html> article
in the v2 docs (it's not in the v3 docs, is it still valid?), which is very
informative... unfortunately when I setTokenCreator to a hidden stream, I
don't get anything more.
Here's my loop:
*UnityScriptLexer lexer = new UnityScriptLexer(file);
lexer.setTokenCreator(new
antlr.CommonHiddenStreamToken.CommonHiddenStreamTokenCreator());

antlr.IHiddenStreamToken token;
while ((token = (antlr.IHiddenStreamToken)lexer.nextToken()).Type != EOF)
{
    if (token.getHiddenBefore() != null)
        LogToken(token.getHiddenBefore());   // this never happens

    LogToken(token);

    if (token.getHiddenAfter() != null)
        LogToken(token.getHiddenAfter());    // this never happens
}*
LogToken() just outputs to the console.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090624/b8f4dbd1/attachment.html 


More information about the antlr-interest mailing list