[antlr-interest] tricky (to this newby) translation problem
Kenny Tilton
ktilton at nyc.rr.com
Fri Dec 30 08:31:55 PST 2005
First of all, thanks to all for Antlr. I have taken my first baby step
translating k&r C to Common Lisp and am feeling a lot better about
converting 23kloc of "C" to Lisp. :)
I took the cgram example as my headstart, deriving from GnuCEmitter.g to
get a new AnsiCLEmitter.g. My first change was in the translation of "C"
enums. First, here is how I want to translate:
"C": enum myEnum {zero, two=2, three };
Common Lisp: (def-enum (myEnum) zero (two 2) three)
(Aside to the curious: no, CL does not have def-enum, I will have to
write that.)
Now here is my problem. The recognizer for an enumerator such as "zero"
or "two=2" looks like this:
enumerator
: i:ID { print( i ); }
( b:ASSIGN { print( b ); }
expr
)?
;
But my problem is that, if the match with an ASSIGN happens, I need to
get a left-parens /in front/ of the emitted ID. Otherwise I have to
always wrap enumerators in parens:
(def-enum (myEnum) (zero) (two 2) (three))
Not the end of the world, but I am hoping I can make the emitted CL as
nice as possible.
My first try was (roughly):
enumerator
: i:ID {print( i ); }
| (j:ID {print("("); print( j ); }
ASSIGN {print(" "); }
expr {print(")");}
)
;
But that gives me a nondeterminism warning when the grammar is processed
by antlr and a mismatchedTokenException runtime error.
Full marks to the Antlr examples, I have gotten this far without
learning too much about parsing. I will dive into the doc now to see if
this idiom can be handled better, just asking here first in case this is
a no-brainer someone can explain easily (or steer me to the right doc).
kenny
More information about the antlr-interest
mailing list