help-bison
[Top][All Lists]
Advanced

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

incorrect yychar for unambiguous GLR


From: Joel E. Denny
Subject: incorrect yychar for unambiguous GLR
Date: Tue, 19 Jul 2005 16:50:51 -0400 (EDT)

Should yychar be reliable in semantic actions when declaring %glr-parser?
It gives results that I do not expect for both bison 1.875 and 2.0.  I can
understand that delayed semantic actions might cause trouble in the case
of ambiguous grammars (although it would be nice if they didn't).
However, for unambiguous grammars I expect results identical to that of an
LALR(1) parser.

Here's a simple bison spec that demonstrates the problem.  The
bison-generated code is a complete C program with yylex(), yyerror(), and
main() already defined, so it is easy to try out:

  %{
    #include <stdio.h>
    void yyerror( char * msg );
    int yylex();
  %}

  %glr-parser /* Changes the value of yychar. */

  %%

  S:
    'a' {
      if ( yychar == YYEMPTY ) { printf( "yychar=YYEMPTY\n" ); }
      else { printf( "yychar='%c'\n", yychar ); }
    }
    ;

  %%

  void yyerror( char * msg ) { printf( "%s\n", msg ); }

  int yylex() {
    static int once = 0;
    if ( !once ) { once = 1; return 'a'; }
    return 0;
  }

  int main() { yydebug = 1; yyparse(); return 0; }

If you remove the %glr-parser declaration, the output of the semantic
action is as expected:

  yychar=YYEMPTY

If not, it's:

  yychar='a'

Thanks.

Joel Denny





reply via email to

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