StateJava Pre-Processor
I have been doing some work every now and then over the course of the summer for one of my professors, Prof. Sheng Yu, at The University of Western Ontario. The work is based off of one of his papers, Adding States into Object Types, where he develops the τ-calculus as an extension to the ς-calculus of Abadi and Cardelli.
My work was to develop a pre-processor for the JavaTM programming language that turns classes into state machines. A description of how the pre-processor works can be found here: http://ioreader.com/code/python/state-java-pre-processor.pdf.
package example;
public class Auto {
states { :Forward, :Neutral, :Reverse, :Off }
public Auto() :Off {
System.out.println("AUTO: Off (constructor)");
}
/* the turnOff method shows a motivation for having the
ability to remember a previous state. */
public void turnOn() :Off -> :Neutral {
System.out.println("AUTO: On (Neutral).");
}
/* the following methods ass a motivation for having
the ability to perform set operations, such as
difference, on states. */
public void turnOff() :Forward, :Neutral, :Reverse -> :Off {
System.out.println("AUTO: Off.");
}
/* this will be specialized by Car::reverse() for the
transition :Forward -> :Reverse */
public void reverse() :Forward, :Neutral -> :Reverse {
System.out.println("AUTO: Reverse.");
}
public void neutral() :Forward, :Reverse -> :Neutral {
System.out.println("AUTO: Neutral.");
}
/* the following two methods will be merged into one in the
compiled version. */
public void forward() :Neutral -> :Forward {
System.out.println("AUTO: Forward (from Neutral).");
}
public void forward() :Reverse -> :Forward {
System.out.println("AUTO: Forward (from Reverse).");
}
}
package example;
import example.Auto;
public class Car extends Auto {
public Car() :Off {
System.out.println("CAR: Off (constructor)");
}
/* example of state specialization when overwriting
parent class methods. */
public void reverse() :Forward -> :Reverse {
System.out.println("CAR: Reverse (from Forward).");
}
}
package example;
import example.Car;
public class Test {
static public void main(String[] args) {
Car car = new Car();
car.turnOn();
car.forward();
car.reverse(); /* Car::reverse() specialization */
car.neutral();
car.forward();
car.reverse(); /* Auto::reverse() */
car.turnOff();
}
}
When the above code is run through the pre-processor actual JavaTM code is produced. The processed code can then be compiled and run as usual. The following is the output from running the Test class above:
AUTO: Off (constructor) CAR: Off (constructor) AUTO: On (Neutral). AUTO: Forward (from Neutral). CAR: Reverse (from Forward). AUTO: Neutral. AUTO: Forward (from Neutral). CAR: Reverse (from Forward). AUTO: Off.
The code for the pre-processor can be found here: http://ioreader.com/code/python/state-java/
Comments
- posted by Williamson on Sep 11, 2010 at 3:28am
-
posted by Peter Goodman on Sep 11, 2010 at 5:45pm
I recently posted an article on the basic algorithm of PEGs (without followed-by and not-followed-by operators) in my most recent blog post titled PEGs and Left Recursion.
The algorithms I have written on the subject interprets a grammar data structure. The interpretation is similar to how a recursive descent parser works, with the exception that the algorithms caches the results of each time a non-terminal of the grammar is interpreted at a particular point in the input stream.
The algorithm written about in the post roughly corresponds to the workings of this implementation of PEGs without the aforementioned operators.
In the past, I've found it easier to implement PEG using this formulation to gain power equivalent to the aforementioned operators.
If you have further questions on this subject, then feel free to ask them in the comments section of my most recent blog post about PEGs.
Comment

Thanx