help-bison
[Top][All Lists]
Advanced

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

error matches first error token twice


From: Tim Van Holder
Subject: error matches first error token twice
Date: Tue, 27 May 2003 11:34:46 +0200

It seems that an 'error' rule matches the first error token twice.
Or rather, it matches the error token and a copy of it.
In particular if I use "free ($<ptr>1); $<ptr>1 = NULL;" as action
in an error rule, I will still get the freed pointer as $1 right
away, making it nearly impossible to use an error rule for cleanup.

Example:

[--Start foo.l--]
%{
extern int yylval;
%}

%%

[0-9] {
  yylval = yytext[0] - '0';
  return yytext[0];
}

%%

int yyerror(char* msg) { return 0; }
[--End foo.l--]

[--Start foo.y--]
%union { int integer; }

%start answers

%%

answers
: answers one_answer
| one_answer
;

one_answer
: '4' '2' { printf("Saw 42.\n"); }
| error { printf("Saw '%d' as error token.\n", $<integer>1); }
;

%%

int
main(void)
{
  yyparse();
}
[--End foo.y--]

With this simplistic input, entering '123' prints

Saw '1' as error token.
Saw '1' as error token.
Saw '2' as error token.
Saw '3' as error token.

Is there any way to avoid this behaviour?
Also, if '45' is entered, '5' is seen by the error rule (twice),
but '4' isn't; can I assume in such a case its %destructor is called?





reply via email to

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