Antlr 4中可选规则的语法错误无法正常工作 [英] Syntactic errors on optional rules in Antlr 4 doesn't work as expected

查看:176
本文介绍了Antlr 4中可选规则的语法错误无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Antlr 4.7.2.我正在尝试执行"if else"语句:

I am using Antlr 4.7.2. I am trying to implement an "if else" statement:

主要问题是ParseTree中未包含可选规则,因此我认为该可选规则没有语法错误.

Main problem is that optional rule is not being included on ParseTree, for this reason I think I am not getting the syntax's errors on that optional rule.

我当前的语法定义是:

 prog       : stat+ ;                   

 stat       : func_declaration                              #rFuncDeclStat
            | if_stat                                       #rIfStat
            | while_stat                                    #rWhileStat     
            | for_stat                                      #rForStat
            | 'return' expr? STAT_END                       #rReturnStat
            | LET ID ('=' expr)? STAT_END                   #rVarDeclStat
            | var_reference '=' expr STAT_END               #rAssignStat
            | print_stat                                    #rPrintStat                     
            | expr STAT_END                                 #rFuncCallStat
            ;

block_stat  : '{' stat* '}' ;

if_stat     : if '(' expr ')' (stat | block_stat) else_stat?;

else_stat   : ELSE (stat | block_stat) ;

当我语法正确地编写代码并使用"org.antlr.v4.gui.TestRig"运行语法时,一切都很好:

Everything work good when and I write the code correct syntactically and run grammar using "org.antlr.v4.gui.TestRig":

if (2==2){
    let a = 4;
}
else{
    let b = 5;  //var declaration
}

但是当我编写下一个代码时,"else_stat"规则未包含在ParseTree结果中,并且Antlr4不会报告任何语法错误.

But when I wrote next code, "else_stat" rule is not included on the ParseTree result and Antlr4 doesn't report any syntactic error.

if (2==2){
    let a = 4;
}
else{
    let b = 5;

如果我删除?"根据"else_stat"规则(使其成为强制性规则),Antlr4在ParseTree中包含"else_stat",并且能够识别错误并显示相应的消息:缺少'}'".

If I remove "?" from "else_stat" rule (making it mandatory), Antlr4 include "else_stat" at ParseTree and is able to identify the error and show the corresponding message: "Missing '}'".

请,有人可以在正确的地址指引我吗?我需要知道如何使Antlr4显示语法错误,而不管它是否发生在可选规则上,或者是否需要修复我的语法定义或其他内容.

Please, anybody could guide me in the right address? I need to know how to make Antlr4 show the syntactic error regardless it happens on optional rules or if I need to fix my grammar definition or something else.

谢谢.

推荐答案

通常,如果输入流包含的输入多于调用规则所要求的输入,则这不是语法错误.只是将附加输入保留在流中.例如,这使您可以循环调用规则.

Normally it is not a syntax error if an input stream contains more input than what is required by the rule you invoke. The additional input is simply left in the stream. This allows you to invoke rules in a loop for example.

因此,如果在输入if (2==2){ let a = 4; } else { let b = 5;上调用prog规则,则if (2==2){ let a = 4; }部分将被解析为if语句,而没有else且else { let b = 5;将保留在输入缓冲区中.

So if you invoke your prog rule on the input if (2==2){ let a = 4; } else { let b = 5;, the if (2==2){ let a = 4; } part will be parsed as an if-statement without else and else { let b = 5; will remain in the input buffer.

由于这不是您想要的,因此应在prog规则的末尾添加EOF:

Since that's not what you want, you should add EOF at the end of your prog rule:

prog       : stat+ EOF;

EOF告诉ANTLR,只有在语法解析后输入中什么都没有的情况下,程序才在语法上有效.这将为您提供输入所需的语法错误.

The EOF tells ANTLR that a program is only syntactically valid if there's nothing left in the input after parsing it. This will give you the syntax error you want for your input.

这篇关于Antlr 4中可选规则的语法错误无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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