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:
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.