Writing Offline FSM Classes - RopleyIT/GLRParser GitHub Wiki

Writing offline state machine classes

An offline state machine is created using the ParseLR.exe command line tool with the -f option, to create source code for a finite state machine. This source file is then added to the project in which the state machine is needed, and compiled to make the executable with an embedded state machine engine. Despite this, there are surprisingly few differences in how the FSM class source code is written relative to an inline FSM.

In an offline state machine, the FSM class source code consists of two classes. For some application-specific state machine whose class name is MyNamespace.MyFSM, the class MyNamespace.Autogenerated.MyFSM_Autogenerated will be created by ParseLR.exe. If MyFSM.cs is the file that contains the application-specific class MyFSM, MyFSM.Designer.cs is created from the input grammar specification by the ParseLR.exe application, and contains the autogenerated derived class. Note that your state machine class should be inherited from the Parsing.FSM library base class as well.

In an offline state machine, there is no need to include assemblyref options at the top of the grammar. It is assumed that any references needed will be added in the Visual Studio (or other C# development environment) project to which the autogenerated code gets added. It is required that you add the autogenerated source code to the same project as the application-specific state machine class MyFSM, so that the factory can be constructed using its parameterless constructor.

An example of the non auto-generated part of the state machine class is given below:

// References will have been added to the project for Parsing.dll and
// ParserGenerator.dll. Suitable using statements appear here:
 
using Parsing;
using ParserGenerator;
 
namespace MyApplication
{
    public class MyFSM
    {
        // Example of a guard function.
        // Note that this, like action functions,
        // could have been written in the grammar
        // under the definition of PluralNoun
        // in the guards section of the grammar.
         
        public bool SafeToCrossRoad()
        {
            return State.Name == "RedWalk";
        }
         
        . . . other application specific members . . .
    }
}

Construction

The constructor for your offline state machine should be just a default constructor, or no constructor at all. This is because the factory class that will create state machine instances expects there to be default constructors at all three levels of the state machine inheritance hierarchy. To initialise your state machine with values before running the state machine, you will have to invoke your own custom initialise method, or assign to properties in the application-specific state machine class after construction.

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