Antlr如何避免reportAttemptingFullContext和reportAmbigity [英] Antlr How to avoid reportAttemptingFullContext and reportAmbiguity

查看:19
本文介绍了Antlr如何避免reportAttemptingFullContext和reportAmbigity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java程序中,我分析了许多代码行,并使用了:

ParseTreeWalker walker = new ParseTreeWalker ();
if (!(lexerErrorListener.hasError() || parserErrorListener.hasError ()))
     walker.walk (listener, tree);
else
     line error

使用监听程序:

        @Override
        public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact,
                BitSet ambigAlts, ATNConfigSet configs) {
            hasError = true;
        }

        @Override
        public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex,
            BitSet conflictingAlts, ATNConfigSet configs) {
            hasError = true;
        }

输入如下:|U1,0 = comment生成歧义和完整的上下文错误与我的语法。 是错误的方法还是有处理这些错误的方法?

我的词法分析器:

 lexer grammar LexerGrammar;

SINGLE_COMMENT : '|' -> pushMode(COMMENT); 
NUMBER : [0-9];  
VIRGOLA : ',';
WS  : [ 	] -> skip ;   
EOL : [
]+;   
 
// ------------ Everything INSIDE a COMMENT ------------ 
mode COMMENT;
    COMMENT_NUMBER  : NUMBER -> type(NUMBER);
    COMMENT_VIRGOLA : VIRGOLA -> type(VIRGOLA);  
    
    TYPE : 'I'| 'U'| 'Q';
     
    EQUAL : '=';
    
    COMMENT_TEXT: ('a'..'z' | 'A'..'Z')+;
     
    WS_1        : [ 	] -> skip ;   
    COMMENT_EOL : EOL -> type(EOL),popMode;

我的解析器:

 parser grammar Parser;

options {
      tokenVocab = LexerGrammar;
  }

prog : (line? EOL)+;   
line : comment; 

comment: SINGLE_COMMENT (defComm | genericComment); 
defComm: TYPE arg EQUAL COMMENT_TEXT; 
arg : (argument1) (VIRGOLA argument2)?;   
  
argument1    : numbers ;  
argument2    : numbers ;   
numbers      : NUMBER+ ;  
genericComment: .*?;

推荐答案

reportAmbiguityreportAttemptingFullContext并不表示存在语法错误。您可以监听这些事件以了解它们是否发生,但ANTLR有一种确定性的方法来解决这种歧义(它使用第一种替代方法)。

如果您不将这些视为错误,您将从您的分析中获得正确的分析树。

这篇关于Antlr如何避免reportAttemptingFullContext和reportAmbigity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆