I have used Python3 grammar and have built an AST. My source string is just a small function of Python. My code is as follows:
public class Main {
public static void main(String[] args) {
String source = "def sum( arg1, arg2 ):
"
+ " total = arg1 + arg2
"
+ " print "Inside the function : ", total
"
+ " return total;
";
Python3Lexer lexer = new Python3Lexer(CharStreams.fromString(source));
Python3Parser parser = new Python3Parser(new CommonTokenStream(lexer));
ParseTreeWalker.DEFAULT.walk(new Python3BaseListener() {
@Override
public enterFuncdef(Python3Parser.FuncdefContext ctx) {
//Here which function would give me the function definition?
}
}, parser.single_input());
}
}
Here, how do I output the name of the function? (sum)? I am having a little hard time understanding the grammar.
See Question&Answers more detail:os