[antlr-interest] simple function exercise
Sohail Somani
sohail at taggedtype.net
Mon Oct 17 21:02:23 PDT 2005
On Mon, 2005-17-10 at 04:13 -0700, Eric Nelson wrote:
> I haven't posted for awhile since my research responsibilities shifted
> over the summer, but my goal is still to learn ANTLR ... Its actually
> been driving me crazy, although mabye that's why it holds my
> interest? ;)
>
> Well here is a simple exercise that would give me some insight:
>
> Using a Java grammar, write a source to source translator that simply
> recognizes functions and replaces the names as such:
>
> 1) replace "main" (or "int main" or whatever) with "func_0"
> 2) append to all other functions "func_1" ... "func_n"
>
> I'd like the new source code to be written to a file ... although
> honestly I'll take anything you can throw my way. You might ask me
> for what purpose I'd like to do this ... I'd probably just answer its
> because I have no idea what I'm doing ;) Thanks for any tips!
I'd suggest something slightly less ambitious (than modifying a java
grammar!).
Make up your own language that has only two sentences (you'll have to
figure out how to get the below to actually work, define a lexer and a
parser atleast):
funcDefn: "Function" IDENTIFIER LCURLY EOL
(singleStatement EOL)+
RCURLY EOL
;
singleStatement: "MOV" IDENTIFIER (COMMA IDENTIFIER);
Transform the source text by changing MOV A,B to "A = B" and change the
function name to be the concatenation of identifiers of the last
statement.
My guess is you will need atleast a tree grammar, though I might be
mistaken.
Also, make sure you work with streams, not files.
Best of luck.
Sohail
More information about the antlr-interest
mailing list