Spirit Parser Framework - Example

Example

#include #include #include #include using namespace std; using namespace BOOST_SPIRIT_CLASSIC_NS; int main { string input; cout << "Input a line." << endl; getline(cin, input); cout << "Got '" << input << "'." << endl; unsigned count = 0; /* Next line parses the input (input.c_str), using a parser constructed with the following semantics (indentation matches source for clarity): Zero or more occurrences of ( literal string "cat" ( when matched, increment the counter "count" ) or any character (to move on finding the next occurrence of "cat") ) */ parse(input.c_str, *( str_p("cat") | anychar_p )); /* The parser is constructed by the compiler using operator overloading and template matching, so the actual work is done within spirit::parse, and the expression starting with * only initializes the rule object that the parse function uses. */ // last, show results. cout << "The input had " << count << " occurrences of 'cat'" << endl; return 0; }

Of course, there are better algorithms suited for string searching, but this example gives an idea how to construct rules and attach actions to them.

Read more about this topic:  Spirit Parser Framework

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)