Flex 和 Bison 编译器在遇到语法错误时不会显示词法错误
我正在使用Flex和Bison编码编译器,我对Yyerror(char*)和一些printf()消息进行了编码,以显示词汇和语法错误。我通过在文件“ programm.txt”中添加一些错误来测试我的代码,以显示应显示的错误。 (例如:int x = 89754545
是一个词汇错误,因为我定义了对整数的长度限制(最大= 5的长度))
我遇到的问题:
所有词汇错误都会出现一个接一个,但是当编译器遇到语法错误时:
他显示了第一个语法错误之前发生的所有词汇错误。
他显示了他遇到的第一个语法错误。
然后,他停止编译而没有显示其他错误,无论它们是词汇还是语法错误。
这是我的代码:
Lexical.l:
I'm coding a compiler using Flex and Bison, I coded yyerror(char*) and some printf() messages to show lexical and syntax errors. I tested my code by adding some errors in my file "programm.txt" in order to show the errors that should be displayed. (for example: int x = 89754545
is a lexical error that will be displayed because I defined a length limit to integers (max = 5 in length) )
The problem I met:
All lexical errors appear one after the other, but when the compiler meets a syntax error :
He shows all lexical errors that occur before the first syntax error.
He shows the first syntax error he meets.
And then he stops compiling without showing the other errors whether they are lexical or syntax errors.
Here's my code:
lexical.l:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您将错误恢复作品添加到语法中,否则野牛会在遇到语法错误时停止解析。因此,它不会遇到更多错误。它只是返回错误值(1)。
此外,当解析器停止解析时,它停止向扫描仪询问令牌。因此,将不再阅读输入,也不会发现更多的词汇错误。
错误恢复并不容易。您可能应该首先要使Lexer和Parser首先处理正确的输入。一旦您了解语法和词汇扫描在实践中的工作原理,则可能会更轻松地添加信息性错误消息。从简短的,集中的程序(和语法)开始几乎总是更容易的,而不是生产数百行的代码,它们的互动您不完全理解。
《野牛手册》中有一章,涉及。您绝对应该阅读它,但也可以阅读一些示例,这些示例显示了如何做简单的错误恢复和相关的说明。在《野牛手册》中确实有很多有用的信息。
Unless you add error recovery productions to your grammar, bison will just stop parsing when it encounters a syntax error. So it won't encounter any more errors. It just returns an error value (1).
Furthermore, when the parser stops parsing, it stops asking the scanner for tokens. So no more input will be read, and no more lexical errors will be discovered.
Error recovery is not easy. You probably should work on getting your lexer and parser working on correct inputs first. Once you understand how grammars and lexical scanning works in practice, you'll probably have an easier time adding informative error messages. It's almost always easier to start with short, focussed programs (and grammars) rather than producing hundreds of lines of code whose interactions you don't fully understand.
There is a chapter in the Bison manual about error recovery. You should definitely read it, but also read through some of the examples which show how to do simple error recovery, and the related explanations. There really is quite a lot of useful information in the Bison manual.