Removing Redundant Declarations Using TXL
Software Transformation Systems
TXL solution to
TIL Chairmarks #4.1: Removing redundant declarations.
--
JamesCordy - 04 Jul 2006
File "TILredundant.Txl"
% TXL transformation to remove unused declarations in a TIL program
% Jim Cordy, July 2006
% Based on the TIL base grammar
include "TIL.Grm"
% Preserve comments, we're probably going to maintain the result
include "TILCommentOverrides.Grm"
% Transformation rule to remove all redundant variable declarations from a program.
% If a declared variable is not mentioned by the following statements, it is not used.
rule main
% Find each variable declaration
replace [statement*]
'var Id [id] ';
FollowingStatements [statement*]
% Check to see if the declared identifier does not appear in the following statements
deconstruct not * [id] FollowingStatements
Id
% If not, the declaration is redundant so we remove it
by
FollowingStatements
end rule
Example run:
<linux> cat redundantvareg.til
// Meaningless example TIL program with some redundant declarations
// (the ones with names like rr, rx, ry, rm)
var d;
var y;
d := 17;
read y;
var rr;
while y != 0 do
var rx;
var a;
a := 3;
var j;
j := 1;
while j != 100 do
a := a * 2;
var ry;
j := j + 1;
end
var c;
c := a + j;
var n;
n := c * y;
write n;
y := y - 1;
var rm;
end
<linux> txl redundantvareg.til TILredundant.Txl
TXL v10.4c (15.3.06) (c)1988-2006 Queen's University at Kingston
Compiling TILredundant.Txl ...
Parsing redundantvareg.til ...
Transforming ...
// Meaningless example TIL program with some redundant declarations
// (the ones with names like rr, rx, ry, rm)
var d;
var y;
d := 17;
read y;
while y != 0 do
var a;
a := 3;
var j;
j := 1;
while j != 100 do
a := a * 2;
j := j + 1;
end
var c;
c := a + j;
var n;
n := c * y;
write n;
y := y - 1;
end
<linux>