[antlr-interest] approach for rewriting source code

T Stotts timotheus at tstotts.net
Thu May 28 21:23:50 PDT 2009


I want to use ANTLR v3 to rewrite source code by removing and partially
compiling specific items. How would I go about doing the following with
this very simple example.


-- Program Input
constant x : integer := 5;
constant y : integer := x + 6;
constant z : integer := y * 8;

-- Program Output
constant y : integer := 5 + 6;
constant z : integer := y * 8;


In this example, I already have created a lexer and parser that can
understand the above language. Do you have a recommendation on how I
achieve this language transformation where the main program arbitrarily
decides that constant 'x' should be substituted and removed? Should I
use the parser's parse tree, or should I use grammar rewrite rules to
first create a structured AST? How do I rewrite the contents of the
input stream buffer and output preserving white-space?

How much of the language functionality does ANTLR already provide with
its API? Obviously the rewriting decision making has to be a custom
routine.

Thanks for any ideas!

-- 
aka:   timotheus
web:   http://tstotts.net/


More information about the antlr-interest mailing list