bison-patches
[Top][All Lists]
Advanced

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

Re: RFC: propagate the indentation of the actions


From: Akim Demaille
Subject: Re: RFC: propagate the indentation of the actions
Date: Sun, 16 Jun 2019 15:43:07 +0200

Hi Paul,

> Le 14 juin 2019 à 19:59, Paul Eggert <address@hidden> a écrit :
> 
> On 6/12/19 10:48 PM, Akim Demaille wrote:
>> I do not see how I could help the compiler produce accurate diagnostics: 
>> there's no #column (or better, _Pragma) that I know of, that we could use to 
>> insert accurate location information within the line.
> 
> If the generated token's column is too small, can't you output white space to 
> make it large enough; and if it's too large, can't you output a newline, the 
> same "#line" directive as before, and you're back to column 1 so you can now 
> output white space to make the column large enough?

Interesting idea!

It's not perfect, but it's an improvement.  This input:

> | exp "+" exp   { $$ = $<char*>1 / $<char*>3; }

generates

>   case 9:
> #line 104 "reccalc/parse.y"
>                 { (yyval.TOK_exp) = (*(char**)(&yyvsp[-2])) / 
> (*(char**)(&yyvsp[0])); }
> #line 1334 "reccalc/parse.c"
>     break;

so the original error message is

> reccalc/parse.y:104:61: error: invalid operands to binary / (have 'char *' 
> and 'char *')
>   104 | | exp "+" exp   { $$ = $<char*>1 / $<char*>3; }
>       |                                     ~~~~~~~~~~~             ^         
>              
>       |                                      |                         |
>       |                                      char *                    char *

where we can see that gcc meant to show both operands, since they are on the 
same line.

I tweaked the generated code as follows:

>   case 9:
> #line 104 "/Users/akim/src/gnu/bison/examples/c/reccalc/parse.y"
>                 { (yyval.TOK_exp)
> #line 104 "/Users/akim/src/gnu/bison/examples/c/reccalc/parse.y"
>                      = (*(char**)(&yyvsp[-2]))
> #line 104 "/Users/akim/src/gnu/bison/examples/c/reccalc/parse.y"
>                                  / (*(char**)(&yyvsp[0]))
> #line 104 "/Users/akim/src/gnu/bison/examples/c/reccalc/parse.y"
>                                             ; }
> #line 1334 "examples/c/reccalc/parse.c"
>     break;

on which GCC generates:

> reccalc/parse.y:104:34: error: invalid operands to binary / (have 'char *' 
> and 'char *')
>   104 | | exp "+" exp   { $$ = $<char*>1 / $<char*>3; }
>       |                                  ^ ~~~~~~~~~~~~          
>       |                                     |
>       |                                     char *

which is better but not correct: it underlines too much as it's using the 
generated width instead of the source one; this would be even worse looking 
with $1 instead of this long $<char*>1.  Also, it no longer tries to underline 
the lhs, because, I think, it's too width compared to the start column of the 
rhs.

In an ideal world, one would expect

> reccalc/parse.y:104:34: error: invalid operands to binary / (have 'char *' 
> and 'char *')

>       | | exp "+" exp   { $$ = $<char*>1 / $<char*>3; }
>       |                        ~~~~~~~~~ ^ ~~~~~~~~~                     
>       |                        |           |
>       |                        char *      char *

but without new specific support from the compiler, I don't see how to do that 
(except playing dirty games with intermediate variables, but I feel 
uncomfortable with this).


Meanwhile, I think your idea does bring an improvement, WDYT?  I'll try to work 
on it.

Thanks!


reply via email to

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