[antlr-interest] Weird problem with semantic predicates. Incorrect warning message

Gabriel Petrovay gabriel.petrovay at 28msec.com
Tue Apr 28 14:08:08 PDT 2009


Hi Jim,

1. warning(200): exit_test.g:9:3: Decision can match input such as
"IF" using multiple alternatives: 1, 3

The IF does not MATCH the first alternative, therefore, the warning is
NOT correct.

Why is the warning disappearing when I delete the second alternative?
If the warning were correct, it should still be displayed.

WITH PROBLEM
p_ExprSingle
   : p_IfExpr
   | {false}?=> p_ExitExpr
   | p_StepExpr
   ;

WITHOUT PROBLEM
p_ExprSingle
   : p_IfExpr
//    | {false}?=> p_ExitExpr
   | p_StepExpr
   ;



2. I want to parse the following texts where p_ExprSingle is the start rule:
--------
if // rule 3
--------
if(blah)  // rule 1 -> rule 3 for 'blah'
--------
if(if) // rule 1 -> rule 3 for the 'if' in parantheses
--------

So far the rule p_ExprSingle with only 2 alternatives does the job.
What about the following texts:

--------
if(exit with a)
--------
if(exit with if)
--------
if(exit with exit with exit)
--------

The p_ExitExpr is only available once a flag is set (therefore I use
the semantic predicate). If you delete the semantic predicate you will
see some correct warnings. As long as you have the predicate, the
warning is not correct anymore.

The problem is for sure the grammar ambiguity that John pointed out in
a previous e-mail.

I hope these examples together with the shortened-revised parser
grammar below makes a little more sense to you. I cannot make it
simpler.



PARSER GRAMMAR
--------------------------

p_ExprSingle
   : p_IfExpr
   | {false}?=> p_ExitExpr
   | p_StepExpr
   ;

p_IfExpr
   : IF LPAREN p_ExprSingle RPAREN
   ;

p_StepExpr
   : L_NCName | IF | EXIT
   | LBRACKET p_ExprSingle RBRACKET
   ;

p_ReplaceExpr
   : REPLACE p_ExprSingle WITH p_ExprSingle
   ;

p_ExitExpr
   : EXIT WITH p_ExprSingle
   ;



Regads,
Gabriel

Original message

> Hi,
>
> Thanks a lot John for make it it clear. Although I see the problem and
> I think that generates everyting, there are at least a couple of
> problems with ANTLR:
>
> 1. The Warning is incorrect: Decision can match input such as "IF"
> using multiple alternatives: 1, 4.  As a result, alternative(s) 4 were
> disabled for that input.
>
> This is non-sense to me since decision 1 in p_ExprSingle does not
> match "IF". Only decision 4 can match it (p_StepExpr)
>
No, the warning is perfectly correct. Use ANTLRWorks, do the Check
Gramar, select the rule in red, select the syntax tab, turn on both
paths, your problem will be obvious.
>
> 2. What do those two alternatives (1 and 4) have to do with the
> alternative 2 (p_ExitExpr) and a rule that is NOT used anywhere
> (p_ReplaceExpr) ???
>
Because in the look ahead set, you calculate all possible tokens that
the grammar says can come next, not just the rule that you have
provided. Basically your grammar does not make a lot of sense and ANTLR
is trying to tell you this. You might be better starting with the
examples and building up from there; trying to run before you can walk
gets you in to trouble with grammars ;-)

Jim


--
MSc Gabriel Petrovay
MCSA, MCDBA, MCAD
Mobile: +41(0)787978034



-- 
MSc Gabriel Petrovay
MCSA, MCDBA, MCAD
Mobile: +41(0)787978034


More information about the antlr-interest mailing list