Spirit is implemented by means of Templatemetaprogrammierung recursively descending parser.
The use of the extended Backus -Naur form of C is made possible by using the expression templates.
The parser objects are created by overloading operators and give a LL parser that is able to evaluate ambiguous expressions.
Spirit can be used together and separately for lexical analysis, and also for easy parsing.
The Spirit Parser is a component of the free boost library.
Operators
Because of limitations on the part of the C syntax, the Spirit was built around the operator precedence, stay with similarities to EBNF and regular expressions obtained.
Example
# include
# include
# include
# include
using namespace std;
using namespace boost :: spirit;
int main ( void)
{
string input;
cout << " Enter a line \ n ".;
getline ( cin, input);
cout << "Enter ' " << input << " ' \ n ".;
unsigned count = 0;
/ *
The next line parses the input ( input.c_str ())
using the following semantics
( Indentation corresponds to the source code for clarity ):
Zero or more occurrences of (
Letters " cat " ( if true, increment the counter variable "count" )
or any other character ( proceed to next occurrence of " cat " to find)
)
* /
parse ( input.c_str ()
* ( Str_p ( " cat " ) [ increment_a (count) ]
| anychar_p
) );
/ *
The parser and using operator overloads
Built template matching, i.e.
that the actual
Work in spirit :: parse () will be executed, the expression
starting with *, only initializes the rules
which uses the parsing function.
* /
/ / Show the result finally.
cout << " The input was " << count
<< " Occurrences of ' cat ' \ n";
}
There are other algorithms that are better suited for searching strings.
This example is intended only to illustrate the concept as rules created and these actions can be assigned.