在 antlr4 中使用错误标记

Consuming Error tokens in antlr4

这是我的语法,我正在尝试输入

alter table ;

一切正常,但当我给

altasder table; alter table ;

它按预期在第一个字符串上给我一个错误,但我想要解析第二个命令而忽略第一个“altasder table;

grammar Hello;

start : compilation;
compilation : sql*;
sql : altercommand;
altercommand : ALTER TABLE SEMICOLON;
ALTER: 'alter';
TABLE: 'table';
SEMICOLON : ';';

如何实现???

我已经使用了 DefualtError 策略,但仍然无法正常工作

import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.misc.IntervalSet;

public class CustomeErrorHandler extends DefaultErrorStrategy {


        @Override
        public void recover(Parser recognizer, RecognitionException e) {
        // TODO Auto-generated method stub
        super.recover(recognizer, e);


            TokenStream tokenStream = (TokenStream)recognizer.getInputStream();


            if (tokenStream.LA(1) == HelloParser.SEMICOLON  )
            {

                IntervalSet intervalSet = getErrorRecoverySet(recognizer);

                tokenStream.consume();

                consumeUntil(recognizer, intervalSet);
            }
        }
    }

主要 class : public class 主要 {

public static void main(String[] args) throws IOException {
    ANTLRInputStream ip = new ANTLRInputStream("altasdere table ; alter table ;");
    HelloLexer lex = new HelloLexer(ip); 
    CommonTokenStream token = new CommonTokenStream(lex);
    HelloParser parser = new HelloParser(token);
    parser.setErrorHandler(new CustomeErrorHandler());
    System.out.println(parser.start().toStringTree(parser));

}

}

我的输出:

line 1:0 token recognition error at: 'alta'
line 1:4 token recognition error at: 's'
line 1:5 token recognition error at: 'd'
line 1:6 token recognition error at: 'e'
line 1:7 token recognition error at: 'r'
line 1:8 token recognition error at: 'e'
line 1:9 token recognition error at: ' '
(start compilation)

为什么它不移动到第二个命令?

需要使用 DefaultErrorStrategy 来控制解析器如何响应识别错误。根据需要进行扩展,修改 #recover 方法,以使用令牌直到令牌流中所需的解析重新启动点。

#recover 的简单实现是:

@Override
public void recover(Parser recognizer, RecognitionException e) {
    if (e instanceof InputMismatchException) {
        int ttype = recognizer.getInputStream().LA(1);
        while (ttype != Token.EOF && ttype != HelloParser.SEMICOLON) {
            recognizer.consume();
            ttype = recognizer.getInputStream().LA(1);
        }
    } else {
        super.recover(recognizer, e);
    }
}

根据需要调整while条件以识别下一个有效点以恢复识别。

请注意,错误消息是由于词法分析器无法匹配无关的输入字符。要删除错误消息,请添加作为最后一个词法分析器规则:

ERR_TOKEN : . ;