Spirit Parser Framework

Spirit Parser Framework

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.

Example


#include
#include
#include
#include

using namespace std;using namespace boost::spirit;

int main(void){ string input; cout << "Input a line. "; getline(cin, input); cout << "Got '" << input << "'. "; 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 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' "; return 0;}

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

External links

* [http://spirit.sourceforge.net Spirit parser framework SourceForge page]
* [http://www.boost.org/libs/spirit/index.html Documentation in the Boost project]
* [http://www.ddj.com/article/printableArticle.jhtml?articleID=184401692&dept_url=/cpp/ Review on Dr. Dobb's Journal]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Spirit (Parser) — Das Spirit Parser Framework ist ein objektorientierter, rekursiv absteigender Parsergenerator der mittels Templatemetaprogrammierung implementiert worden ist. Die Benutzung der Erweiterten Backus Naur Form in C++ wird dem Programmierer mithilfe… …   Deutsch Wikipedia

  • Spirit (disambiguation) — The term spirit has many disparate meanings in various fields. Religion and spirituality *A ghost *In religion and spirituality, the innate essence (soul) of a being *The Holy Spirit or Holy Ghost, in Christianity, one of the three hypostases… …   Wikipedia

  • LL parser — An LL parser is a top down parser for a subset of the context free grammars. It parses the input from Left to right, and constructs a Leftmost derivation of the sentence (hence LL, compared with LR parser). The class of grammars which are… …   Wikipedia

  • Recursive descent parser — A recursive descent parser is a top down parser built from a set of mutually recursive procedures (or a non recursive equivalent) where each such procedure usually implements one of the production rules of the grammar. Thus the structure of the… …   Wikipedia

  • Comparison of parser generators — This is a list of notable lexer generators and parser generators for various language classes. Contents 1 Regular languages 2 Deterministic context free languages 3 Parsing expression grammars, deterministic boolean grammars …   Wikipedia

  • Parsing — In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a sequence of tokens to determine their grammatical structure with respect to a given (more or less) formal grammar.Parsing is also… …   Wikipedia

  • Синтаксический анализ — В информатике, синтаксический анализ (парсинг)  это процесс сопоставления линейной последовательности лексем (слов, токенов) языка с его формальной грамматикой. Результатом обычно является дерево разбора (синтаксическое дерево). Обычно… …   Википедия

  • Грамматический анализ — В информатике, синтаксический анализ (парсинг) это процесс сопоставления линейной последовательности лексем (слов, токенов) языка с его формальной грамматикой. Результатом обычно является дерево разбора. Обычно применяется совместно с лексическим …   Википедия

  • LL-анализатор — Стиль этой статьи неэнциклопедичен или нарушает нормы русского языка. Статью следует исправить согласно стилистическим правилам Википедии …   Википедия

  • Грамматический разбор — В информатике, синтаксический анализ (парсинг) это процесс сопоставления линейной последовательности лексем (слов, токенов) языка с его формальной грамматикой. Результатом обычно является дерево разбора. Обычно применяется совместно с лексическим …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”