[antlr-interest] Strange ambiguous decision warning
Jim Idle
jimi at temporal-wave.com
Wed Apr 2 10:42:01 PDT 2008
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Martin Probst
> Sent: Wednesday, April 02, 2008 1:19 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Strange ambiguous decision warning
>
> Hi all,
>
> I'm running into a seemingly strange ambiguity warning with ANTLR 3
> (latest beta).
>
> I have a rule like this:
> sequenceType : (EMPTY_SEQUENCE '(' ')')
> | (itemType occurrenceIndicator) => (itemType
> occurrenceIndicator)
> | itemType ;
> occurrenceIndicator
> : '?' | '*' | '+';
> itemType : kindTest | (ITEM '(' ')') | atomicType ;
> kindTest : ...;
> atomicType : qNameOrIdent ;
> qNameOrIdent : QNAME | EMPTY_SEQUENCE | ...;
>
> ANTLR complains about sequenceType:
> ANTLR Parser Generator Version 3.1b1 (??) 1989-2007
> warning(200): /Users/martin/workspace/XQPretty/src/antlr/XQuery.g:
> 235:14: Decision can match input such as "EMPTY_SEQUENCE" using
> multiple alternatives: 1, 2, 3 As a result, alternative(s) 2,3 were
> disabled for that input
If you use ANTLRWorks, it will help you to see the following:
Alt1 starts with EMPTY_SEQUENCE
Alt2 goes to itemType
At3 goes to itemType
itemType goes to atomicType
atomicType goes to qNameOrIndent
qNameOrIndent contains EMTPY_SEQUENCE
hence all three alts can start with EMPTY_SEQUENCE. as occurenceIndicator is optional, then it is ambiguous.
Change your rule to:
sequenceType : (EMPTY_SEQUENCE '(')=>(EMPTY_SEQUENCE '(' ')')
| itemType occurrenceIndicator?
;
Then you will get ambiguities on '+', which means that you actually need:
sequenceType : (EMPTY_SEQUENCE '(')=>(EMPTY_SEQUENCE '(' ')')
| itemType ((occurrenceIndicator)=>occurrenceIndicator)?
;
If you want to eliminate all the warnings.
Doing this removes the huge predicate that you have right, replace it with a two token sequence, which will be much faster :-)
Though it is somewhat irrelevant to the issue, you will find your grammar more maintainable if you format it a little ;-)
Jim
More information about the antlr-interest
mailing list