boost-spirit-qi

parsing a simple repeated text macro with Boost.spirit

主宰稳场 提交于 2019-12-23 18:23:02
问题 I'm learning how to use Boost.Spirit library for parsing strings. It seems to be a very nice tool but difficult as well. So, I want to parse a string with some words separated with / and put them in a vector of strings. Here is an example: word1/word2/word3 . That's a simple task, I can do this with the following finction: bool r = phrase_parse(first, last, (+~char_("/") % qi::lit("/")),space,v) where v is std::vector<std::string> . But in general, I'd like to parse something like w1/[w2/w3]2

Boost spirit semantic actions on qi::rule

旧巷老猫 提交于 2019-12-23 17:52:19
问题 I've been reading up on semantic actions and I have a rule that looks like this: property_rule %= identifier_rule % ',' >> lit(L":") >> type_specification_rule >> -(lit(L":=") >> +(alnum - ';')) >> lit(L";"); The property_rule is defined as qi::rule<Iterator, property(), space_type> property_rule; Now, I also want to support operator ≡ so what I want is to change the rule to something like ... >> -(( lit(L":=") || lit(L"≡")[SEMANTIC_ACTION_HERE]) >> +(alnum - ';')) ... In the semantic action,

boost spirit, recursion and stack overflow

霸气de小男生 提交于 2019-12-23 14:57:03
问题 Why does the following code crash in run-time (it'll give a stack overflow error)? #include <boost/any.hpp> #include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> #include <boost/fusion/include/define_struct.hpp> #include <boost/fusion/adapted.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/variant.hpp> #include <iostream> #include <string> #include <vector> namespace qi = boost::spirit::qi; // Helper structs // types enum class types { void_t, int_t, double_t,

Expression grammar with exponentiation operator using Boost Spirit

戏子无情 提交于 2019-12-23 12:22:05
问题 I would like to add the exponentiation operator to the expression grammar provided in the Boost spirit samples. The BNF grammar is the following: (see this answer for example: "Unambiguous grammar for exponentiation operation" ) E -> E + T | E - T | T T -> T * F | T / F | X X -> X ^ Y | Y Y -> i | (E) which I translated to Boost spirit like this: template <typename Iterator> struct calculator : qi::grammar<Iterator, ascii::space_type> { calculator() : calculator::base_type(expression) { qi:

error handling and annotation in Boost.Spirit X3

大城市里の小女人 提交于 2019-12-23 09:55:56
问题 What is the logic under using boost::spirit::x3::position_tagged as base class for some AST nodes (how to choose which should be tagged, e.g. for C-like language?) and other constructions, used in rule ID definition, like: struct error_handler_tag; struct error_handler_base { template< typename Iterator, typename Exception, typename Context > x3::error_handler_result on_error(Iterator & /*first*/, Iterator const & /*last*/, Exception const & x, Context const & context) { std::string message_

Searching/Iterating boost::spirit::qi::symbols

旧城冷巷雨未停 提交于 2019-12-23 09:52:10
问题 If I have a symbol table: struct MySymbols : symbols<char, MyEnum::Fruits> { MySymbols () : symbols<char, MyEnum::Fruits>(std::string("MySymbols")) { add("apple", MyEnum::Apple) ("orange", MyEnum::Orange); } }; I want to iterate over the table in order to search for a symbol by data value. I cannot use lambda expressions so I implemented a simple class: template<typename T> struct SymbolSearcher { SymbolSearcher::SymbolSearcher(T searchFor) : _sought(searchFor) { // do nothing } void operator

Boost Spirit grammar eol

拥有回忆 提交于 2019-12-23 03:05:45
问题 I am trying to parse files of the following form: // comment bla bla [sectionname] key = value key2=value2 // comment key = value [anothersection] ... using the following code. Unfortunately, it reports the last eol as an error although all eols at the end should be accepted by: (*qi::eol > -(sectionGrammar > *(+qi::eol > sectionGrammar)) > *qi::eol), Besides I really don't know how to parse comments properly without taking the eol which is required for the next key-value pair which is the

Boost Spirit char parser

血红的双手。 提交于 2019-12-23 02:44:43
问题 Here is a code sample: // file main.cpp #include <iostream> #include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple_io.hpp> #include <boost/spirit/include/qi.hpp> int main() { std::string s( "1 A" ); boost::tuple<double, char> p; complex_matrix_parser::iterator b = s.begin(); complex_matrix_parser::iterator e = s.end(); qi::phrase_parse( b, e, ( qi::double_ >> qi::char_('A') ), qi::space, qi::skip_flag::postskip, p ); std::cerr << "==== " << p << std::endl; return 0; } This should print

qi::rule with inherited attribute as inherited attribute

混江龙づ霸主 提交于 2019-12-22 18:43:20
问题 Let's say we have a rule1 qi::rule<std::string::iterator, int()> rule1 = qi::int_[qi::_val=qi::_1]; And we decide getting an int as attribute is not enough, we also want to get the raw data (boost::iterator_range). We may have a lot of rules with the same type as rule1. So it's better to have a generic solution for this. Therefore we could define another rule2. qi::rule< std::string::iterator, std::pair<int, boost::iterator_range<std::string::iterator>>( qi::rule<std::string::iterator, int()>

Boost spirit parsing string with leading and trailing whitespace

假装没事ソ 提交于 2019-12-22 18:03:38
问题 I am still new to Boost spirit. I am trying to parse a string with possible lead and trailing whitespace and intermediate whitespace. I want to do the following with the string Remove any trailing and leading whitespace Limit the in-between word spaces to one whitespace For example "( my test1 ) (my test2)" gets parsed as two terms - "my test1" "my test2" I have used the following logic using boost::spirit::qi; struct Parser : grammar<Iterator, attribType(), space_type> { public: Parser() :