简单的antlr4语法中的输入错误不匹配 [英] Mismatched input error in simple antlr4 grammar
本文介绍了简单的antlr4语法中的输入错误不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用antlr4解析SQL的简单子集.
I'm trying to parse a simple subset of SQL using antlr4.
我的语法如下:
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应用程序对此进行测试,如下所示:
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
}
}
我得到的输出如下:
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?
谢谢
安德鲁
推荐答案
您不能拥有两个匹配相同的词法分析器规则(至少,不处于相同的模式/状态...):
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;
这篇关于简单的antlr4语法中的输入错误不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文