[antlr-interest] Use another rule template's result

BeRniTo bernito.lb at gmail.com
Sun Nov 28 12:03:18 PST 2010


Hello guys!

I'm trying to make a translator to convert some input written in (some sort
of) C to a non-existent language (I'm doing a work for university and the
language comes from the teacher).

I've already created the lexer and the parser, and so a tree walker that
checks input's semantic.

Now, having the input:

constdef c1 = 1+2;
void main() { c1; }

I want to emit this code: ((add 1) 2));

The thing is I don't need to print the template's result of constdef rule
"((add 1) 2))" when visiting it's node but to store it somewhere and then
refer to it later in MAIN rule maybe by it's name (c1).
I was thinking in creating a HashMap<String, String>, like I did with the
symbol table (to store IDs so to report an error if a constant is declared
twice and those things), but I can't put embeded java code in rules as I
could when checking the semantic in the other tree walker with output=AST
(here I'm using "output=template" instead).

Is there a way to do this? What am I missing?

Sorry if my explanation isn't clear enough but I'm not an english-speaker so
it's a little hard to explain myself correctly. :-P


More information about the antlr-interest mailing list