【问题标题】:Error when compiling a grammar with debug activated在激活调试的情况下编译语法时出错
【发布时间】:2015-02-08 20:43:33
【问题描述】:

我正在尝试调试要在 Visual Studio 项目中使用的 boost::spirit 语法:这是我的代码 sn-p:

#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

// This is pasted and copied from another header file

namespace StateMachine {
namespace Private {

struct LuaParameterData {
  std::wstring name;
  std::wstring type;
  std::wstring unit;
  std::wstring cardinality;
  std::wstring value;
};

} // namespace Private
} // namespace StateMachine

BOOST_FUSION_ADAPT_STRUCT(
  StateMachine::Private::LuaParameterData,
  (std::wstring, name)
  (std::wstring, type)
  (std::wstring, unit)
  (std::wstring, cardinality)
  (std::wstring, value)
)

// From here original file continues
namespace StateMachine {
namespace Private {

namespace qi = boost::spirit::qi;

template<typename Iterator>
struct LuaParameterDataParser : qi::grammar<Iterator, LuaParameterData(), qi::ascii::space_type>
{
  LuaParameterDataParser() : LuaParameterDataParser::base_type(start)
  {
    quotedString %= qi::lexeme['"' >> +(qi::ascii::char_ - '"') >> '"'];

    start %=
      qi::lit("\"parameter\"")
      >> ':'
      >> '{'
      >> qi::lit("\"name\""       ) >> ':' >> quotedString >> ','
      >> qi::lit("\"type\""       ) >> ':' >> quotedString >> ','
      >> qi::lit("\"unit\""       ) >> ':' >> quotedString >> ','
      >> qi::lit("\"cardinality\"") >> ':' >> quotedString >> ','
      >> qi::lit("\"value\""      ) >> ':' >> quotedString
      >> '}'
      ;
  }

  qi::rule<Iterator, std::string(), qi::ascii::space_type> quotedString;
  qi::rule<Iterator, LuaParameterData(), qi::ascii::space_type> start;
};

} // namespace Private
} // namespace StateMachine

BOOST_SPIRIT_DEBUG_RULE(StateMachine::Private::LuaParameterDataParser<std::string::const_iterator>::quotedString);

BOOST_SPIRIT_DEBUG 在项目属性中定义。

当我编译它时,我在使用 BOOST_SPIRIT_DEBUG_RULE 的最后一行出现以下错误:

错误 C3484:语法错误:返回类型前应为“->”

错误 C2061:语法错误:标识符“register_node”

我不知道我是否做对了。我想调试我的语法,但我只看到了调试规则的提示(herehere),所以我尝试调整我的代码。

当我将此语法与phrase_parse 一起使用时,我做错了什么以及必须做什么才能打印调试信息?

【问题讨论】:

    标签: visual-studio debugging boost boost-spirit boost-spirit-qi


    【解决方案1】:

    该字段不是静态的。

    您也没有显示 #define BOOST_SPIRIT_DEBUG 行(也许您在编译器命令行中指定它)。

    典型的方法是使用 BOOST_SPIRIT_DEBUG_NODES() 例如

    注意事项:

    • 删除classic 包括
    • 使用wstring的任何特殊原因?

    Compiling On Coliru

    #define BOOST_SPIRIT_DEBUG
    #include <boost/fusion/include/io.hpp>
    //#include <boost/spirit/include/classic.hpp>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/fusion/include/adapt_struct.hpp>
    
    // This is pasted and copied from another header file
    
    namespace StateMachine {
    namespace Private {
    
        struct LuaParameterData {
            std::wstring name;
            std::wstring type;
            std::wstring unit;
            std::wstring cardinality;
            std::wstring value;
        };
    
    } // namespace Private
    } // namespace StateMachine
    
    BOOST_FUSION_ADAPT_STRUCT(
      StateMachine::Private::LuaParameterData,
      (std::wstring, name)
      (std::wstring, type)
      (std::wstring, unit)
      (std::wstring, cardinality)
      (std::wstring, value)
    )
    
    namespace qi = boost::spirit::qi;
    
    // From here original file continues
    namespace StateMachine {
    namespace Private {
    
        template<typename Iterator>
        struct LuaParameterDataParser : qi::grammar<Iterator, LuaParameterData(), qi::ascii::space_type>
        {
            LuaParameterDataParser() : LuaParameterDataParser::base_type(start)
            {
                quotedString = qi::lexeme['"' >> +(qi::ascii::char_ - '"') >> '"'];
    
                start =
                    qi::lit("\"parameter\"")
                    >> ':'
                    >> '{'
                    >> qi::lit("\"name\""       ) >> ':' >> quotedString >> ','
                    >> qi::lit("\"type\""       ) >> ':' >> quotedString >> ','
                    >> qi::lit("\"unit\""       ) >> ':' >> quotedString >> ','
                    >> qi::lit("\"cardinality\"") >> ':' >> quotedString >> ','
                    >> qi::lit("\"value\""      ) >> ':' >> quotedString
                    >> '}'
                    ;
    
                BOOST_SPIRIT_DEBUG_NODES((start)(quotedString));
            }
    
            qi::rule<Iterator, std::string(), qi::ascii::space_type> quotedString;
            qi::rule<Iterator, LuaParameterData(), qi::ascii::space_type> start;
        };
    
    } // namespace Private
    } // namespace StateMachine
    
    int main() {
        using It = std::string::const_iterator;
    
        std::string const input = R"(
            "parameter" : {
                "name"        : "name"       , 
                "type"        : "type"       , 
                "unit"        : "unit"       , 
                "cardinality" : "cardinality", 
                "value"       : "value"       
            }
        )";
        It f = input.begin(), 
           l = input.end();
    
        StateMachine::Private::LuaParameterDataParser<It> p;
        StateMachine::Private::LuaParameterData data;
        bool ok = qi::phrase_parse(f, l, p, qi::ascii::space, data);
    
        if (ok) {
            std::wcout << L"Parsed: \n";
            std::wcout << L"\tname: " << data.name << L'\n';
            std::wcout << L"\ttype: " << data.type << L'\n';
            std::wcout << L"\tunit: " << data.unit << L'\n';
            std::wcout << L"\tcardinality: " << data.cardinality << L'\n';
            std::wcout << L"\tvalue: " << data.value << L'\n';
        } else {
            std::wcout << L"Parse failure\n";
        }
    
        if (f!=l)
            std::wcout << L"Remaining unparsed: '" << std::wstring(f,l) << L"'\n";
    }
    

    打印

    <start>
      <try>\n        "parameter"</try>
      <quotedString>
        <try> "name"       , \n   </try>
        <success>       , \n          </success>
        <attributes>[[n, a, m, e]]</attributes>
      </quotedString>
      <quotedString>
        <try> "type"       , \n   </try>
        <success>       , \n          </success>
        <attributes>[[t, y, p, e]]</attributes>
      </quotedString>
      <quotedString>
        <try> "unit"       , \n   </try>
        <success>       , \n          </success>
        <attributes>[[u, n, i, t]]</attributes>
      </quotedString>
      <quotedString>
        <try> "cardinality", \n   </try>
        <success>, \n            "valu</success>
        <attributes>[[c, a, r, d, i, n, a, l, i, t, y]]</attributes>
      </quotedString>
      <quotedString>
        <try> "value"       \n    </try>
        <success>       \n        }\n  </success>
        <attributes>[[v, a, l, u, e]]</attributes>
      </quotedString>
      <success>\n    </success>
      <attributes>[[[110, 97, 109, 101], [116, 121, 112, 101], [117, 110, 105, 116], [99, 97, 114, 100, 105, 110, 97, 108, 105, 116, 121], [118, 97, 108, 117, 101]]]</attributes>
    </start>
    Parsed: 
        name: name
        type: type
        unit: unit
        cardinality: cardinality
        value: value
    

    【讨论】:

    • 我在项目选项中设置了BOOST_SPIRIT_DEBUG,以便编译器使用它。我会试试你的方法。感谢您的帮助。
    • 它编译(谢谢)但我没有从失败的解析中获得任何输出,我已经设置了BOOST_SPIRIT_DEBUG 定义,那么我能做什么?也许我的解析器错了?
    • 我在你的代码中看不到任何解释,如图所示。你能发布一个SSCCE吗?因为它是你并没有真正解析任何东西:)
    • @Jepessen 我刚刚意识到您应该删除 classic 标头。显然它阻止了调试功能(我不知道这一点)。更新
    • 我会尽快检查的。对不起,如果我总是迟到,但我在两个不同的地方工作..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多