Friday, 26 May 2017

Using ANTLR 4 to construct a Java AST with Node JS

I am trying to parse Java 8 files and create an AST out of them using antlr 4, all in Javascript / Node JS. Everything seems to be going well, with the exception of the actual AST construction. When I call parser.compilationUnit() (which is the start rule in the Java8 antlr grammar it seems), the program does not terminate until it eventually runs out of heap.

Here is what I have done so far:

I managed to generate the Java8 antlr files using:

antlr4 -Dlanguage=JavaScript Java8.g4 -visitor

I created the chars, lexer, tokens and parser following this guide:

const input = JavaExampleFile.code
const chars = new antlr4.InputStream(input)
const lexer = new Java8Lexer.Java8Lexer(chars)
const tokens = new antlr4.CommonTokenStream(lexer)
const parser = new Java8Parser.Java8Parser(tokens)
parser.buildParseTrees = true
const tree = parser.compilationUnit()

Now the problem arises when I call const tree = parser.compilationUnit(). In the grammar, the compilationUnit is defined as the following:

compilationUnit
    :   packageDeclaration? importDeclaration* typeDeclaration* EOF
    ;

and building an AST out of any parts of the definition (packageDeclaration, importDeclaration, typeDeclaration) works just fine. It is just when they are combined into the compilationUnit that the program does not terminate.

Has anyone else dealt with this issue? Any help would be appreciated.



via jundl77

No comments:

Post a Comment