[antlr-interest] Enhancing error reporting.

Bosko Ivanisevic ivanisev at sezampro.com
Sun Oct 28 01:48:27 PDT 2007


Hi everybody,

I have following rule in my grammar:
	
function
	:	FUNCTION return_type ID statement+ END -> ^(FNC_DEF ID ^(RETURN_TYPE
return_type) ^(BLOCK statement+))
	;

return_type
	:	NUMERIC_SIMPLE
	|	NUMERIC_SERIES
	|	BOOLEAN_SIMPLE
	|	BOOLEAN_SERIES
	|	STRING_SIMPLE
	|	STRING_SERIES
	;
FUNCTION
	:	'Function';


If I start parser on the input that doesn't have return_type:

Function foo
...
End

error I get from parser is:

ine 1:9 mismatched input 'foo' expecting set null

which is very cryptic for users. Obviously, it would be better if user
can see that return_type specifier is missing. I've also implemented
getErrorMessage function as is written in the book, but I couldn't get
better message.

On the other hand if I miss keyword 'Function' in the input:

Numeric foo
....
End

Error message is:

line 1:0 mismatched input 'Numeric' expecting FUNCTION

As can be seen it is made of token name, not real keyword which also
doesn't give useful information to the end user. Does anyone have
suggestion how can I improve error reporting?

Regards,
Bosko



More information about the antlr-interest mailing list