Related articles |
---|
Error with Bison spa@surya.trddc.ernet.in (1995-06-27) |
Solution to Error with Bison spa@research.trddc.ernet.in (1995-07-05) |
Newsgroups: | comp.compilers |
From: | spa@surya.trddc.ernet.in (Sanjeev P Athalye) |
Keywords: | yacc, question |
Organization: | Compilers Central |
Date: | Tue, 27 Jun 1995 04:25:52 GMT |
Hello,
I am working with Tata Research Development and Design Centre ( an R&D
division of Tata Consultancy Services), Pune (India). While using bison
version 1.22 for generating LALR(1) parser for COBOL, I came across a
peculiar behaviour of bison which differs from a) the behaviour of yacc
and b) what the user document for bison says.
In theory, when the parser comes across a syntax error, in order to avoid
a cascade of error messages, it does not report an error till it shifts
three tokens after the first error has been reported. Meantime, it
discards the tokens from the input stream.
The trace of bison-generated parser (obtained by turning on YYDEBUG
switch) indicates that after detecting an error for the first time, it
reports an error, goes to a state where there is a valid shift on error,
then it does say that "discarding the token ---" but in reality, it tries
to do a shift on this token (by standard panic mode error recovery) and
changes the current LR(0) state. Actually, it should discard the token and
it SHOULD REMAIN IN THE SAME STATE. As a result of this, it reports a
number of error messages for a single error in the input.
I will illustrate this with the following example:
%token A B C D E
%token PERIOD
%%
begin : a E { printf("done!\n"); }
;
a : b C D end
| b error { yyerror( "C and D expected"); recover_from_error(); } end
;
b : A B B
;
end : PERIOD /* "." */
;
%%
For an erroneous input " A B B G D . E ", yacc-generated parser correctly
reports a single error message as : "syntax error : C and D expected" and
discards the tokens G and D while remaining in the same LR(0) state.
The bison-generated parser says that it is discarding the tokens G and D
but in practice, it tries to take action on these tokens and as a result,
gives three error messages (since it reduces 3 times by the rule "a : b
error end") as follows:
syntax error : C and D expected
syntax error : C and D expected
syntax error : C and D expected
You can verify this by observing the trace of bison-generated parser by turning
on YYDEBUG switch while compiling xx.tab.c.
As a result of this behaviour of the bison-generated parser, not only the
errors are reported thrice but any error recovery action specified for the
error rule (rule a: b error end ; in the above example) gets executed
thrice.
After carefully examining and comparing the traces of the drivers of the
two parser generators yacc and bison, I found that in bison's driver, in
the block of the test condition if(yyerrstatus == 3) under the label
yyerrlab1, the last statement should be "goto yynewstate;" which is
missing in the current parser. After including that statement, my parser
generated by bison started behaving correctly.
I want to know if anyone else has faced this problem any time with the bison.
Regards,
Sanjeev
********************************************************************
Sanjeev Athalye
Tata Consultancy Services
1, Mangaldas Road, Pune - 411 050 (India) Mailing Address
91 212 622809 Phone (Office)
91 212 623713 Fax
spa@trddc.ernet.in E-mail
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.