[antlr-interest] Recursive rules and LL(*)
Emond Papegaaij
e.papegaaij at student.utwente.nl
Fri Jun 23 08:02:12 PDT 2006
Hello,
I'm having a bit of a problem with a recursive rule combined with LL(*). The
following (snapshot) of the grammar triggers the problem:
grammar TPL;
nodeDecl: (targetClass ':')? targetClass;
targetClass: '<' (targetClass)? '>';
This should match strings like '<<>>:<>' and '<<<>>>'. However the recursive
rule does not work with the LL(*) algorithm. It tries to build the entire
language in a single DFA and then bails out with:
tpl2.g:3:30: Alternative 1: after matching input such as '<' '<' '<' '<' '<'
decision cannot predict what comes next due to recursion overflow to
targetClass from targetClass
tpl2.g:3:30: Alternative 2: after matching input such as '<' '<' '<' '<' '<'
decision cannot predict what comes next due to recursion overflow to
targetClass from targetClass
Is there any way to make this work with LL(*)? The DFA should take all '<'
and '>' tokens without trying to match the number of tokens and finally
branch on ':' or EOF. A mismatch between the number of '<' and '>' tokens
will be detected by the parser. Of course I could use the following rule:
nodeDecl options {k = 1;}
: (targetClass ':')=> targetClass ':' targetClass
| targetClass;
But I think the LL(*) version is more clear, and it should be faster.
Best regards
Emond Papegaaij
More information about the antlr-interest
mailing list