ANTLR4 extraneous input

2019-05-20 23:07发布

问题:

I have a problem with my ANTLR4. I'm trying to print AST from python 3 code but there are some errors and I don't know how to fix them.

I wrote simple code for test:

a=(1,2,3)
print(a)

I ran the program but this errors appeared:

line 1:1 extraneous input '=' expecting {<EOF>, '.', '*', '(', '**', '[', '|', '^', '&', '<<', '>>', '+', '-', '/', '%', '//', '@'}
line 2:0 extraneous input '\n' expecting {<EOF>, '.', '*', '(', '**', '[', '|', '^', '&', '<<', '>>', '+', '-', '/', '%', '//', '@'}
line 3:0 extraneous input '\n' expecting {<EOF>, '.', '*', '(', '**', '[', '|', '^', '&', '<<', '>>', '+', '-', '/', '%', '//', '@'}

My main class :

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import org.antlr.v4.*;
public class Main {

    public static void main(String[] args) {
        try {
            ANTLRInputStream input = new ANTLRFileStream("/home/grzegorz/Desktop/Python3/input.txt");
            Python3Lexer lexer = new Python3Lexer(input);
            CommonTokenStream token = new CommonTokenStream(lexer);
            Python3Parser parser = new Python3Parser(token);
            ParseTree parseTree = parser.expr();
            System.out.println(parseTree.toStringTree(parser));

        }catch (Exception ex){
            ex.printStackTrace();
        }

    }
}

I have the grammar from this site: https://github.com/antlr/grammars-v4/tree/master/python3

回答1:

Explanation

Your input file consists of two statements and you are parsing the file as if it was an expression (with line ParseTree parseTree = parser.expr();; rule expr from Python 3 grammar).

This also exaplains the first error: an identificator a is accepted as a part of expression but = sign is not. That's because = is a part of assignment statement.

Solution

Parse the input using another grammar rule for example file_input rule which will accept many statements. Change the abovementioned line to ParseTree parseTree = parser.file_input();.



标签: antlr4