help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Token value in custom error reporting


From: Akim Demaille
Subject: Re: Token value in custom error reporting
Date: Thu, 18 Jun 2020 18:45:36 +0200

Daniele,

> Le 18 juin 2020 à 10:24, Daniele Nicolodi <daniele@grinta.net> a écrit :
> 
>> Would you have an example of what you mean?
> 
> Sure, but it is rather contrived :-)
> 
> I am working on a project that is based on Flex and Bison 3.4. The code
> goes through some contortions so that the lexer can report errors to the
> parser. I would like to make use of some of the features introduced in
> Bison 3.6 to try to avoid the most ugly ones.
> 
> In the existing code, on error the lexer emits a LEX_ERROR token. This
> results in a grammar error that triggers error recovery (good) but also
> in an extra error emitted by Bison (bad). Right now the code checks the
> error messages in yyerror() and suppresses the unwanted error reporting
> if it contains the string "LEX_ERROR".

That's the exact use case for returning YYerror from the scanner.

> Simply returning YYerror from the lexer on error would solve half of the
> problem in a straight forward way.

I don't see what is the other half.

> However, would like to avoid to have
> to change the token stream emitted by the lexer as it can be seen as API
> in this case. And it does not solve the (minor) issue of having error
> reporting code in two places.

In this case, it cannot be in a single place.  There are details in
the scanner that irrelevant to the parser.  It is really up to the
scanner to forge the error message.  It still can use yyerror to
emit it though.

> Did I say that the example is contrived?

:)

In your present case, the right answer is returning YYerror.  I don't see
a need for something else so far :(


reply via email to

[Prev in Thread] Current Thread [Next in Thread]