Re: Trouble in Error Recovery

haberg@math.su.se (Hans Aberg)
23 Sep 2005 15:59:15 -0400

          From comp.compilers

Related articles
Trouble in Error Recovery sidharth.masaldaan@gmail.com (Sidharth) (2005-09-22)
Re: Trouble in Error Recovery haberg@math.su.se (2005-09-23)
| List of all articles for this month |

From: haberg@math.su.se (Hans Aberg)
Newsgroups: comp.compilers
Date: 23 Sep 2005 15:59:15 -0400
Organization: Mathematics
References: 05-09-103
Keywords: parse, errors

"Sidharth" <sidharth.masaldaan@gmail.com> wrote:


> I'm having trouble in writing error recovery routines. I spent the
> last days trying to make error recovery working but I
> can't catch one and yyerror is always invoked. I'm using Bison 2.0. n
> Flex 2.5.31.


Bison has a built in error recovery feature. The Bison manual
describes this. It is somewhat erratic, though, due to the fact that
LALR(1), relative LR(1), upon seeing an error token, may perform
further actions before reporting the error. So don't expect fine
tuned error recovery. You will have to experiment with the .y
grammar, to see what works.


> %token Max_Header_Token DIGITS Error_Token
>
> %%
> Max_Header: Max_Header_Token DIGITS {/*action*/}
> | Error_Token DIGITS { printf("ERROR in header\n"); }
> | Max_Header_Token Error_Token { printf("ERROR in DIGITS\n");
> }
> ;
> %%


You can write


> %token Max_Header_Token DIGITS Error_Token
>
> %%
> Max_Header: Max_Header_Token DIGITS {/*action*/}
> | error DIGITS { printf("ERROR in header\n"); }
> | Max_Header_Token error { printf("ERROR in DIGITS\n");
> }
> ;


with the "error" token thrown in here and there. Upon seeing an error,
the Bison generated parser will (see the Bison manual) unwind the
parser stack, until it sees an error recover state, and then perform
the rule action .


> Flex Source:-
>
> SPACE [ \t]*
> TOKEN "Max-Header"
> COLON ":"
> HEADER {SPACE}+ {TOKEN}{SPACE}+ {COLON}
> DIGITS [0-9]+
> Error_Token .+
>
> %%
> {HEADER} {return Max_Header_Token;}
> {DIGITS} {return DIGITS;}
> {Error} {return Error_Token;}
> %%


If you then return the Error_Token from the lexer, it will generate an
error, as it is not a part of the grammar. You could write
.. { return yytext[0]; }
as last rule. This means that the parser gets the token code which is the
character it found. If this character does not fit into the .y grammar via
a '...' character token construct, it will generate a pareser error, which
then can be picked up by the above mentioned "error" construct.


--
    Hans Aberg


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.