[antlr-interest] simple function exercise

eric nelson wirecracker at gmail.com
Mon Oct 17 21:23:20 PDT 2005


Why do you say "make sure you work with streams, not files"? Eventually I'd
like to actually be able to have the new source code in a new file. Thanks
for your response! - e


 On 10/17/05, Sohail Somani <sohail at taggedtype.net> wrote:
>
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20051017/881a62e3/attachment.html


More information about the antlr-interest mailing list