本文介绍了简单 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]+;
...

改为这样做:

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

这篇关于简单 antlr4 语法中的不匹配输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:53