in progress
Goal: text input to text output; usually source code.
Formal systems
ASF+SDF. Symbol differentiator has syntax then separate rewrite rules like:
d(E1+E2)/dV = dE1/dV + dE2/dV E + 0 = E
Stratego does same but can also do AST rewrites.
TXL rewrites with union grammars
ANTLR
Tree grammars + templates
Rewrite only if result is same AST structure else annotate and build other structures then emit code.
Instruction generators
TWIG, BURG, etc...
Tom Harwood's JBURG example (partial):
/*
* Recogize a PLUS node, add up its operands, and return the result.
*/
reg = ExprParser.PLUS(reg arg1, reg arg2): 1
{
Object dst = regAlloc.allocateRegister(#reg);
mipsEmitter.printInstruction ( "add", dst, arg1, arg2);
return dst;
}
/*
* Load a constant into a register.
*/
reg = constant : 1
{
Object dst = regAlloc.allocateRegister(#reg);
mipsEmitter.printInstruction("li", dst, #reg, null);
return dst;
}
Tweaking source
Use TokenRewriteStream to insert/replace/delete rather than create tree and then try to regenerate same thing with comments and whitespace.
Labels: