简体   繁体   English

野牛:一个错误会导致其他但不正确的错误

[英]bison: one error causes additional but incorrect error

In the class section of my code, if I write an error a few lines will be reported as errors when they should not. 在我的代码的类部分中,如果我写了一个错误,则在不应该的情况下将报告几行错误。 I put ' | error 我把' | error | error ' in certain locations where it's good/safe to recover from errors but I don't think it's using it. | error '可以从错误中恢复的很好/安全的某些位置,但我认为它没有使用它。 Maybe it's trying to resume mid expression someplace? 也许它正试图在某个地方恢复中间表情?

Is there a way I can force Bison to try to recover in designated locations? 有没有一种方法可以迫使野牛尝试在指定的位置恢复? How does it work and what might I be doing wrong? 它是如何工作的,我可能做错了什么?

I put the error line next to the loop. 我将错误行放在循环旁边。 Here is an example: 这是一个例子:

SomeRuleLoop:
    | Rule ',' SomeRuleLoop
Rule:
      A
    | B
    | C
    | Error
A:
      AA AAB AABC
    | AA AAB AABC Z
...

Here is an example of my rules. 这是我的规则示例。 I see "funcBody error" in my console however the next line gets an error because of the first error. 我在控制台中看到“ funcBody错误”,但是由于第一个错误,下一行出现了错误。 Even though each funcBodyRule is standalone. 即使每个funcBodyRule是独立的。

funcBodyLoop:
    | funcBodyLoop funcBody

funcBody:
      funcBodyRule
    | error { printf("funcBody error"); $$=0; }
    | '#' EQ { printf("still in funcBody\n"); $$=0; }

I tried writing #== between the line with the first error and the line with the 2nd. 我尝试在第一个错误的行和第二个错误的行之间编写#==。 I wrote this to check if the parser is still in the funcbody loop. 我写这个来检查解析器是否仍然在funcbody循环中。 This doesnt give an error so it is . 这不会给出错误,而是 Nevermind i added a printf and the string isnt printed so maybe it isnt in the function loop anymore? 没关系,我添加了printf并且未打印字符串,所以也许它不再存在于函数循环中? how do i fix this? 我该如何解决?

Yacc and Bison normally use left-recursive rules, and the rules shown are not left-recursive. Yacc和Bison通常使用左递归规则,显示的规则不是左递归。

As shown, the first rule is equivalent to: 如图所示,第一个规则等效于:

SomeRuleLoop:
        /* Nothing */
    |   Rule ',' SomeRuleLoop
    ;

This is a right-recursive rule which says that a 'SomeRuleLoop' is either an empty string of tokens or a 'Rule' followed by a comma and some more 'SomeRuleLoop'. 这是一条右递归规则,它表示“ SomeRuleLoop”是一个空的令牌字符串或“ Rule”,后跟一个逗号和更多的“ SomeRuleLoop”。 Note that this means a 'SomeRuleLoop' ends with a comma, which is probably not what you had in mind. 请注意,这意味着“ SomeRuleLoop”以逗号结尾,这可能与您的想法不符。

The first rule should probably read: 第一条规则应为:

SomeRuleLoop:
        Rule
    |   SomeRuleLoop ',' Rule
    ;

Note that allowing for empty alternatives is important - but adding them everywhere tends to make the grammar ambiguous (more shift/reduce conflicts) 请注意,允许使用空的替代品很重要-但是将其添加到各处往往会使语法变得模棱两可(更多的移位/减少冲突)


You also need to use the token 'error' (all lower case) rather than 'Error' (mixed case) to indicate a point where error recovery can occur. 您还需要使用标记“错误”(所有小写)而不是“错误”(混合大小写)来指示可能发生错误恢复的点。

However, I'm not sure what the rest of your troubles are... 但是,我不确定您其余的麻烦是什么...

Forcing ';' 强制';' or newlines at the end of the error solves it. 或错误末尾的换行符可以解决该问题。 ( | error my_end_of_statenent instead of | error ) | error my_end_of_statenent而不是| error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM