Hi,

Transitions on declared, but unused tokens are not included into state
transitions. You normally would get $default transitions, but byacc
insists on specifying all tokens. This leads to wrong
results because a syntax-error is generated on a perfectly legal input.

Try this source, once without the tNL rule, and once with. A syntax
error is generated if the rule is not implemented. When the rule is
there, then the proper result is printed.

Version of byacc:
yysccsid[] = \"@(#)yaccpar     1.9 (Berkeley) 02/21/93\";"

The bug was also submitted to www.freebsd.org.


To compile (use attached source below):
$ byacc byacc-bug.y
$ gcc -o byacc-bug y.tab.c
$ ./byacc-bug

---- file byacc-bug.y ----
%{
#include <stdio.h>
#include <stdlib.h>
%}

%token tTOK tNUM tNL
%left '+'

%%
lines   : line
        | lines line
        ;

line    : tTOK xpr ',' xpr {
                if(yychar == tNL)
                        printf("Success: Got tNL\n");
        }
 /* Declare NON-terminal as used */
 /*     | tNL   */
        ;

xpr     : xpr '+' xpr
        | tNUM
        ;
%%

int yylex(void)
{
        static int tok[] = {tTOK, tNUM, ',', tNUM, tNL, 0};
        static int idx = 0;
#define NTOK    (sizeof(tok)/sizeof(tok[0]))

        if(idx < NTOK)
                return tok[idx++];
        return 0;
}

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

int main(void)
{
        return yyparse();
}

----------------Cut here------------------

Possible work-arround:
You have to define a rule that captures all non-terminal tokens (i.e.
all tokens in %token, %left and %right declarations that are otherwise
unused). This can be tricky if such a rule has grammatical side-effects.

A real fix requires a patch in byacc (but I am not familiar with its
internals).

Greetings Bertho

Reply via email to