TXL is already installed for all users. To transform a piece of source code, one needs to give:
txl [source code] [transformation program]There are a lot of options one can add. You can see them by giving
txl -helpTo test the setup, download this file and give
tar xvf TXLExamples.tarIf the output ends with 42, we're good to go!
Examine Sort.Txl. It transforms "programs" that are sequences of numbers so that the sequences are sorted. You can run it on a sample program with:
txl Numbers.Sort Sort.TxlTo see the transformations rules that were applied, run as
txl Numbers.Sort Sort.Txl -DapplyTo see the parse tree for the input, run as
txl Numbers.Sort Sort.Txl -DparseDo the same with the other two examples. Following are the commands you will need:
txl TwoVectors.DotProduct DotProduct.TxlWe will next transform real programs written in Java. For this purpose, we will need a grammar for the Java language. Thankfully, the TXL website contains such a grammar. You can find it at
/cse/fac/bin/6431/Txl/java.grmWe will attempt to create a transformation program that simplifies conditions that contain expressions such as a++. An example of such a condition is
if (a++ == b)These are notoriously hard to read and can lead to very subtle bugs. We will attempt to transform them so that the incrementation happens before the if statement. The transformed program should look like
a++;For the sake of simplicity, we will concentrate on expressions like the above, and ignore expressions such as ++a, a--, and --a. We will also assume that only one such incremental expression occurs within a condition (it is really poor practice to have multiple such expressions in the same condition).
To test our TXL program, we will use this small app, as well as this larger source file from the TAB2PS system.
Write a TXL program called Step1.txl that transforms every if statement condition to true.
Test your program with:
txl PlusPlus.java Step1.txlModify the previous program into a new program called Step2.txl that only transforms to true if the original condition contains an expression, such as a++.
Test your program with:
txl PlusPlus.java Step2.txlModify again into Step3.txl so that instead of transforming to true, your program removes the ++ part and replaces it with -1.
Test your program with:
txl PlusPlus.java Step3.txlFinally, add the a++; statement before the if statement. Call your final program Step4.txl, and test it with
txl PlusPlus.java Step4.txlWhat other test cases can you think of?