[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Best Practice: lexer error to silent parser abort
From: |
Hans Åberg |
Subject: |
Re: Best Practice: lexer error to silent parser abort |
Date: |
Wed, 2 Jan 2019 10:37:45 +0100 |
> On 1 Jan 2019, at 23:19, Simon Richter <address@hidden> wrote:
>
> Hi,
>
> I'm wondering what would be the best way to communicate from the lexer
> to the parser that the lexer detected something it could not tokenize
> (in my case, characters outside the allowed character set).
>
> In this case, it is fairly obvious that we want to abort, without a call
> to yyerror, but properly destructing and deallocating any values still
> on the stack.
>
> The best way I can think of at the moment would be to define an
> additional token type that isn't used in any rule, return that from the
> lexer, and create a shortcut between lexer and parser that silences yyerror.
>
> Is there a better way to do this?
If you have a rule at the start symbol
| error {
/* Cleanup stuff */
YYABORT;
}
and return a token value, like the character code, not otherwise recognized in
the grammar, then the stack will be properly unwound (in the C parser grammar
maybe using %destructor). One can get better error messages by actually
defining a token, I think, Akim had some example of that, maybe later versions
of the C++ calculator example.