Mismatched input error in simple antlr4 grammar

不羁岁月 提交于 2019-11-29 22:58:24

问题


I'm trying to parse a simple subset of SQL using antlr4.

My grammar looks like this:

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

I am testing this with a simple java application as follows:

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

    }
}

The output I get is as follows:

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

What I don't understand is why the parser seems to be picking up "table" as the tablename in the parser tree, but yet I'm also getting an error thrown. What am I missing?

Thanks

Andrew


回答1:


You cannot have two lexer rules that match the same (at least, not in the same mode/state...):

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

Do this instead:

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


来源:https://stackoverflow.com/questions/13970984/mismatched-input-error-in-simple-antlr4-grammar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!