| The Rule |

The rule is a polymorphic parser that acts as a named place-holder capturing the behavior of an EBNF expression assigned to it. Naming an EBNF expression allows it to be referenced later. The `rule` is a template class parameterized by the type of the scanner (`ScannerT`) , the rule's context and its tag. Default template parameters are provided to make it easy to use the rule.

        template<
            typename ScannerT = scanner<>,
            typename ContextT = parser_context,
            typename TagT = parser_address_tag>
        class rule;

Default template parameters are supplied to handle the most common case. `ScannerT` defaults to `scanner<>`, a plain vanilla scanner that uses `char const*` iterators and does nothing special at all other than iterate through all the chars in the null terminated input a character at a time. The rule tag, `TagT`, typically used with ASTs, is used to identify a rule. It is explained here. In trivial cases, declaring a rule as `rule<>` is enough. You need not be concerned at all with the `ContextT` template parameter unless you wish to tweak the low level behavior of the rule. Detailed information on the `ContextT` template parameter is provided elsewhere.

Presented above is the public API. There may actually be more template parameters after `ContextT`. Everything after the `ContextT` parameter should not be of concern to the client and are strictly for internal use only.

The rule class models EBNF's production rule. Example:

        rule<> a_rule = *(a | b) & +(c | d | e);

The type and functionality of the right-hand (rhs) EBNF expression, which may be arbitrarily complex, is encoded in the rule named a_rule. a_rule may now be referenced elsewhere in the grammar:

        rule<> another_rule = f >> g >> h >> a_rule;

Referencing rules When a rule is referenced anywhere in the right hand side of an EBNF expression, the rule is held by the expression by reference. It is the responsibility of the client to ensure that the referenced rule stays in scope and does not get destructed while it is being referenced.

        a = int_p;
        b = a;
        c = int_p >> b;

### Forward declarations

A `rule` may be declared before being defined to allow cyclic structures typically found in BNF declarations. Example:

        rule<> a, b, c;

        a = b | a;
        b = c | a;

### Recursion

The right-hand side of a rule may reference other rules, including itself. The limitation is that direct or indirect left recursion is not allowed (this is an unchecked run-time error that results in an infinite loop). This is typical of top-down parsers. Example:

        a = a | b; // infinite loop!

What is left recursion? Left recursion happens when you have a rule that calls itself before anything else. A top-down parser will go into an infinite loop when this happens. See the FAQ for details on how to eliminate left recursion.

### Undefined rules

An undefined rule matches nothing and is semantically equivalent to `nothing_p`.

### Redeclarations

Like any other C++ assignment, a second assignment to a rule is destructive and will redefine it. The old definition is lost. Rules are dynamic. A rule can change its definition anytime:

        r = a_definition;
        r = another_definition;

Rule `r` loses the old definition when the second assignment is made. As mentioned, an undefined rule matches nothing and is semantically equivalent to `nothing_p`.

### Dynamic Parsers

Hosting declarative EBNF in imperative C++ yields an interesting blend. We have the best of both worlds. We have the ability to conveniently modify the grammar at run time using imperative constructs such as `if`, `else` statements. Example:

        if (feature_is_available)
            r = add_this_feature;

Rules are essentially dynamic parsers. A dynamic parser is characterized by its ability to modify its behavior at run time. Initially, an undefined rule matches nothing. At any time, the rule may be defined and redefined, thus, dynamically altering its behavior.

### No start rule

Typically, parsers have what is called a start symbol, chosen to be the root of the grammar where parsing starts. The Spirit parser framework has no notion of a start symbol. Any rule can be a start symbol. This feature promotes step-wise creation of parsers. We can build parsers from the bottom up while fully testing each level or module up untill we get to the top-most level.

### Parser Tag

Rules may be tagged for identification purposes. This is necessary, especially when dealing with parse trees and ASTs to see which rule created a specific AST/parse tree node. Each rule has an ID of type `parser_id`. This ID can be obtained through the rule's `id()` member function:

        my_rule.id(); //  get my_rule's id

The `parser_id` class is declared as:

        class parser_id
        {
        public:
                        parser_id();
            explicit    parser_id(void const* p);
                        parser_id(long l);

            bool        operator==(parser_id const& x) const;
            bool        operator!=(parser_id const& x) const;
            bool        operator<(parser_id const& x) const;
            long        to_long() const;
        };

The rule's `TagT` template parameter supplies this ID. This defaults to `parser_address_tag`. The `parser_address_tag` uses the address of the rule as its ID. This is not often the most convenient, since it is not always possible to get the address of a rule to compare against. It is possible to have specific constant integers to identify a rule. For this purpose, we can use the `parser_tag<N>`, where N is a constant integer:

        rule<scanner<>, parser_context, parser_tag<123> > my_rule; //  set my_rule's id to 123

Copyright © 1998-2003 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)