Grammar Actions - RopleyIT/GLRParser GitHub Wiki

The actions section

When writing a parser grammar, any actions to be taken on recognition of input grammar rules can be written in-line in the grammar as fragments of C# source code. In this case no actions section needs to be provided.

Alternatively, the author of the parser can write action functions inside the user's parser class, each with the correct signature. In this case, the actions section will be needed. The actions section is where we list the names of any action functions written separately in the parser source code, so that the grammar can make calls on them by name.

If written separately in the parser class, each action function must have the same name as one of the listed actions in the actions section. It must also have the same signature as the Action<object[]> delegate, as seen in the following example:


namespace MyApplication
{
    public partial class MyParser
    {
        ... other things ...
         
        public void InsertBolt(object[] argValues)
        {
            ... perform some action compatible with
                recognition of a complete rule  ...
        }
         
        ... other action methods ...
    }
}

Note that the parameters to the action function are values specifically selected from the tokens within a grammar rule's right hand side, that is about to be reduced to its left hand side single element. The selection mechanism through which specific token values are passed to the action function's object array is explained later, where in-line actions are discussed.

Example of actions section


actions
{
    InsertBolt,
    AttachNut,
    PaintUndercoat,
    DrillHole
}
⚠️ **GitHub.com Fallback** ⚠️