Compartir a través de


Alternation

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

Rules in MGrammar specify one or more productions. When more than one production is present, they are separated by the "or" symbol (|). The rule is satisfied if any of the productions is satisfied.

Alternation Examples

Alternation can be present in syntax, interleave, and token rules.

Alternation in Token Rules

The following code is an example of alternation in a token rule.

        token AccessValue = 
                            "public" 
                          | "private" 
                          | "internal" 
                          | "protected";

The AccessValue rule is satisfied if any of the literals are present in the input stream. If present, they are replaced with the AccessValue token, unless another token rule takes precedence.

Alternation in Interleave Rules

The following code causes any instances of line feeds, carriage returns, or spaces in the token stream to be ignored.

        token LF                = "\u000A";
        token CR                = "\u000D";
        token Space             = "\u0020";
        interleave Whitespace   = Space | LF | CR;

Alternation in Syntax Rules

The following syntax rule, taken from the grammar that defines the “M” modeling language, specifies four productions for the ModuleMemberDeclaration syntax rule. It essentially specifies the four possible kinds of declarations that can appear inside a module.

        syntax ModuleMemberDeclaration  = ExtentDeclaration
                                        | ComputedValueDeclaration
                                        | TypeDeclaration
                                        | TopScopeGraphInitializationExpression;