1
votes

I am printing out the type field and text field from an AST tree based on my grammar and I get this

type=5 text=and
    type=14 text==
        type=4 text=ALIAS
            type=20 text=a
        type=7 text=ATTR_NAME
            type=20 text=column_b
        type=36 text=STR_VAL
            type=35 text="asdfds"
    type=14 text==
        type=4 text=ALIAS
            type=20 text=a
        type=7 text=ATTR_NAME
            type=20 text=yyyy
        type=12 text=DEC_VAL
            type=11 text=564.555

Valid type ints in my generated lexer are

public static final int EOF=-1;
public static final int ALIAS=4;
public static final int AND=5;
public static final int ATTR_NAME=7;
public static final int DECIMAL=11;
public static final int DEC_VAL=12;
public static final int EQ=14;
public static final int ID=20;
public static final int STR_VAL=36;

I would very much like to NOT have type=20 ever in the tree!!! and instead move the nodes with type 20 up one level so text would be the information(not the token name) and the type would be 4,7, or ALIAS or ATTR_NAME types. Is there a way to do this?

This part of my current grammar is using imaginary tokens ATTR_NAME and ALIAS right now like so(comment if I need to put more of my grammar up but I think this is enough to solve it)

primaryExpr
    :   compExpr
    |   inExpr
    |   parameterExpr
    |   attribute
    ;

parameterExpr
    :   attribute (EQ | NE | GT | LT | GE | LE)^ parameter
    |   aliasdAttribute (EQ | NE | GT | LT | GE | LE)^parameter
    ;

compExpr
    :   attribute (EQ | NE | GT | LT | GE | LE)^ value
    |     aliasdAttribute(EQ | NE | GT | LT | GE | LE)^value
    ;
alias
    :   ID
    ;
inExpr  :   attribute IN^ valueList
    ;

attribute: ID -> ^(ATTR_NAME ID);

aliasdAttribute
    :       alias(DOT)(ID) -> ^(ALIAS alias ) ^(ATTR_NAME ID)
    ;
1

1 Answers

1
votes

Is there a way to do this?

Sure.

The alias rule in grammar T:

grammar T;

options {
  output=AST;
}

tokens {
  ALIAS;
}

alias
 : ID -> ALIAS[$ID.text]
 ;

ID : ('a'..'z' | 'A'..'Z')+;

will always produce (rewrite) a token with type ALIAS, but with inner text the same as the ID token.