[antlr-interest] [Q] how rewrite tree for this simple rule with alternatives?

Ruslan Zasukhin ruslan_zasukhin at valentina-db.com
Sat Oct 15 07:54:18 PDT 2011


Hi All,
Hi Terrence,


What still wonders me a lot in ANTLR v3  is that fact that we must quite
often to use helper rules_leafs to build trees.

============================================
EXAMPLE in v2 rule looks as

alter_trigger_statement
    :    "alter"! "trigger"! trigger_name ( "enable" | "disable" )
            { ## = #(#[ALTER_TRIGGER, "ALTER_TRIGGER"],##); }
    ;



============================================
ANTLR3 working solution:

alter_trigger_statement
    :    alter_trigger_statement_leaf
            -> ^( ALTER_TRIGGER alter_trigger_statement_leaf )
    ;
    
alter_trigger_statement_leaf
    :    T_ALTER! T_TRIGGER! trigger_name ( T_ENABLE | T_DISABLE )
    ;



============================================
Because attempts to make it in SINGLE rule way  -- not works:

alter_trigger_statement
    :    T_ALTER T_TRIGGER trigger_name ( T_ENABLE | T_DISABLE )
            -> ^( ALTER_TRIGGER trigger_name (T_ENABLE | T_DISABLE) )
    ;


PROBLEM comes from this alternative (T_ENABLE | T_DISABLE ) ...
We cannot label it right?

And it is wrong make both tokens optional

alter_trigger_statement
    :    T_ALTER T_TRIGGER trigger_name ( T_ENABLE | T_DISABLE )
            -> ^( ALTER_TRIGGER trigger_name T_ENABLE? T_DISABLE? )
    ;


And we have many such places, when IMAGINARY token,
added in v2 as easy as one line, requires in v3 adding one more RULE.

As for me, this not sounds, as "more clear and powerful way to build trees".


Am I really blind and not see some easy way for v3?
In examples of ANTLR I have not found any that use imaginary tokens btw...

Thank you for any ideas...




-- 
Best regards,

Ruslan Zasukhin
VP Engineering and New Technology
Paradigma Software, Inc

Valentina - Joining Worlds of Information
http://www.paradigmasoft.com

[I feel the need: the need for speed]




More information about the antlr-interest mailing list