boost-spirit

Constraining the existing Boost.Spirit real_parser (with a policy)

笑着哭i 提交于 2019-12-17 14:21:10
问题 I want to parse a float, but not allow NaN values, so I generate a policy which inherits from the default policy and create a real_parser with it: // using boost::spirit::qi::{real_parser,real_policies, // phrase_parse,double_,char_}; template <typename T> struct no_nan_policy : real_policies<T> { template <typename I, typename A> static bool parse_nan(I&, I const&, A&) { return false; } }; real_parser<double, no_nan_policy<double> > no_nan; // then I can use no_nan to parse, as in the

Boost::Spirit Expression Parser

天涯浪子 提交于 2019-12-17 09:45:20
问题 I have another problem with my boost::spirit parser. template<typename Iterator> struct expression: qi::grammar<Iterator, ast::expression(), ascii::space_type> { expression() : expression::base_type(expr) { number %= lexeme[double_]; varname %= lexeme[alpha >> *(alnum | '_')]; binop = (expr >> '+' >> expr)[_val = construct<ast::binary_op<ast::add>>(_1,_2)] | (expr >> '-' >> expr)[_val = construct<ast::binary_op<ast::sub>>(_1,_2)] | (expr >> '*' >> expr)[_val = construct<ast::binary_op<ast:

Understanding Boost.spirit's string parser

拈花ヽ惹草 提交于 2019-12-17 07:50:01
问题 #include <iostream> #include <boost/spirit/include/qi.hpp> namespace qi = boost::spirit::qi; int main () { using qi::string; std::string input("a"); std::string::iterator strbegin = input.begin(); std::string p; bool ok = qi::phrase_parse(strbegin, input.end(), ((string("a") >> string("a")) | string("a")), qi::space, p); if (ok && strbegin == input.end()) { std::cout << p << std::endl; std::cout << p.size() << std::endl; } else { std::cout << "fail" << std::endl; std::cout << std::string

Compiling a simple parser with Boost.Spirit

醉酒当歌 提交于 2019-12-17 07:49:43
问题 Part of a simple skeleton utility I'm hacking on I have a grammar for triggering substitutions in text. I thought it a wonderful way to get comfortable with Boost.Spirit, but the template errors are a joy of a unique kind. Here is the code in its entirety: #include <iostream> #include <iterator> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> namespace bsq = boost::spirit::qi; namespace { template<typename Iterator> struct skel_grammar : public bsq::grammar

undefined behaviour somewhere in boost::spirit::qi::phrase_parse

大憨熊 提交于 2019-12-17 06:56:23
问题 I am learning to use boost::spirit library. I took this example http://www.boost.org/doc/libs/1_56_0/libs/spirit/example/qi/num_list1.cpp and compiled it on my computer - it works fine. However if I modify it a little - if I initialize the parser itself auto parser = qi::double_ >> *(',' >> qi::double_); somewhere as global variable and pass it to phrase_parse, everything goes crazy. Here is the complete modified code (only 1 line is modified and 1 added) - http://pastebin.com/5rWS3pMt If I

boost spirit, boost any and quoted string - compile-time error

梦想与她 提交于 2019-12-13 18:10:01
问题 I have the following code: #include <boost/any.hpp> #include <boost/spirit/include/qi.hpp> #include <iostream> #include <string> template <typename Iterator> struct parser : boost::spirit::qi::grammar<Iterator, boost::any(), boost::spirit::qi::ascii::space_type> { parser() : parser::base_type(start) { start %= boost::spirit::qi::int_ | boost::spirit::qi::lexeme['"' >> +(boost::spirit::qi::char_ - '"') >> '"']; // example: 0 or "str" } boost::spirit::qi::rule<Iterator, boost::any(), boost:

X3: How to create parser to read in sets?

梦想与她 提交于 2019-12-13 14:26:50
问题 How would one create a rule for reading integers in sets of 3. I.e., ... 1 2 3 OK, 1 set of 3 ints 1 2 3 4 5 6 OK, 2 sets of 3 ints 1 2 3 4 5 ERROR, 1 set of 3 ints, 1 short for 2nd 1 2 3 4 5 6 7 8 9 OK, 3 sets of 3 ints 1 2 3 4 5 6 7 8 9 10 ERROR, 3 sets of 3 ints, 1 short for 4th I'm having issue with the fusion adapt struct (how to make the number of args variable) ... BOOST_FUSION_ADAPT_STRUCT(client::ast::number, n1, n2, n3, n4, n5, n6) And not sure why this rule wouldn't work. ... = *

Statefulness of Spirit V2 and X3

大城市里の小女人 提交于 2019-12-13 08:11:18
问题 What is the intent of Spirit X3 being so much 'stateless'? Bad aspects of 'states' in Spirit V2 Looking back to Spirit V2, the "grammar" was, say, conceptually stateful - in many ways. This is because the grammar was a class instance. Basically, there are lots of bad aspects for making your grammar -- or even any single rule -- to be stateful: It might make your grammar non-re-entrant; It might add thread-unsafety to your grammar instance; Self-managed 'flag' is a disaster. Theoretically

how to change the parser of a rule

帅比萌擦擦* 提交于 2019-12-13 03:19:36
问题 Is it possible to modify the parser of a rule at runtime? I am able to dynamically create parsers (classes that are derived from parser_base), but I dont know how I can assign the new parser to an existing rule. Basically my problem is that I want to define a parser for a line that consists of elements like numbers (lets call them constants in the sense that they are invariant over all my parser input) and symbols (the variants that I want to capture with a dynamic parser approach). Since the

Map input to ast types in boost spirit

不羁的心 提交于 2019-12-13 02:40:09
问题 I want to implement color highlighting of input string that is being fed to given spirit grammar. Is there an easy (or any, if not easy) way to map given character from input into rule/ast type that it matches? Preferable in form of array/vector of rule/ast types where index is index of character of input string. Or maybe better - iterator rages to ast types. 回答1: Of course there is. Several answers on this site demonstrate similar things. You'll have to decide how you want to treat subrules.