[antlr-interest] v3: semantic predicates in parser
Terence Parr
parrt at cs.usfca.edu
Fri Aug 18 10:57:55 PDT 2006
interesting...this in fact has 2 alts that lead to the same recursive
rule invocation. I need to modify this message so it shuts up with
predicates, but it will have to unravel back to k=1. When you get
that message, it means ANTLR will never be able to build the DFA.
Ter
On Aug 18, 2006, at 5:25 AM, Richard Musiol wrote:
> Hi,
>
> semantic predicates in the parser don't seem to work in beta 3 as
> they did in ANTLR 2. Will they be supported in the final?
>
> For example the following grammar:
>
> grammar Test;
>
> document : (element)* EOF ;
> element : theA | B | C ;
>
> theA
> :
> A
> (
> { someCondition }?=>
> theElement=element
> {
> // do something with theElement
> }
> |
> // quit the rule
> )
> ;
>
> A : 'a' ;
> B : 'b' ;
> C : 'c' ;
>
> ANTLR can't handle it:
> "[fatal] rule theA has non-LL(*) decision due to recursive rule
> invocations in alts 1,2. Resolve by left-factoring or using
> syntactic predicates with fixed k lookahead or using backtrack=true
> option."
>
> At my opinion, ANTLR should use the first alternative if
> someCondition is true and the second one if not.
>
> My second approach was to swap the alternatives:
>
> theA
> :
> A
> (
> { !someCondition }?=>
> // quit the rule
> |
> theElement=element
> {
> // do something with the element
> }
> )
> ;
>
> ANTLR compiles this without any errors, but the resulting code is
> really stange:
>
> [...]
> else if ( (LA3_0==A) ) {
> else {
> NoViableAltException nvae = [...];
> throw nvae;
> }
> }
> [...]
>
> It seems that there is a bug in the code generation. But also the
> decisions are wrong:
>
> [...]
> int alt3=2;
> int LA3_0 = input.LA(1);
> if ( (LA3_0==EOF) && ( !someCondition )) {
> alt3=1;
> }
> [...]
>
> Why should it only quit the rule if an EOF is following? There may
> be an A, B or C too.
>
> I hope this will get fixed until the final, because semantic
> predicates are a really powerful feature.
>
> Regards,
> Richard
>
More information about the antlr-interest
mailing list