野牛-从其他错误中恢复 [英] Bison - recover from If Else error

查看:114
本文介绍了野牛-从其他错误中恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从If-Else语句中的错误中恢复. 在我的语法中,If后面总是一个Else.

I'm trying to recover from an error in an If-Else statement. In my grammar an If is always followed by an Else.

statement:      OBRACES statements CBRACES
    |       IF OPAR exp CPAR statement ELSE statement
    |       IF OPAR exp CPAR statement error '\n'       { yyerrok; yyclearin;}
    ;

发现的错误在最后几行的注释中:

The error found is in the commented else in the last lines:

public boolean Equal(Element other){

    if (!this.Compare(aux01,Age))
        ret_val = false ;
    //else 
        //nt = 0 ;
}

错误:语法错误,意外的大括号,期待ELSE->} @第29行

error: syntax error, unexpected CBRACES, expecting ELSE -> } @ line 29

忽略后继错误,并不能从该错误中恢复.

It is not recovering from that error ignoring the errors that come after.

也许我不太了解此错误的工作原理,但我只能在每个站点上找到有关错误恢复的两个示例:错误'\ n'"和'('错误')'"

Maybe i'm not understanding well how this error works but i can only find 2 examples on every site about error recovery: "error '\n'" and "'(' error ')'"

任何人都知道如何从此错误中恢复(当if后面没有else时).

Anyone have an idea how to recover from this error (when an if is not followed by an else).

谢谢

推荐答案

可能(尽管很难确定,但看不到更多语法),在else的情况下,您无需跳过任何标记找不到.

Probably (although it is hard to say for sure without seeing more of the grammar) you don't need to skip any tokens in the case that else is not found.

最可能的情况是该程序只是缺少else子句(可能是因为其作者习惯于其他编程语言(其中else是可选的)),并且解析可以简单地继续进行,就好像有一个空值一样. else子句.因此您应该可以使用:

The most likely case is that the program is simply lacking an else clause (perhaps because its author is used to other programming languages in which else is optional), and parsing can simply continue as though there were an empty else clause. So you should be able to just use:

IF OPAR exp CPAR statement error       { yyerrok; }

(注意:,我删除了yyclearin是因为您几乎肯定不想这样做.在OP中发生错误的情况下,结果将是忽略'}'令牌,导致稍后在解析中产生无关的错误.)

(Note: I removed yyclearin because you almost certainly don't want to do that. In the case of the error in the OP, the result would be to ignore the '}' token, leading to extraneous errors later in the parse.)

您可能应该利用此错误产生中的操作来产生清晰的错误消息("if语句必须具有else子句"),尽管默认消息也相当清晰.

You probably should take advantage of the action in this error production to produce a clear error message ("if statements must have else clauses"), although the default message is reasonably clear as well.

在某些情况下,扫描程序必须能够生成任何用作错误上下文的令牌.除了换行在语法上很重要的语言之外,这通常排除了诸如跳到行尾"之类的错误恢复技术.

It is certainly the case that whatever token(s) are used as error context must be produceable by the scanner. That generally precludes error recovery techniques such as "skip to the end of the line", except in the case of languages in which newlines are syntactically significant.

这篇关于野牛-从其他错误中恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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