[antlr-interest] disabling portions of a rule?
Loring Craymer
lgcraymer at yahoo.com
Mon Oct 30 18:11:37 PST 2006
ANTLR 3 does an inorder traversal of the tree being
walked; shortcuts are not supported. This is
different from ANTLR 2, where you could match a node
and then ignore its children (and siblings). The big
gain for the inorder traversal is that you can support
k>1 for tree walkers. A secondary benefit is that you
get strong checking of tree structure, but that is
counterbalanced by the inability to do shortcutting.
[I consider this a gain, but I start with automtically
generated tree grammars.]
The best you can do right now is to have two versions
of the subrule--one with actions and one without--and
use the sempred to pick which one to use.
--Loring
--- Robert Hill <rob.hill at blueyonder.co.uk> wrote:
> Hi all,
> I Have a rule like this
>
> exampleRule
> @init {
> Symbol s = null;
> }
> : ^(MAP Identifier { s =
> Symbol.Resolve($Identifier.text);
> if (s==null)
> ShowError();
> }
> ({s==null}? Subrule[s]);
> )
>
> So , basically if the identifier isn't in the symbol
> table we don't call the
> subrule. The predicate does the job but when a
> symbol isn't found I get a
> whole bunch of errors about mismatched tree nodes..
> - how do I either
>
> a) exit the rule early without displaying the antlr
> errors, or
> b) consume the tokens that follow even though I cant
> call the subrule to get
> rid of them?
>
> The subrule assumes it will always be passed a valid
> symbol, and has quite a
> few alts in it, so I don't want to add a if
> (s!=null) before all of the code
> in the subrule.
> I'd like to prevent antlr from showing its
> mismatched tree errors, whilst
> preferably not exiting with a null pointer exception
> from within the
> subrule...
>
> Whats the neat/preferable way of handling this?
> Cheers!
>
> Rob
>
>
>
>
>
____________________________________________________________________________________
Cheap Talk? Check out Yahoo! Messenger's low PC-to-Phone call rates
(http://voice.yahoo.com)
More information about the antlr-interest
mailing list