Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
255 views
Welcome To Ask or Share your Answers For Others

1 Answer

A couple of things:

  1. you should use the parser rule file_input as your starting point: single_input is used for REPL-like parsing of Python source
  2. public enterFuncdef in not valid Java code: you're missing a return type (void, in this case)
  3. the Python source you're parsing in not valid Python 3 source. print "..." should be print("...")

Now, if you look at the parser rule you're listening for:

funcdef
 : DEF NAME parameters ( '->' test )? ':' suite
 ;

you see it has a NAME token. You can get a hold of it like this:

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

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 void enterFuncdef(Python3Parser.FuncdefContext ctx) {
        System.out.printf("NAME=%s
", ctx.NAME().getText());
      }
    }, parser.file_input());
  }
}

Running the class above will print: NAME=sum

Btw, there is an example from the grammar's repo that does exactly what you're trying to: https://github.com/bkiers/python3-parser/blob/master/src/main/java/nl/bigo/pythonparser/Main.java


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...