0
votes

For the following rule :

 switchBlockLabels

:   ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabel* switchDefaultLabel? switchCaseLabel*)
;

I got an error:"rule switchBlockLabels has non-LL descision due to recursive rule invocations reachable from alts 1,2".And I tried to add syntactic predicate to solve this problem.I read the book "The Definitive ANTLR Reference".And Now I am confused that since there is no alternatives in rule switchBlockLabels,then no decision need to be made on which one to choose. Is anyone can help me?

1

1 Answers

0
votes

Whenever the tree parser stumbles upon, say, 2 switchCaseLabels (and no switchDefaultLabel in the middle), it does not know to which these switchCaseLabels belong. There are 3 possibilities the parser can choose from:

  • 2 switchCaseLabels are matched by the 1stswitchCaseLabel*;
  • 2 switchCaseLabels are matched by the 2ndswitchCaseLabel*;
  • 1 switchCaseLabel is matched by the 1stswitchCaseLabel*, and one by the 2ndswitchCaseLabel*.

and since the parser does not like to choose for you, it emits an error.

You need to do something like this instead:

switchBlockLabels
 : ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabel* (switchDefaultLabel switchCaseLabel*)?)
 ;

That way, when there are only switchCaseLabels, and no switchDefaultLabel, these switchCaseLabels would be always matched by the first switchCaseLabel*: there is no ambiguity anymore.