[go: nahoru, domu]

Jump to content

Spirit Parser Framework

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Addbot (talk | contribs) at 03:52, 14 March 2013 (Bot: Migrating 1 interwiki links, now provided by Wikidata on d:q2311085). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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(∞) 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 > y After matching x, expect 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. If x does not match, try to match 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 or both in any order.
x || y Match x or y or x followed by y.
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

This example shows how to construct rules and attach actions to them.

#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_increment_actor.hpp>
#include <string>
#include <iostream>

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") [ increment_a(count) ]
          | 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;
}

External links