[antlr-interest] Handling errors using syntactic predicate

Jim Idle jimi at temporal-wave.com
Wed Apr 27 06:58:12 PDT 2011


Go and look at the expression tree for say the Java.g example grammar. You
will probably see that your expression tree is not formed correctly and
that is one possibility for why you need the predicate (BTW, when you do
need a predicate, don't specify the whole rule, use the minimum set of
tokens that is required).

Specifically here, look at you unary operator and sort that out, then I
suspect that you are not showing the whole thing here and your paren
expression is clashing with something else higher up the chain, which is
the other reason you use the predicate.

Basically, you need to remove the predicate and fix the grammar problems
that ensue once you remove it.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Philippe Frankson
> Sent: Wednesday, April 27, 2011 2:29 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Handling errors using syntactic predicate
>
> 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
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list