[antlr-interest] Irregular AST construction

Mike Lischke mike at lischke-online.de
Thu Jul 5 04:29:58 PDT 2012


Hey Benjamin,

thanks for taking a look.

> If I had to guess, I'd say Antlr is not actually looking at the end of
> that rule when it generates the code for the subrule (so it uses
> streams in case you have more of the same tokens or rules); you'll get
> similar code if you leave out the ( COLLATE_SYM identifier )* but
> leave the parens around the alternatives.

So this is probably a bug in code generation?

> Personally, I've always had problems with rewrite rules appearing in
> alternatives, particular when I try to reference labels that appear
> later in the rule (in e.g. your example, you can't reference
> "identifier" in a rewrite rule in the subrule above it).

I have a number of cases where I construct subtrees for later tree parsing (so we can not only skip certain parts easily, but also know the level of nesting, which determines certain things like the scope for references).

> My strategy
> such cases is to use semantic predicates instead of embedding rewrite
> rules in subrules:
> 
> primary:
>        (
>               ...
>                | PLUS_SYM p1=primary
>                | MINUS_SYM p2=primary
>        )
>        (options {greedy = true;}: COLLATE_SYM identifier)*
>        -> {$function_call}? ^(FUNCTION_CALL function_call)
>        -> literal? field_name? ... PLUS_SYM? $p1? MINUS_SYM? $p2? ...
> interval_expression? ( COLLATE_SYM identifier )*
> ;


Using subrules here sounds like a sensible way to bypass this problem. Thanks for your suggestion!

Mike
-- 
www.soft-gems.net




More information about the antlr-interest mailing list