Parser error handling

"Piotr Wyderski" <>
20 Aug 2003 01:27:52 -0400

          From comp.compilers

Related articles
Parser error handling (Piotr Wyderski) (2003-08-20)
Re: Parser error handling (2003-08-23)
Re: Parser error handling (Piotr Wyderski) (2003-08-30)
Re: Parser error handling (2003-09-04)
| List of all articles for this month |

From: "Piotr Wyderski" <>
Newsgroups: comp.compilers
Date: 20 Aug 2003 01:27:52 -0400
Organization: tp.internet -
Keywords: errors
Posted-Date: 20 Aug 2003 01:27:52 EDT


I'm writing a translator for an experimental language using Bison and
Flex. The "right" part of it works more or less correctly, so I'd like
to implement the "wrong" part, i.e. error handling. All I need is to
stop parsing after an error has been detected and display an
appropriate message. I do not want to "fake" the parser and resume
parsing (as most C++ compilers do), because I consider such a feature
to be pure evil. ;-) The position tracking mechanism is working
correctly, so I always know where the error is.

One of my problems is to report a lexical error and stop the parser
immediately after that. For example, there's a statement which allows
the user to input an integer encoded in a specified base. It's
"base_string#value_string". E.g. 16#ff is 255, 3#11 is 4, 10#123 is
123. The parser expects constant integers, so all possible value
encodings (this one among other things) are decoded by the lexical
analyser. When it's incorrect, say, 10#12A ('A' is a wrong digit for a
given base) or 157#14 (only the bases 2,...,26 are valid) it should
report a lexical error and stop parsing. It should be decoded at the
lexical analysis stage, because those numbers could be very large (I
use GMP for them), so creating a string, passing it to the parser,
converting it into an integer and deleting the string surely is not an
economical solution. So, my first question is how to report lexical
errors and stop parsing correctly.

The second question is about cleaning the intermediate results during
failures. The parsing tree is composed of objects derived from the
Node class; the parser's stack stores pointers to that
objects. Provided semantic actions use that pointers as parameters,
create and initialize a new node using them and put it onto the stack
-- nothing unusual. When there's an error, the stack may contain
pointers to "intermediate" nodes. They should be deleted to prevent
memory leakage. How to assure that those objects will be destroyed?

Could you please show me a good flex/bison translator (i.e. its source
code) with correctly solved error reporting? I'd like to see a good
example of ER and finish my own translator. Thanks in advance.

        Best regards
        Piotr Wyderski

Post a followup to this message

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