State Machines - RopleyIT/GLRParser GitHub Wiki

Finite State Machines

Simple finite state machines can also be generated from grammar scripts using the classes and tools provided. These state machines follow the conventional event-driven model. A machine when in a particular state is performing no work, but awaiting one of a number of possible types of input event. When an event arrives, the machine tests zero or more guard conditions associated with that type of event for the current state. If an event-guard condition pair are found to match, a transition is made to a new state. In making the transition, one or more actions associated with that transition can be executed.

A more detailed description of this style of state machine can be found in UML documentation to be found on the Web. The subset of state machine implemented in this tool is one in which event-condition pairs are used to determine which transition to take between states; Entry and exit actions can be specified for each state, as well as actions on the transitions themselves; Nested state machines are possible if a new FSM (finite state machine) instance is launched as the entry action for a superstate; state machines are terminating, meaning that they are created already in a starting state, and are allowed to include transitions that terminate the processing of further input events.

Specifying finite state machines in a grammar

The grammar rules for a state machine are the same as for an LR(1) parser grammar, except for the grammar rules section. Instead of a grammar section, a section using the keyword fsm or statemachine is used:

events
{
    TICK,
    TOCK,
    STOPALARM,
    END
}
guards
{
    AlarmTimeReached,
    AtQuarterHour
}
actions
{
    Advance,
    AlarmOn,
    Chime,
    AlarmOff
}
fsm(AwaitingTick)
{
    AwaitingTock :
        entry = Advance
        TOCK AwaitingTick
    ;
 
    AwaitingTick :
        TICK[AlarmTimeReached] AwaitingTock
            = AlarmOn
    |   TICK[AtQuarterHour & !AlarmTimeReached] AwaitingTock
            = Chime
    |   STOPALARM AwaitingTick
            = AlarmOff
    |   TICK AwaitingTock
    |   END
    ;
}

Note that the fsm or statemachine keyword is followed by an identifier in parentheses. This identifier declares which state will be the starting state for instances of the state machine when they are created and before they have processed any input events. This identifier will also appear to the left of a transition rule later in the fsm section, representing one of the states of the state machine.

⚠️ **GitHub.com Fallback** ⚠️