[antlr-interest] Handling errors using syntactic predicate

Philippe Frankson Philippe.Frankson at Frsglobal.com
Wed Apr 27 02:28:51 PDT 2011


Hi,

In my grammar, I can parse expression like this:
(A <= 1 or A >= 6) and (A not in (43,44))

Let's say, it is a kind of sql WHERE clause.

Here is a part of the grammar to understand the problem:

@rulecatch {
catch (RecognitionException re) {
	if (this.errorMessage.isEmpty())
		this.errorMessage = this.getErrorMessage(re,
this.getTokenNames()) +  " [pos: " + (re.charPositionInLine -
errorPosOffset) + "]";
	throw re;
}
}

...

arithexpr:		(arithterm ) (
				'+' arithterm  | 
				'-' arithterm  | 
				CONCAT arithterm)*;

arithterm:		(arithatom | '-' arithatom) 
				('*' arithatom 
				| '/' arithatom)*;

arithatom:		arithvar
			| ifthenelse
			| constantorparameter
			| floorexpr
			| floatexpr
			| leftexpr
			| rightexpr
			| replaceexpr
			| dayexpr
			| monthexpr
			| yearexpr
			| yearnoexpr
			| (arithexprparen)=> arithexprparen 
			;

arithexprparen:		'(' arithexpr ')';


You can notice that I'm using syntactic predicate to support nested
expression with parenthesis.

The problem is the following:
If I have a mistake in my expression like this:  (A <= 1 or A >= 6xx)
and (A not in (43,44))
Where 6xx is the mistake,
The error I have is: 'no viable alternative at input 'A' [pos:1]
This is because it doesn't reach the right parentheses while parsing
"(arithexprparen)=> arithexprparen", so the error is triggered for the
beginning of arithatom evaluation.
I would have liked it to tell me that the problem is 'x' and not 'A'.
Is there any way to deal with that ?


Thanks a lot for any help.
Philippe



More information about the antlr-interest mailing list