[antlr-interest] HELP: translate javascript code to C#

Guoliang Cao cao at danucom.com
Sun Mar 26 14:25:52 PST 2006


Hello everyone,

I'm new to ANTLR and face a language translation task: translate
Javascript code to C#. Here is the detail:

I can create a form based PDF document and embed Javascript code
snippets in the document. What javascript code does here is accessing
the form controls inside the document, initializing, validating or
computing when a user is operating on the document.

The PDF document is converted to our private format, its forms, fields
are all converted. Now I need to convert Javascript code to C# code
because our private format only supports C# scripting and uses a
different set of APIs.

I read through a couple ANTLR tutorials and basically know how it
works, then I found the only available Javascript grammar on ANTLR
grammar list. I downloaded it, tried to build a parser but got a lot
of warnings. Hoping those warnings are not fatal, I passed a short
code snippet to it. It ruthlessly failed at first character:

line 1:1:unexpected token: v

Then I dive into the grammar, added missing lexical rules, made a few
changes which I think might help, rebuilt the parser, still got
warnings and failed at first character. Anyone please help me. I would
appreciate any ideas, suggestions and comments.

For your convenience, I copied related code/error below. The .g file
can be found at http://www.daoqigame.com/temp/js-new.g

==== grammar warnings ====
ANTLR Parser Generator   Version 2.7.6 (2005-12-22)   1989-2005
src\main\config\js-new.g: warning:lexical nondeterminism between rules EXPONENT_INDICATOR and ALPHA_CHARACTER upon
src\main\config\js-new.g:     k==1:'E','e'
src\main\config\js-new.g:     k==2:<end-of-token>
src\main\config\js-new.g:     k==3:<end-of-token>
src\main\config\js-new.g:     k==4:<end-of-token>
src\main\config\js-new.g: warning:lexical nondeterminism between rules EXPONENT_INDICATOR and LOWER_ALPHA_CHAR upon
src\main\config\js-new.g:     k==1:'e'
src\main\config\js-new.g:     k==2:<end-of-token>
src\main\config\js-new.g:     k==3:<end-of-token>
src\main\config\js-new.g:     k==4:<end-of-token>
...

==== java code ====
JavascriptLexer lexer = new JavascriptLexer(new FileInputStream("input.js"));
JavascriptParser parser = new JavascriptParser(lexer);
parser.program();
System.out.println(parser.getAST().toStringTree());

==== input.js ====
var table = remainingValue.substring(start + 1, end);

// availableList.appendChild(selectedList.options.item(selIndex))

if (formName == xdpText)
{
        var len = anotherList.length++;
        anotherList.options[len].text = subString;
        anotherList.options[len].value = subString1;
}




More information about the antlr-interest mailing list