List Info

Thread: pure-parser warning: ‘yylval’ is used uninitialized in this function




pure-parser warning: ‘yylval’ is used uninitialized in this function
country flaguser name
Canada
2008-05-08 03:20:02
Hello,

GCC with "-Os -Wall -Werror" complains in the
case of pure-parser, while
without -Os it works well. Any help? Thanks in advance.

$ bison -o parse.c parse.y
$ gcc -Os -Wall -Werror  -c parse.c -o parse.o
cc1: warnings being treated as errors
parse.c: In function ‘yyparse’:
parse.c:1247: warning: ‘yylval’ is used uninitialized in
this function

The generated problem code snippet is:

1246   yystate = yyn;
1247   *++yyvsp = yylval;
1248 
1249   goto yynewstate;
1250 
1251 
1252
/*----------------------------------------------------------
-.
1253 | yydefault -- do the default action for the current
state.  |
1254
`-----------------------------------------------------------
*/
1255 yydefault:



$gcc -Wall -Werror  -c parse.c -o parse.o
(compiles well)

$ uname -a
Linux test64 2.6.21-2950.fc8xen #1 SMP Tue Oct 23 12:23:33
EDT 2007
x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying
conditions.  There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR
PURPOSE.
$ bison --version
bison (GNU Bison) 2.3
Written by Robert Corbett and Richard Stallman.

Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying
conditions.  There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR
PURPOSE.


The source code is 

$ cat parse.y
%{
#define YYSTYPE int

static int yylex(YYSTYPE *lvalp);
static int yyerror(char const *s);
%}
%pure-parser
%start t
%%

t: 'a'

%%

static int yylex(YYSTYPE *lvalp)
{
    return YYEOF;
}

static int yyerror(char const *s)
{
       return 0;
}

int main()
{
    return yyparse();
}



Thanks,
Kaiwang



_______________________________________________
help-bisongnu.org http
://lists.gnu.org/mailman/listinfo/help-bison

Re: pure-parser warning: ‘yylval’ is used uninitialized in this function
country flaguser name
United States
2008-05-10 18:43:29
On Thu, 8 May 2008, Kaiwang Chen wrote:

> GCC with "-Os -Wall -Werror" complains in
the case of pure-parser, while
> without -Os it works well. Any help? Thanks in
advance.
> 
> $ bison -o parse.c parse.y
> $ gcc -Os -Wall -Werror  -c parse.c -o parse.o
> cc1: warnings being treated as errors
> parse.c: In function ‘yyparse’:
> parse.c:1247: warning: ‘yylval’ is used
uninitialized in this function

According to the gcc documentation, -Wuninitialized (implied
by -Wall) is 
affected by the optimization level.

> static int yylex(YYSTYPE *lvalp)
> {
>     return YYEOF;
> }

The warning seems legitimate to me because you're not
setting *lvalp in 
yylex.

If yylex is not declared static, the warning goes away. 
I'm thinking that 
gcc believes yylex is capable of initializing yylval, but
gcc gives up 
when it sees that yylex could have external linking.

When I keep yylex static and initialize *lvalp within it,
the warning line 
number moves to 1035, which is the declaration of yylval.  I
don't 
understand what that means.  I would've guessed the warning
should always 
have the line number of a usage of yylval.  Maybe gcc has a
bug.

I don't have time to pursue this further right now.  Maybe
someone can 
take this to the gcc maintainers for advice.  If you find an
answer, 
please let us know.

As a workaround, you can add this to your definitions
section:

  %initial-action { $$ = 0; }
_______________________________________________
help-bisongnu.org http
://lists.gnu.org/mailman/listinfo/help-bison
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )