[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RFC: generate the default semantic action
From: |
Akim Demaille |
Subject: |
Re: RFC: generate the default semantic action |
Date: |
Tue, 16 Oct 2018 07:38:06 +0200 |
> Le 16 oct. 2018 à 07:13, Akim Demaille <address@hidden> a écrit :
>
> Hi Rici,
>
>> This patch won't affect programs which rely on this feature, as far as I
>> can see. But I wanted to highlight the issue because the road that follows
>> this patch might inadvertently affect existing programs which count on the
>> default action to be a copy of the *entire* semantic value.
>
> No, I don’t think it changes anything, indeed. Even type checking
> is run before adding the explicit default action. Your example
> runs fine.
Scratch that, my experiments were broken. Like C’s handling of arrays.
This language treats arrays in a special way, forbidding assignments
between arrays, but accepting them happily if the arrays are members
of structs and you assign the structs.
Consider this:
%{
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void yyerror(const char* msg) { fprintf(stderr, "%s\n", msg); }
int yylex(void);
typedef char string[256];
%}
%union {
string str;
};
%token <str> WORD
%type <str> word
%%
prog: %empty
| prog word { printf("%s\n", $2); }
word: WORD
%%
int yylex(void) {
static int c = 0;
int val = c++;
if (val < 4)
{
sprintf(yylval.str, "%d", val);
return WORD;
}
else
return YYEOF;
}
int main(int argc, char** argv) { return yyparse(); }
The old bison is happy with it, and it generates valid C code.
$ /opt/local/bin/bison /tmp/foo.y && gcc-mp-7 foo.tab.c && ./a.out
0
1
2
3
The ‘new’ bison is happy with it, but generates invalid code.
$ ./_build/8s/tests/bison /tmp/foo.y && gcc-mp-7 foo.tab.c
/tmp/foo.y: In function 'yyparse':
/tmp/foo.y:24:19: error: assignment to expression with array type
word: WORD
^
This example is dubious, is the user really likely to expect
word: WORD
to behave properly without strncpy? In practice, it does, but…
So I think I will (for a start?) restrict this change to only
C++ with variants.
WDYT?
- Re: RFC: generate the default semantic action, (continued)
Re: RFC: generate the default semantic action, Paul Eggert, 2018/10/14
Re: RFC: generate the default semantic action, Rici Lake, 2018/10/15