[antlr-interest] Why does syntactic predicate not take effect?

Britta Kiera nukiti at yahoo.de
Tue Nov 11 01:52:02 PST 2008


John B. Brodie <jbb at acm.org> wrote:
> > In the grammar below NAMES has been made a fragment. But still it produces
> > the same output as before that doesn't contain a NAMES token:
> actually I believe the prefered way is to specify this and any other Virtural
> Token in a tokens {} section - should appear after the options {} and before
> your @header {} section.
I agree.

> > Token:  IDENT( 0) >GoTo<
> > Token:  WHITE(99) > <
> here is, i believe the problem, see comments near your IDENT rule below.
You were right.

> > IDENT
> >     : (ID (WS ID)+)=> ID (WS ID)+ {$type = NAMES;}
> recall that ANTLR lexer constructs greedily consume the longest possible string, and that,
> further, once commited to a particular looping construct no other alternative is recognized.
> so your predicate wants a list of WS ID pairs. fine. good. gotcha. but your input has some
> WS ID pairs and then a WS LB pair - that does not match a (WS ID)+ and the predicate fails.
> remove the blank before the { in your test data and see what I mean. so try this ident
> rule instead:
> IDENT
>     : ID ( ((WS ID)=>(WS ID))+ {$type = NAMES;} )?
>     ;
Obviously I didn't understand how predicates work. I thought they would
match as much as possible. I did not understand that the complete predicate
fails when the common WS prefix was recognized. Coming from lex/JFlex
this will probably be a pitfall for quite some time.

Thank you very much John. You brought me a big step further in understanding
how ANTLR works.

Regards,
Nukiti


      


More information about the antlr-interest mailing list