标签: boost-spirit

Boost.Spirit.Qi用法中的错误雪崩

我无法弄清楚我的代码有什么问题.Boost的模板让我变得疯狂!我不能在这一切中做出正面或反面,所以我不得不问.

这有什么问题?

#include <iostream>
#include <boost/lambda/lambda.hpp>
#include <boost/spirit/include/qi.hpp>

void parsePathTest(const std::string &path)
{
    namespace lambda = boost::lambda;
    using namespace boost::spirit;

    const std::string permitted = "._\\-#@a-zA-Z0-9";
    const std::string physicalPermitted = permitted + "/\\\\";
    const std::string archivedPermitted = permitted + ":{}";

    std::string physical,archived;

    // avoids non-const reference to rvalue
    std::string::const_iterator begin = path.begin(),end = path.end();

    // splits a string like "some/nice-path/while_checking:permitted#symbols.bin"
    // as physical = "some/nice-path/while_checking"
    // and archived = "permitted#symbols.bin" (if this portion exists)
    // I could barely find out the …
Run Code Online (Sandbox Code Playgroud)

c++ boost-spirit boost-spirit-qi c++11

3
推荐指数
1
解决办法
1240
查看次数

C++ boost :: lambda :: ret等效于凤凰

Boost lambda允许使用ret<T>模板覆盖推断的返回类型.我曾尝试在凤凰中搜索等效物,但找不到它.

在凤凰中有相同的东西吗?我知道怎么做自己的替换,但我宁愿不做.谢谢

c++ boost-spirit boost-lambda boost-phoenix

3
推荐指数
1
解决办法
905
查看次数

分裂错误与琐碎的Spirit Parser语法

我正在使用我的Spirit Qi解析器遇到频繁的段错误.

花了好几天来调试问题(我发现堆栈跟踪无法理解)我决定将其修改为一个最小的例子.任何人都能说出我做错了什么,如果有的话?

将代码保存为bug.cpp,编译,g++ -Wall -o bug bug.cpp你应该好好去.

//#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 boost::spirit::qi;

    template <typename Iterator, typename
        Skipper> struct bug_demo : 
            public grammar<Iterator, Skipper>
    {
        bug_demo() : 
            grammar<Iterator, Skipper>(story, "bug"),
            story(the),
            the("the")
        {
//          BOOST_SPIRIT_DEBUG_NODE(story);
//          BOOST_SPIRIT_DEBUG_NODE(the);
        }

        rule<Iterator, Skipper> story, the;
    };

    template <typename It>
        bool do_parse(It begin, It end)
    {
        bug_demo<It, space_type> grammar;
        return phrase_parse(begin, end, grammar, space); …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-spirit boost-spirit-qi

3
推荐指数
1
解决办法
805
查看次数

boost :: bind不编译

我是新手来提升精神,我有以下问题:

#include <string>
#include <vector>
#include <boost/spirit/include/qi.hpp>

#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>

#include <boost/bind.hpp>

using namespace boost::spirit;
using namespace std;

struct MyGrammar
    : qi::grammar<string::const_iterator, string(), ascii::space_type> {
    MyGrammar();

    void myFun(const string& s);

    private:
    qi::rule<string::const_iterator, string(), ascii::space_type> myRule;
};



using namespace boost::spirit;
using namespace std;

MyGrammar::MyGrammar() : MyGrammar::base_type(myRule) {
    using qi::_1;

    myRule = int_ [boost::bind(&MyGrammar::myFun, this, _1)]; // fails
    myRule = int_ [_val = _1];  // fine
}

void MyGrammar::myFun(const string& s){
    cout << "read: " << s << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-bind boost-spirit

3
推荐指数
1
解决办法
1875
查看次数

如何摆脱带有spirit :: lex的令牌中的转义字符?

我想标记我自己的SQL语法扩展.这涉及识别双引号字符串中的转义双引号.例如在MySQL中,这两个字符串标记是等价的:( """"第二个双引号充当转义字符)和'"'.我尝试了不同的东西,但我仍然坚持如何替换令牌的价值.

#include <boost/spirit/include/lex_lexertl.hpp>
namespace lex = boost::spirit::lex;

template <typename Lexer>
struct sql_tokens : lex::lexer<Lexer>
{
  sql_tokens()
  {
    string_quote_double = "\\\"";    // '"'

    this->self("INITIAL")
      = string_quote_double [ lex::_state = "STRING_DOUBLE" ] // how to also ignore + ctx.more()?
      | ...
      ;

    this->self("STRING_DOUBLE") 
      = lex::token_def<>("[^\\\"]*") // action: ignore + ctx.more()
      | lex::token_def<>("\\\"\\\"") // how to set token value to '"' ?
      | lex::token_def<>("\\\"") [ lex::_state = "INITIAL" ]
      ;
  }

  lex::token_def<> string_quote_double, ...;
};
Run Code Online (Sandbox Code Playgroud)

那么如何将令牌的值设置为何""" …

c++ boost-spirit boost-spirit-lex

3
推荐指数
1
解决办法
545
查看次数

提高规则的精神排列

我有两种类型的表达式,我想解析并计算结果.

  • 拟合表达式:+, - ,*,/和sqrt()函数; 例如:"2 + 3*sqrt(100*25)" - >应计算为152

  • 功能:GetSubString()和ConcatenateStrings()如: "GetSubString( '100str1',0,3)" - >应被计算为100

我有2个单独的语法来解析这些表达式类型.现在我想结合这两个语法,并可以将这些表达式一起定义.

例如:

  • "GetSubString('100str1',0,2 + 1)+ sqrt(9)" - > result = 103
  • "2 + 3*sqrt(GetSubString('100str1',0,2 + 1))" - >结果= 32

我试图通过使用置换运算符组合下面的2个语法.但它没有编译.

    expr_    =
        ( *( (function_call_ ^ arithmeticexpression_)| string_ ));
Run Code Online (Sandbox Code Playgroud)

那么这是一个合并我的function_call_和arithmeticexpression_规则的正确方法,或者我该怎么做?

typedef boost::variant<int, float, double, std::wstring> RetValue;


RetValue CTranslationFunctions::GetSubString(RetValue const& str, RetValue position, RetValue len)
{
    std::wstring strToCut;
    size_t posInt = 0;
    size_t lenInt = 0;

    try
    {
        strToCut = boost::get<std::wstring>(str);
        posInt = boost::get<int>(position);
        lenInt = boost::get<int>(len); …
Run Code Online (Sandbox Code Playgroud)

c++ parsing boost-spirit boost-phoenix boost-spirit-qi

3
推荐指数
1
解决办法
351
查看次数

如何使用boost :: spirit解析数学表达式并将其绑定到函数

我想定义一个带有2个参数的函数

 double func(double t, double x); 
Run Code Online (Sandbox Code Playgroud)

从外部文本文件中读取实际实现的位置.
例如,在文本文件中指定

function = x*t;    
Run Code Online (Sandbox Code Playgroud)

功能应该实现的乘法xt,以便它可以在稍后阶段被调用.我正在尝试使用boost :: spirit来解析函数.但我不知道如何实现它.

下面,我创建了一个实现乘法的简单函数.我将它绑定到boost函数,我可以使用它.我还创建了一个简单的语法,它解析了两个双精度数之间的乘法.

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include "boost/function.hpp"
#include "boost/bind.hpp"
#include <boost/spirit/include/qi_symbols.hpp>
#include <iostream>
#include <string>

namespace qi = boost::spirit::qi;
namespace ascii=boost::spirit::ascii;
using boost::spirit::ascii::space;
using boost::spirit::qi::symbols;

template< typename Iterator >
struct MyGrammar : public virtual qi::grammar<  Iterator,  ascii::space_type >
{
    MyGrammar() : MyGrammar::base_type(expression)
    {
        using qi::double_;
        //The expression should take x and t as symbolic expressions
        expression = (double_ >> '*' …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-bind boost-spirit boost-function

3
推荐指数
1
解决办法
3765
查看次数

使用Boost.Spirit仅解析特定数字

如何构建仅匹配特定范围内的数字的Boost.Spirit解析器?

考虑一下简单的解析器qi::uint_.它匹配所有无符号整数.是否有可能构建的数字相匹配的解析器012345,但不是12346和更大?

c++ boost boost-spirit

3
推荐指数
1
解决办法
300
查看次数

Boost Spirit Qi - 使用两个组件序列列出解析

我正在尝试为参数列表编写一个解析器,这将允许以下内容:

myFunc( arg0, arg1, namedArg0 = valueA, namedArg1 = valueB )
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我希望前两个参数解析为TypeA的实体,然后由std :: vector <TypeA>包含.后两个参数将解析为TypeB,它将由std :: vector <TypeB>包含.所有TypeA参数都应该在所有TypeB参数之前.但是我想从一个以逗号分隔的列表中解析所有内容.应该可以只有TypeA参数,只有TypeB参数或TypeA元素序列,后跟一系列TypeB元素.

我无法定义规则,以便将最终的TypeA参数与第一个TypeB参数分开的逗号不会被误认为是另一个TypeA参数的期望.

我目前的实施情况如下.任何人都可以就如何解决这个问题提出任何建议吗?

这里的关键区别是TypeA参数应该是单个符号,而TypeB参数应采用以下形式:symbol = symbol.

问题似乎与TypeA参数等同于TypeB参数的第一部分这一事实有关,因此使得TypeA序列的结尾不清楚?

谢谢!

struct Params
{
    std::vector<TypeA> a_elements;
    std::vector<TypeB> b_elements;

    Params(const std::vector<TypeA>& a_vec, const std::vector<TypeB>& b_vec)
    : a_elements( a_vec ), b_elements( b_vec ) {}

    static Params create(const std::vector<TypeA>& a_vec, const std::vector<TypeB>& b_vec)
    {
        return Params( a_vec, b_vec );
    }
};

struct ParamsParser : qi::grammar<Iterator, Params(), Skipper>
{
    qi::rule<Iterator, Params(), Skipper>                           start_rule;
    qi::rule<Iterator, std::vector<TypeA>(), Skipper>               type_a_vec_opt_rule;
    qi::rule<Iterator, std::vector<TypeB>(), Skipper> …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-spirit

3
推荐指数
1
解决办法
96
查看次数

Boost精灵x3解析器不适用于多个属性

Spirit X3解析器函数使用1 attribut可以很好地工作.当我尝试使用多个属性编译文档中的代码时,它不起作用.

#include <boost/spirit/home/x3.hpp>
#include <iostream>
using namespace std;
using namespace boost::spirit;

string a = "3.2 4.5";
auto begin = a.begin();
auto end = a.end();
double d1 = 0.0, d2 = 0.0;
x3::phrase_parse(begin, end ,
                 x3::double_ >> x3::double_,
                 x3::space,
                 d1, d2);  // doesn't work. Accept only 1 attribut
Run Code Online (Sandbox Code Playgroud)

它返回以下错误:

/home/sacha/Dev/vql/vqlcompiler.cpp:20: erreur : no matching function for call to ‘phrase_parse(__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, boost::spirit::x3::sequence<boost::spirit::x3::real_parser<double>, boost::spirit::x3::real_parser<double> >, const space_type&, double&, double&)’
                      x3::double_ >> x3::double_, x3::space, d1, d2); …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-spirit c++14 boost-spirit-x3

3
推荐指数
1
解决办法
180
查看次数