[antlr-interest] "everything else" clause

Ric Klaren klaren at cs.utwente.nl
Mon Apr 26 05:23:03 PDT 2004


On Sun, Apr 25, 2004 at 05:30:34PM +0200, Mike Lischke wrote:
> Does anybody know how I can specify a rule, which matches a few things and
> collects everything else into an own series of tokens. ANTLR creates a
> switch statement from my grammar, which contains the few allowed cases. But
> instead using the default branch for everything else it lists all tokens it
> knows for that single parser for the "rest" of the input. This does not
> work well however because due to the lexer there can be tokens, which are
> not known to the parser.
>
...
> rest:
>   (options {greedy = false;}: .)+
> ;
>
> produces a nondeterminism warning for each alternative in the program rule.
> I would be very thankful for any good solution.

The . alternative always generates non determinism errors since it does not
behave like 'if-nothing-matched-so-far-try-this'. In general you can read
the generated code and see if the does the right thing. Note that you
probably have to move the '.' into the rule with the other alternatives
because the parser will never leave the above rule (barring EOF) (I don't
think the greedy option will matter in this case).

In the action code for the . alternative you will have to make sure to do
the right thing with the tokens you don't want to match in the parser. Note
also that your lexer will have tokenized it in some way. You could also
maybe use a tokenstream to get the right effect (e.g. replace whole ranges
of tokens with a wildcard token that stores the actual tokens beneath it
similar to the hidden token stuff)

You will maybe get strange interactions with semantic predicates (depending
on the action used, you may be able to cheat that with an init action for
the . alternative and check explicitly for guessing mode there).

(Advice read a lot of generated code ;) )

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893755  ----
-----+++++*****************************************************+++++++++-------
     "Evil will always triumph, because Good is dumb." ---  Spaceballs



 
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