当前位置: 首页 > 知识库问答 >
问题:

ANTLR 4 SQLite语法给出空令牌流

梁才
2023-03-14

我正在使用ANTLR4语法来解析SQLite查询。但是,lexer返回一个空的令牌流,即使我使用的是一个简单的选择查询。

这会产生以下错误:


atorg.antlr.v4.runtime.Lexer.nextToken(Lexer.java:138)
atorg.antlr.v4.runtime.BufferedTokenStream.fetch(BufferedTokenStream.java:186)
atorg.antlr.v4.runtime.BufferedTokenStream.sync(BufferedTokenStream.java:169)
atorg.antlr.v4.runtime.BufferedTokenStream.setup(BufferedTokenStream.java:271)
atorg.antlr.v4.runtime.BufferedTokenStream.lazyInit(BufferedTokenStream.java:266)
atorg.antlr.v4.runtime.CommonTokenStream.LT(CommonTokenStream.java:116)
atorg.antlr.v4.runtime.Parser.enterRecursionRule(Parser.java:677)
atantlr.antlr4.SQLIte.SQLiteParser.expr(SQLiteParser.java:5109)
atantlr.antlr4.SQLIte.SQLiteParser.expr(SQLiteParser.java:5100)
atantlr.antlr4.SQLIte.LexerParser.main(java.lang.IllegalStateException)

这是主类.java代码:

import antlr.antlr4.SQLIte.*;
import antlr.antlr4.SQLIte.SQLiteParser.ExprContext;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class LexerParser {

    public static void main(String[] args) throws Exception {

        // The list that will hold our function names.
        final List<String> functionNames = new ArrayList<String>();

        // The select-statement to be parsed.
        String sql = "SELECT * FROM table";

        // Create a lexer and parser for the input.
        InputStream stream = new ByteArrayInputStream(sql.getBytes(StandardCharsets.UTF_8), 0, 0);

        ANTLRInputStream antlrTemp = new ANTLRInputStream(stream);
        SQLiteLexer lexer = new SQLiteLexer(antlrTemp);
       // SQLiteLexer lexer = new SQLiteLexer(antlrTemp);
        CommonTokenStream cts = new CommonTokenStream(lexer);
        System.out.print(cts.getTokens());

        SQLiteParser parser = new SQLiteParser(cts);

            ExprContext theExpr = parser.expr();
            System.out.println("found expr " + theExpr.toString());

我哪里错了?

语法链接

共有1个答案

况胡媚
2023-03-14

如果它是Maven项目的一部分,这段代码就可以工作。我之前试图将它作为Java项目来运行。

谢啦

 类似资料: