[antlr-interest] Predicate question

Ric Klaren klaren at cs.utwente.nl
Mon May 3 05:28:36 PDT 2004


Hi,

On Sun, May 02, 2004 at 03:28:43AM -0000, Brad Schick wrote:
> // The RCURLY lookahead will match but not consume it,
> // allowing the RCURLY to terminate the enclosing function
> endline
>     : (RCURLY) =>
>     | NEWLINE!;
>
> This works fine, but the generate C++ code confuses me. The if
> statement that checks for a match of the first subrule first tests LA
> (1) against a tokenset that includes RCURLY along with all tokens
> that can follow it (mostly tokens that could start the next line).
> If this tokenset is matched, the lookehead takes places and checks
> for RCURLY specifically.
>
> What is the purpose of first matching LA(1) to this broad tokenset?

It's probably due to the fact that the rule matches nothing as a result
ANTLR first checks the follow set of the rule and then attempts the
predicate only if the followset is not matched. Doing a predicate check
can be very expensive so if you can see on beforehand that you don't have
to go through it, then it's a good thing. (at least that's what I'd expect)

Terence might be able to give a less guesstimate answer to this.

> I'm guessing this might be to reduce the need for the lookahead and
> a possibly exception, but why not just check LA(1) against the first
> token of the predicate?

ANTLR seems to do the reverse check with the follow set of the alternative.

It's maybe something personal of me, but rules that match nothing when
using a recursive descent parser are plain ugly ;) but if it works for
you...

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893755  ----
-----+++++*****************************************************+++++++++-------
  "You can't expect to wield supreme executive power just because some
   watery tot throws a sword at you!"
  --- Monty Python and the Holy Grail



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list