4.3 Starting States

The final type of sections in PyLly are starting state sections. Each start state section is indicated with a list of start state names seperated by commas followed by a colon and a newline. Each line within the indented section specifies a pattern and code to execute when the pattern is matched. The code is specified either as a single line of code immediately following a colon, or the indented block of code following a colon and a newline. For example:

INITIAL :
    "{ID}" :
        idcnt += 1
        return "Identifier"
    "abc*" : return "pattern1"

This section specifies two patterns within the INITIAL start state. When the first pattern is matched, two lines of code are executed. When the second line is matched, just one line of code is executed.

Start states specify seperate state machines. Most lexers will only need one start state, and it may be given any name (excluding definitions and code) as long as it is the first start state in the file. Occasionally a lexer may need to match some patterns in one context and other patterns in another context. Multiple start states provide a mechanism for achieving this. The lexer always starts off in the first start state encountered in the spec file. Helper methods are provided which lexer actions can call to switch the start state.

See the PyGgy Home Page.