[antlr-interest] treewalker question: Identifiers

Andy Tripp antlr at jazillian.com
Fri Aug 17 09:36:26 PDT 2007


I have a parser rule where Identifiers and literals can have special
prefixes and suffixes:

formValue:
    formPrefix? (literal^ | Identifier^) HEX_DISPLACEMENT?
    ;

And now, writing my treewalker, I take the parser rule for literal:

literal:
    integerLiteral
    | LongLiteral
    | floatingPointLiteral
    | StringLiteral
    | DATE_LITERAL
    | TRUE
    | FALSE
    ;

...and say "ok, when treewalking, literal can also be a root node with
optional prefix or suffix:

literal:
    (integerLiteral
    | LongLiteral
    | floatingPointLiteral
    | StringLiteral
    | DATE_LITERAL
    | TRUE
    | FALSE
    ) formPrefix? HEX_DISPLACEMENT?
    ;

Does that look right? OK, good, that's just a warmup for the real issue: 
Identifier.
We have this slightly ugly lexer rule for Identifier:

Identifier:
    '['? LETTER (LETTER| DECIMAL_LITERAL)*
        ('%'|'#'|'$'|'&'|
          ('!' ~(LETTER)) => '!'
        )? ']'?
    ;

...and now what should the treewalker rule for Identifier be? Should I 
even have
a treewalker rule that corresponds to a lexer rule? If so, do I just do 
this:

Identifier:
    ( '['? LETTER (LETTER| DECIMAL_LITERAL)*
        ('%'|'#'|'$'|'&'|
          ('!' ~(LETTER)) => '!'
        )? ']'?
    ; ) formPrefix? HEX_DISPLACEMENT?

That doesn't seem right, especially with the predicate "=>" in there.

Treewalkers make my head hurt :)







More information about the antlr-interest mailing list