BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help
Not What You Meant?  There are 49 definitions for Spirit.

Spirit Parser Framework

Print-Friendly
About 2 pages (435 words)

Bookmark and Share Know this topic well? Help others and get FREE products!

The Spirit Parser Framework is an object oriented recursive descent parser generator framework implemented using template metaprogramming techniques. Expression templates allow users to approximate the syntax of Extended Backus Naur Form (EBNF) completely in C++. Parser objects are composed through operator overloading and the result is a backtracking LL(inf) parser that is capable of parsing rather ambiguous grammars. Spirit can be used for both lexing and parsing, together or separately. This framework is part of the Boost libraries.

Operators

Because of limitations of the C++ language, the syntax of Spirit has been designed around the operator precedences of C++, while bearing resemblance to both EBNF and regular expressions.

syntax explanation
x >> y Match x followed by y.
*x Match x repeated zero or more times. (This is representing the Kleene star; C++ lacks a unary postfix operator *)
x | y Match x or y.
+x Match x repeated one or more times.
 !x Match x zero or one time.
x & y Match x and y.
x - y Match x but not y.
x ^ y Match x or y but not both.
x [ function_expression ] Execute the function/functor returned by function_expression, if x matched.
( x ) Match x (can be used for priority grouping)
x % y Match one or more repetitions of x, separated by occurrences of y.
~x Match anything but x (only with character classes such as ch_p or alnum_p)

Example

#include <boost/spirit.hpp>
#include <boost/spirit/actor.hpp>
#include <string>
#include <iostream>
using namespace boost;
int main(void)
{
    std::string input;
    
    std::cout << "Input a line.\n";
    std::getline(std::cin, input);
    
    std::cout << "Got '" << input << "'.\n";
    
    unsigned count = 0;
 /*  
    Next line parses the input (input.c_str()),
        using a parser constructed with the following semantics
        (identation matches source for clarity):
     Zero or more occurances of (
          literal string "cat" ( when matched, increment the counter "count" )
      or  any character (to move on finding the next occurance of "cat")
     )
 */
     spirit::parse(input.c_str(),
        *(  spirit::str_p("cat") [ spirit::increment_a(count) ]
          | spirit::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.
    std::cout << "The input had " << count
              << " occurrences of 'cat'\n";
    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.

External links

View More Summaries on Spirit Parser Framework
 
Ask any question on Spirit Parser Framework and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
Spirit Parser Framework from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy