[antlr-interest] C runtime and parser error reporting confusion

Jim Idle jimi at temporal-wave.com
Mon Aug 10 16:03:55 PDT 2009


Adamic Tomislav wrote:
> Hi everybody...
>
> I'm trying to override displayRecognitionError in C runtime for my
> parser grammar. In my combined parser and lexer grammar file I have
> following:
>
> @parser::apifuncs {
> 	RECOGNIZER->displayRecognitionError = myDisplayRecognitionError;
> }
>
> In my code I have the implementation of myDisplayRecognitionError
> which currently doesn't do anything smart except that it notifies me
> it has been run:
>
> .h file:
>
> #ifdef __cplusplus
> extern "C" {
> #endif
>
> //...
> void myDisplayRecognitionError (pANTLR3_BASE_RECOGNIZER recognizer,
> pANTLR3_UINT8 * tokenNames);
> //...
>
> #ifdef __cplusplus
> }
> #endif
>
> .c file (compiled as C++):
>
> #include <stdexcept>
> //...
> void myDisplayRecognitionError (pANTLR3_BASE_RECOGNIZER recognizer,
> pANTLR3_UINT8 * tokenNames) {
>     // Just a notification for now...
>     throw std::logic_error();
> }
> //...
>
> Now, I have two test strings for my parser. Both should triger
> myDisplayRecognitionError. First one "somethingsomething" actually
> does. Second one being "something2something" passes without triggering
> myDisplayRecognitionError and gets parsed as "2" beacause my grammar
> recognizes numbers. This is not what I want, the second one should
> trigger error also. Things marked here as "something" can be anything
> that is not defined by my grammar. I've been searching through
> mailinglist archives but I just can't figure it out... What am I doing
> wrong?
>   
I suspect that this is because of your grammar and nothing to do with 
the recognition error override. Fix your lexer/parser and you will get 
an error. For instance, do you have an EOF terminating the grammar rule 
you are calling? If not then ANTLR will just not recognize a token in 
error at certain points and stop. If you do have the EOF, then you must 
have some sequence that allows numbers, and either unrecognized tokens 
are coming through on a different channel or are being thrown out by the 
lexer and you are not ignoring lexer errors (you should not allow the 
lexer to generate errors in general).

You need to post your grammar, not your code to get some help here.

Jim


More information about the antlr-interest mailing list