Skip to content Skip to sidebar Skip to footer

Parsing Some Java Code With Python Using Antlr

I want to build a Java parser using ANTLR in Python. I downloaded the grammars from the ANTLR repository: Lexer :https://github.com/antlr/grammars-v4/blob/master/java/java/JavaLexe

Solution 1:

Your text file contains a compilationUnit, not an expression you try to parse with

tree = parser.expression()

Look carefully through the parser rules, the rule you need is

compilationUnit
    : packageDeclaration? importDeclaration* typeDeclaration* EOF
    ;

which has to be called as

tree = parser.compilationUnit()

Post a Comment for "Parsing Some Java Code With Python Using Antlr"