Python AST of ANTLR Parse Tree?

I found the ANTLRv4 Python3 grammer , but it generates a parsing tree that usually has many useless nodes.

I am looking for a well-known package to get Python AST from this parse tree.

Is there something similar?

EDIT: Clarification regarding the use of the Python package ast: my project is in Java, and I need to parse the Python files.

EDIT 2: "AST" I mean http://docs.python.org/2/library/ast.html#abstract-grammar , and the "parse tree" is "a href =" http: //docs.python .org / 2 / reference / grammar.html "rel =" nofollow "> http://docs.python.org/2/reference/grammar.html.

+4
source share
4 answers

The following could be a launch:

public class AST {

    private final Object payload;

    private final List<AST> children;

    public AST(ParseTree tree) {
        this(null, tree);
    }

    private AST(AST ast, ParseTree tree) {
        this(ast, tree, new ArrayList<AST>());
    }

    private AST(AST parent, ParseTree tree, List<AST> children) {

        this.payload = getPayload(tree);
        this.children = children;

        if (parent == null) {
            walk(tree, this);
        }
        else {
            parent.children.add(this);
        }
    }

    public Object getPayload() {
        return payload;
    }

    public List<AST> getChildren() {
        return new ArrayList<>(children);
    }

    private Object getPayload(ParseTree tree) {
        if (tree.getChildCount() == 0) {
            return tree.getPayload();
        }
        else {
            String ruleName = tree.getClass().getSimpleName().replace("Context", "");
            return Character.toLowerCase(ruleName.charAt(0)) + ruleName.substring(1);
        }
    }

    private static void walk(ParseTree tree, AST ast) {

        if (tree.getChildCount() == 0) {
            new AST(ast, tree);
        }
        else if (tree.getChildCount() == 1) {
            walk(tree.getChild(0), ast);
        }
        else if (tree.getChildCount() > 1) {

            for (int i = 0; i < tree.getChildCount(); i++) {

                AST temp = new AST(ast, tree.getChild(i));

                if (!(temp.payload instanceof Token)) {
                    walk(tree.getChild(i), temp);
                }
            }
        }
    }

    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder();

        AST ast = this;
        List<AST> firstStack = new ArrayList<>();
        firstStack.add(ast);

        List<List<AST>> childListStack = new ArrayList<>();
        childListStack.add(firstStack);

        while (!childListStack.isEmpty()) {

            List<AST> childStack = childListStack.get(childListStack.size() - 1);

            if (childStack.isEmpty()) {
                childListStack.remove(childListStack.size() - 1);
            }
            else {
                ast = childStack.remove(0);
                String caption;

                if (ast.payload instanceof Token) {
                    Token token = (Token) ast.payload;
                    caption = String.format("TOKEN[type: %s, text: %s]",
                            token.getType(), token.getText().replace("\n", "\\n"));
                }
                else {
                    caption = String.valueOf(ast.payload);
                }

                String indent = "";

                for (int i = 0; i < childListStack.size() - 1; i++) {
                    indent += (childListStack.get(i).size() > 0) ? "|  " : "   ";
                }

                builder.append(indent)
                        .append(childStack.isEmpty() ? "'- " : "|- ")
                        .append(caption)
                        .append("\n");

                if (ast.children.size() > 0) {
                    List<AST> children = new ArrayList<>();
                    for (int i = 0; i < ast.children.size(); i++) {
                        children.add(ast.children.get(i));
                    }
                    childListStack.add(children);
                }
            }
        }

        return builder.toString();
    }
}

and can be used to create an AST for input "f(arg1='1')\n"as follows:

public static void main(String[] args) {

    Python3Lexer lexer = new Python3Lexer(new ANTLRInputStream("f(arg1='1')\n"));
    Python3Parser parser = new Python3Parser(new CommonTokenStream(lexer));

    ParseTree tree = parser.file_input();
    AST ast = new AST(tree);

    System.out.println(ast);
}

which will print:

'- file_input
   | - stmt
   | | - small_stmt
   | | | - atom
   | | | '- TOKEN [type: 35, text: f]
   | | '- trailer
   | | | - TOKEN [type: 47, text: (]
   | | | - arglist
   | | | | - test
   | | | | '- TOKEN [type: 35, text: arg1]
   | | | | - TOKEN [type: 53, text: =]
   | | | '- test
   | | | '- TOKEN [type: 36, text:' 1 ']
   | | '- TOKEN [type: 48, text:)]
   | '- TOKEN [type: 34, text: \ n]
   '- TOKEN [type: -1, text:]

, , , , . !

Gist, JavaDocs .

+6

Eclipse DLTK Python Python AST Java. AntlrV3 ast, AntlrV4.

Eclipse PyDev Java python. : .

, , .

0

:

Jython ast ( @delnan , ). , Python, Java.

PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import ast");
PyObject o = interpreter.eval(
    "ast.dump(ast.parse('f(arg1=\\'1\\')', 'filename', 'eval'))" + "\n");
System.out.print(o.toString());

Expression(body=Call(func=Name(id='f', ctx=Load()), args=[], keywords=[keyword(arg='arg1', value=Str(s='1'))], starargs=None, kwargs=None))

, .

0

ANTLR4 can generate a visitor that you can use to move the parsing tree and build the AST. Python has a package ast, so this should not be a problem (if you are using Python).

I wrote a Python toy interpreter in Python 3 using ANTLR4 (as part of my research). The visitor code is in /tinypy/AST/builder/, so you can understand how this is done.

0
source

Source: https://habr.com/ru/post/1548544/


All Articles