boost-spirit-qi

Segmentation fault with trivial Spirit Parser grammar

霸气de小男生 提交于 2019-12-05 19:07:41
I'm running into frequent segfaults with my Spirit Qi parser. After spending days to debug the issue (I found the stacktraces impossible to grok) I decided to trim it down to a minimal example. Can anyone tell what I'm doing wrong, if anything? Save code as bug.cpp, compile with g++ -Wall -o bug bug.cpp and you should be good to go. //#define BOOST_SPIRIT_DEBUG_PRINT_SOME 80 //#define BOOST_SPIRIT_DEBUG #include <boost/spirit/version.hpp> #include <boost/spirit/include/qi.hpp> #include <iostream> #include <fstream> #include <iterator> #include <string> namespace /*anon*/ { using namespace

Deeply-recursive qi grammars (parsers) with synthesized and inherited attributes

强颜欢笑 提交于 2019-12-05 19:03:15
I'm using spirit::qi grammars that construct and return nontrivial objects as their synthesized attributes. The problem is that I want the grammars to recursively depend on each other. It is straightforward to use recursive rules , but I want recursive grammars . Here's some example code. Notice the comments that say CIRCULAR REFERENCE. Obviously this cannot compile if I uncomment those lines, because the FooGrammar and BarGrammar objects include each other. I can think of two ways to achieve the desired effect, but I can't seem to make them work with qi: Make the BarGrammar hold a (smart)

How do I parse end-of-line with boost::spirit::qi?

匆匆过客 提交于 2019-12-05 18:21:30
问题 Shouldn't a simple eol do the trick? #include <algorithm> #include <boost/spirit/include/qi.hpp> #include <iostream> #include <string> using boost::spirit::ascii::space; using boost::spirit::lit; using boost::spirit::qi::eol; using boost::spirit::qi::phrase_parse; struct fix : std::unary_function<char, void> { fix(std::string &result) : result(result) {} void operator() (char c) { if (c == '\n') result += "\\n"; else if (c == '\r') result += "\\r"; else result += c; } std::string &result; };

in boost::spirit::qi, is it possible to dynamically modify rule definition in runtime

♀尐吖头ヾ 提交于 2019-12-05 17:37:26
I wrote some grammar with boost::spirit::qi::rule to parse the internet packet. the grammar is something like: qi::rule<Iterator> start, request, response, status, query ; start = (request | response | status | query) >> lit("\r\n"); to improve the performance, user maybe want to skip some rules in the runtime, e.g. ignore "response","status","query" and only try to match request, so the rule will change to: start = (request ) >> lit("\r\n"); is it possible to do that? e.g, is there a function like "disable()" to just disable the rule "response", "status" and "query"? sehe The most natural

how to parse and verify an ordered list of integers using qi

穿精又带淫゛_ 提交于 2019-12-05 08:53:33
I'm parsing a text file, possibly several GB in size, consisting of lines as follows: 11 0.1 14 0.78 532 -3.5 Basically, one int and one float per line. The ints should be ordered and non-negative. I'd like to verify the data are as described, and have returned to me the min and max int in the range. This is what I've come up with: #include <iostream> #include <string> #include <boost/spirit/include/phoenix.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/fusion/include/std_pair.hpp> namespace px = boost::phoenix; namespace qi = boost::spirit::qi; namespace my_parsers { using

How to use boost::spirit to parse a sequence of words into a vector?

独自空忆成欢 提交于 2019-12-05 05:09:33
I'm trying to learn boost::spirit . As an example, I'm trying to parse a sequence of words into a vector<string> . I tried this: #include <boost/spirit/include/qi.hpp> #include <boost/foreach.hpp> namespace qi = boost::spirit::qi; int main() { std::vector<std::string> words; std::string input = "this is a test"; bool result = qi::phrase_parse( input.begin(), input.end(), +(+qi::char_), qi::space, words); BOOST_FOREACH(std::string str, words) { std::cout << "'" << str << "'" << std::endl; } } which gives me this output: 'thisisatest' but I wanted the following output, where each word is matched

How can I extend a boost spirit grammar

一曲冷凌霜 提交于 2019-12-05 01:00:30
问题 The thing is that I made a grammar that has been useful for a task, but now the task has changed and I need to define new rules. But I wouldn't like to modify the grammar I already have instead of that I'd like to create a new grammar that uses the existing grammar I have without code duplication, so I just need to define the new rules I need. I tried something like this, but is not working : struct New_grammar : Old_grammar<Iterator, Skipper> { New_grammar() : New_grammar::base_type(Command

Boost::Spirit::Qi. How to turn inlined parser expressions into standalone grammars, and how to unpack the tuples generated by them?

巧了我就是萌 提交于 2019-12-04 19:19:57
问题 I'm using QI and Phoenix, and I want to write a small grammar that returns 4 bools which are to be used as arguments for a function call inside a semantic action. I have several functions that need those things, and so far I have used this approach: ( qi::_bool >> qi::_bool >> qi::_bool >> qi::_bool) [px::bind(&Bool4Function, spirit::_val, spirit::_1, spirit::_2, spirit::_3, spirit::_4)] and while it's okay on it's own, using it all over the place is just plain ugly and confusing, even with

Splitting string using boost spirit

半世苍凉 提交于 2019-12-04 18:44:55
Is it a good idea? For a reason I thought it should be faster than boost's tokenizer or split. however most of the time I'm stuck in the boost::spirit::compile template <typename Iterator> struct ValueList : bsq::grammar<Iterator, std::vector<std::string>()> { ValueList(const std::string& sep, bool isCaseSensitive) : ValueList::base_type(query) { if(isCaseSensitive) { query = value >> *(sep >> value); value = *(bsq::char_ - sep); } else { auto separator = bsq::no_case[sep]; query = value >> *(separator >> value); value = *(bsq::char_ - separator); } } bsq::rule<Iterator, std::vector<std:

Searching for Holy Grail of search and replace in C++

拜拜、爱过 提交于 2019-12-04 14:59:20
Recently I was looking for a way to replace tokens in string which is essentially find and replace (but there is at least one additional approach to the problem) and looks like quite banal task. I've came with couple of possible implementations but none of them was satisfying from performance point of view. Best achievement was ~50us per iteration. The case was ideal, the string was never growing in size and initially I've omitted the requirement to be case insensitive Here is the code at Coliru Results from my machine: Boost.Spirit symbols result: 3421?=3421 100000 cycles took 6060ms. Boyer