Set up

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 -help

To test the setup, download this file and give

tar xvf TXLExamples.tar
cd examples
txl Ultimate.Question Question.Txl

If the output ends with 42, we're good to go!

Some simple examples

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.Txl

To see the transformations rules that were applied, run as

txl Numbers.Sort Sort.Txl -Dapply

To see the parse tree for the input, run as

txl Numbers.Sort Sort.Txl -Dparse

Do the same with the other two examples. Following are the commands you will need:

txl TwoVectors.DotProduct DotProduct.Txl
txl TwoVectors.DotProduct DotProduct.Txl -Dapply
txl TwoVectors.DotProduct DotProduct.Txl -Dparse

txl Ultimate.Question Question.Txl
txl Ultimate.Question Question.Txl -Dapply
txl Ultimate.Question Question.Txl -Dparse

Java example

We 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.grm

We 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++;
if (a-1 == b) ...

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.

Step 1

Write a TXL program called Step1.txl that transforms every if statement condition to true.

Test your program with:

txl PlusPlus.java Step1.txl
txl TextTabLine.java Step1.txl

Step 2

Modify 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.txl
txl TextTabLine.java Step2.txl

Step 3

Modify 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.txl

Step 4

Finally, add the a++; statement before the if statement. Call your final program Step4.txl, and test it with

txl PlusPlus.java Step4.txl

What other test cases can you think of?