I am using the Python3 grammar from below location,
https://github.com/antlr/grammars-v4/blob/master/python3/Python3.g4
I have the below code to to parse,
ANTLRInputStream input = new ANTLRInputStream(new FileInputStream("Functions.py"));
Python3Lexer lexer = new Python3Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Python3Parser parser = new Python3Parser(tokens);
ParseTree tree = parser.funcdef(); //Not sure what to do here
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(new Listener(), tree);
Listener.java
public class Listener extends Python3BaseListener{
@Override
public void enterImport_name(Python3Parser.Import_nameContext ctx) {
System.out.println(ctx.getText());
}
@Override
public void enterFuncdef(Python3Parser.FuncdefContext ctx) {
System.out.println(ctx.getText()); //returns the whole code as string
}
}
I am trying to read all the imports, Variables and method names along with arguments from the python file.
How can i do this?