help-bison
[Top][All Lists]
Advanced

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

Error recovery in C++ with Bison 2.3


From: Bill Lear
Subject: Error recovery in C++ with Bison 2.3
Date: Tue, 2 Jan 2007 16:04:00 -0600

I have written a rather extensive parser in C++, using Bison 2.3,
basing it on the example, calc++, provided with the Bison distribution.

I would like to be able to recover from simple errors in the input
file by making note of them, providing a warning, and continuing on.

However, the documented way to do this is to use the yyerrok macro,
and this seems to have completely disappeared and I cannot figure out
why nor how to conjure it into existence.

Here is how I am invoking bison:

  bison -p lti -S lalr1.cc -o lti-parser.cc lti-parser.yy

and in lti-parser.yy, I have the following directives:

%skeleton "lalr1.cc" /*  -*- C++ -*- */
%require "2.1a"

copied directly from the calc++ example.

The vast majority of the input errors can be handled by a simple rule
that is essentially "If you encounter an error, just read ahead to the
next semi-colon, print a message, and continue".

So, one construct in the language is this:

    CLASS <class_name> ;

my rule for this in my grammar is this:

class: CLASS PID SEMI {
    set_pid($2);
} CLASS POD SEMI {
    set_pod($2);
}

This grammar correctly parses:

    CLASS PID;
    CLASS POD;

but fails on this:

    CLASS pid;

So, I thought I could add error recovery like this:

class: CLASS PID SEMI {
    set_pid($2);
} | CLASS POD SEMI {
    set_pod($2);
} | error SEMI {
    report_error("CLASS");
    yyerrok;
}

But, the yyerrok macro seems to have disappeared, and I get a compile
error.

Removing yyerrok from the above allows me to compile, but I get no
error report --- the "error SEMI" part seems to do nothing, and I
simply get a syntax error and bison quits.

If anyone could help me with this, I would greatly appreciate it.

Thanks.


Bill




reply via email to

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