[antlr-interest] Lexer Token Rewrite/Alias

Matthew Bowman matthew.bowman at sogotech.com
Thu Sep 20 00:31:40 PDT 2007


Here's a simple grammar

--- BEGIN Alias.g ---

grammar Alias;

options { output=AST; }
tokens { FUNC;}

expr
    : func (PLUS^ func)*
    ;

func
    : ( f=FOO | f=BAR ) LPAREN NUMBER RPAREN -> ^(FUNC $f NUMBER)
    | ALIAS -> ^(FUNC FOO ZERO)
    ;

PLUS : '+';
FOO : 'foo';
BAR : 'bar';
LPAREN : '(';
RPAREN : ')';

NUMBER : DIGIT+;
fragment
DIGIT    : '0'..'9';

ZERO    : '0';
ALIAS    : 'f';

WS  : (' '|'\r'|'\t'|'\u000C'|'\n')+ {$channel=HIDDEN;};

--- END Alias.g ---

This supports simple foo/bar "function" calls that take numerical 
arguments. What I'm trying to accomplish is the ability to alias 
function calls like 'f' = 'foo(0)'; With this grammar I get the AST 
output as follows:

foo(0) + bar(1) -> (+ (FUNC foo 1) (FUNC bar 2))
f + bar(2) -> (+ (FUNC FOO ZERO) (FUNC bar 2)) *

* I would like to see (+ (FUNC foo 0) (FUNC bar 2))

1st question is if something like this is even possible? If yes, 2nd 
question would be how?

I've been fiddling with this for some time now and have yet to manage to 
find a solution :( I'm not sure but would TokenRewriteStream be 
applicable? Basically whenever I encounter the ALIAS token, replace it 
in the stream with the tokens FOO='foo' LPAREN='(' NUMBER='0' RPAREN=')' 
in that order. This would allow me to just lex the ALIAS token, but the 
parser rules would never have to worry about seeing the ALIAS token as 
it would simply see the other set of tokens?

Thanks in advance!

-- 
Matthew Bowman



More information about the antlr-interest mailing list