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 07:43:02 +0200

Hi Daniele,

> Le 17 juin 2020 à 21:56, Daniele Nicolodi <daniele@grinta.net> a écrit :
> 
> Hello,
> 
> error reporting has always been a pain point for me in the use of flex
> and bison, especially in coordinated error handling between the scanner
> and the grammar. I am very happy to see a lot of improvements in this
> area in the 3.6 release!
> 
> Trying to use `%define parse.error custom` in a project I realized that
> it would be very handy to have the value of the token that caused the
> syntax error available in yyreport_syntax_error. Is there a way to get
> to it?

I think it's a mistake to try to use the semantic value in error messages.
It's often different from what the user wrote (think of escapes in strings,
non decimal integer literals, rounding errors in floats, etc.).  It can
also be nasty to display (e.g., super long string literals).  Also, if
you want to build an error message string, you'll have to face the problem
of converting the union that contains all the value types into the string
that corresponds to your current semantic value.

The right way to report more details to the user is to quote the source:

$ gcc-mp-10 /tmp/foo.c
/tmp/foo.c:1:12: error: expected ',' or ';' before string constant
    1 | int y = 12 "232";
      |            ^~~~~

Cheers!


reply via email to

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