简单的antlr4语法中的输入错误不匹配

时间:2012-12-20 11:15:52

标签: antlr antlr4

我正在尝试使用antlr4解析SQL的一个简单子集。

我的语法看起来像这样:

grammar Query;
query : select;
select : 'select' colname (','  colname)* 'from' tablename;
colname : COLNAME;
tablename : TABLENAME;
COLNAME: [a-z]+ ;
TABLENAME : [a-z]+;
WS : [ \t\n\r]+ -> skip ; // skip spaces, tabs, newlines

我正在使用一个简单的java应用程序测试它,如下所示:

import java.io.ByteArrayInputStream;
import java.io.InputStream;

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

public class Test {
    public static void main(String[] args) throws Exception {
        // create a CharStream that reads from standard input

        InputStream is = new ByteArrayInputStream("select one,two ,three from table".getBytes());

        ANTLRInputStream input = new ANTLRInputStream(is);

        // create a lexer that feeds off of input CharStream
        QueryLexer lexer = new QueryLexer(input);


        // create a buffer of tokens pulled from the lexer
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        // create a parser that feeds off the tokens buffer
        QueryParser parser = new QueryParser(tokens);

        ParseTree tree = parser.query(); // begin parsing at init rule

        System.out.println(tree.toStringTree(parser)); // print LISP-style tree

    }
}

我得到的输出如下:

line 1:27 mismatched input 'table' expecting TABLENAME
(query (select select (colname one) , (colname two) , (colname three) from (tablename table)))

我不明白为什么解析器似乎将“table”作为解析器树中的表名,但是我也得到了一个错误。我错过了什么?

由于

安德鲁

1 个答案:

答案 0 :(得分:10)

你不能有两个匹配相同的词法规则(至少,不是在同一模式/状态......):

...
COLNAME: [a-z]+ ;
TABLENAME : [a-z]+;
...

请改为:

grammar Query;
query     : select;
select    : 'select' colname (',' colname)* 'from' tablename;
colname   : ID;
tablename : ID;
ID        : [a-z]+;
WS        : [ \t\n\r]+ -> skip;