imputil + pyparsing -> Python-based DSL

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Aug 9 19:50:11 EDT 2006


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





More information about the Python-list mailing list