imputil + pyparsing -> Python-based DSL

P

Paul McGuire

Wilson Fowlie sent me an e-mail describing James Theile's presentation at
the Vancouver Python Workshop, using imputil to create simple DSL's. I
thought that by creating a DSL grammar and making it part of an imputil
hook, you could generate Python source code to implement the corresponding
classes and methods to implement the DSL behavior.

My first experiment was a state machine generator. From a simple state
machine for a traffic light:

TrafficLight = {
Red -> Green;
Green -> Yellow;
Yellow -> Red;
}

My imputil import hook generates the corresponding classes and state
transition logic to implement the state machine, enabling this code:

import stateMachine
import trafficLight

tl = trafficLight.Red()
for i in range(10):
print tl,
print ("STOP","GO")[tl.carsCanGo]
tl.crossingSignal()
tl.delay()
print

tl = tl.nextState()


This page has more examples, plus the source code:
http://www.geocities.com/ptmcg/python/stateMachine.html.

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top