在Python 3.7中,Antlr4解析器在放错位置的标记上过早结束



我遇到了一个问题,如果我的解析器发现一个无法放入任何规则中的令牌,它将在不显式报告错误的情况下结束,即使之后还有更多的令牌要放入。确切地说,令牌实际上是被识别的(我有一个几乎包罗万象的规则(,但令牌放错了地方,任何规则都无法覆盖。在这种情况下,我的解析器成功地结束了,没有报告任何错误(至少大声报告(。

我看到的情况如下:要解析的代码:

.class public final Ld;
.super Ljava/lang/Object;
.source "java-style lambda group"
# interfaces
.implements Landroid/content/DialogInterface$OnClickListener;
<misplaced-tokens>
# static fields
.field public static final f:Ld;
.field public static final g:Ld;
...

(注意<misplaced-tokens>令牌,它实际上是五个令牌-见下文。我预计这里的解析会出错。(

分析的令牌:

[@0,0:5='.class',<'.class'>,1:0]
[@1,7:12='public',<'public'>,1:7]
[@2,14:18='final',<'final'>,1:14]
[@3,20:22='Ld;',<QUALIFIED_TYPE_NAME>,1:20]
[@4,24:29='.super',<'.super'>,2:0]
[@5,31:48='Ljava/lang/Object;',<QUALIFIED_TYPE_NAME>,2:7]
[@6,50:56='.source',<'.source'>,3:0]
[@7,58:82='"java-style lambda group"',<STRING_LITERAL>,3:8]
[@8,85:96='# interfaces',<LINE_COMMENT>,channel=1,5:0]
[@9,98:108='.implements',<'.implements'>,6:0]
[@10,110:158='Landroid/content/DialogInterface$OnClickListener;',<QUALIFIED_TYPE_NAME>,6:12]
[@11,160:160='<',<'<'>,7:0]
[@12,161:169='misplaced',<IDENTIFIER>,7:1]
[@13,170:170='-',<'-'>,7:10]
[@14,171:176='tokens',<IDENTIFIER>,7:11]
[@15,177:177='>',<'>'>,7:17]
[@16,180:194='# static fields',<LINE_COMMENT>,channel=1,9:0]
[@17,196:201='.field',<'.field'>,10:0]
...

分析进度:

enter   parse, LT(1)=.class
enter   statement, LT(1)=.class
enter   classDirective, LT(1)=.class
consume [@0,0:5='.class',<30>,1:0] rule classDirective
enter   classModifier, LT(1)=public
consume [@1,7:12='public',<53>,1:7] rule classModifier
exit    classModifier, LT(1)=final
enter   classModifier, LT(1)=final
consume [@2,14:18='final',<56>,1:14] rule classModifier
exit    classModifier, LT(1)=Ld;
enter   className, LT(1)=Ld;
enter   referenceType, LT(1)=Ld;
consume [@3,20:22='Ld;',<1>,1:20] rule referenceType
exit    referenceType, LT(1)=.super
exit    className, LT(1)=.super
exit    classDirective, LT(1)=.super
exit    statement, LT(1)=.super
enter   statement, LT(1)=.super
enter   superDirective, LT(1)=.super
consume [@4,24:29='.super',<33>,2:0] rule superDirective
enter   superName, LT(1)=Ljava/lang/Object;
enter   referenceType, LT(1)=Ljava/lang/Object;
consume [@5,31:48='Ljava/lang/Object;',<1>,2:7] rule referenceType
exit    referenceType, LT(1)=.source
exit    superName, LT(1)=.source
exit    superDirective, LT(1)=.source
exit    statement, LT(1)=.source
enter   statement, LT(1)=.source
enter   sourceDirective, LT(1)=.source
consume [@6,50:56='.source',<32>,3:0] rule sourceDirective
enter   sourceName, LT(1)="java-style lambda group"
enter   stringLiteral, LT(1)="java-style lambda group"
consume [@7,58:82='"java-style lambda group"',<304>,3:8] rule stringLiteral
exit    stringLiteral, LT(1)=.implements
exit    sourceName, LT(1)=.implements
exit    sourceDirective, LT(1)=.implements
exit    statement, LT(1)=.implements
enter   statement, LT(1)=.implements
enter   implementsDirective, LT(1)=.implements
consume [@9,98:108='.implements',<31>,6:0] rule implementsDirective
enter   implementsName, LT(1)=Landroid/content/DialogInterface$OnClickListener;
enter   referenceType, LT(1)=Landroid/content/DialogInterface$OnClickListener;
consume [@10,110:158='Landroid/content/DialogInterface$OnClickListener;',<1>,6:12] rule referenceType
exit    referenceType, LT(1)=<
exit    implementsName, LT(1)=<
exit    implementsDirective, LT(1)=<
exit    statement, LT(1)=<
exit    parse, LT(1)=<

(观察解析是如何成为主要规则的,并且实际上在这里退出,即使管道中还有很多令牌(

我尝试过的:

我尝试重新实现默认的错误策略和错误侦听器,并将两者都添加到lexer和解析器中,只是想看看这些断点是否会被命中。任何和所有重写方法的断点都不会被命中(有时reportAttemptingFullContext除外(。

这就是我添加覆盖的方式:

def parseFile(self, filePath):
errorListener = MyErrorListener()
strategy = MyErrorStrategy()
file = FileStream("file.smali")
lexer = SmaliLexer(file)
lexer.removeErrorListeners()
lexer.addErrorListener(errorListener)
lexer.addErrorListener(strategy)
stream = CommonTokenStream(lexer)
parser = SmaliParser(stream)
parser.removeErrorListeners()
parser.addErrorListener(errorListener)
parser.addErrorListener(strategy)
tree = parser.parse()
...

我的设置如下:

Windows 10 OS
Python 3.7
Antlr4 v4.8 - antlr-4.8-complete.jar
pip-installed runtime: antlr4_python3_runtime-4.8-py3-none-any.whl

我真的很感激关于如何让Antlr4真正考虑到重写的侦听器和策略的任何帮助,这样我既可以报告错误进行调试,也可以以不同的方式处理它们。谢谢

Antlr4解析器提前结束

当您调用的规则parse不是";"锚定";通过内置的EOF令牌:

parse
: expression
;
expression
: expression '+' expression
| NUMBER
;

在上面的情况下,当输入是1+2 3时,生成的解析器将愉快地解析1+2

如果您想强制解析器使用输入流中的所有令牌,请将EOF添加到您的启动规则中:

parse
: expression EOF
;

相关内容

  • 没有找到相关文章

最新更新