boost :: spirit :: qi语法,用于解析结构化文本文件

时间:2016-09-22 13:48:09

标签: c++ boost qi

a)到目前为止,这是我修订的完整代码。它没有完全运行,因为我有

等错误
  

“错误:'struct中没有名为'type'的类型   升压::精神::性状:: container_value“

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
#include <string>
#include <complex>
#include <fstream>
#include <vector>

namespace OpcUaStackCore
{
namespace spirit = boost::spirit;
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

struct eddlVariable
{
    std::string identifier;
    std::string label;
    std::string help;
    std::string classtype;
};

//std::vector<eddlVariable> variableContainer;
template <typename Iterator>
    struct eddlVariableParser : qi::grammar<Iterator, eddlVariable(), ascii::blank_type>
    {
        eddlVariableParser() : eddlVariableParser::base_type(start)
        {
            using boost::spirit::eol;

            text  = +qi::graph;
            start = "VARIABLE" >> text >> qi::eol
                 >> '{'     >> qi::eol
                 >> "LABEL" >> text >> qi::eol
                 >> "HELP"  >> text >> qi::eol
                 >> "CLASS" >> text >> qi::eol
                 >> "TYPE"  >> text >> qi::eol
                >> '}';
        }
        private:
       qi::rule<Iterator, eddlVariable(), ascii::blank_type> start;
        // lexemes
       qi::rule<Iterator, eddlVariable()> text;
};
}

BOOST_FUSION_ADAPT_STRUCT(
OpcUaStackCore::eddlVariable,
(std::string, identifier)
(std::string, label)
(std::string, help)
(std::string, classtype)
)

int main(int argc, char **argv)
{
using namespace OpcUaStackCore;
using boost::property_tree::ptree;
using boost::property_tree::write_xml;
using boost::property_tree::xml_writer_settings;

typedef std::string::const_iterator iterator_type;
typedef eddlVariableParser<iterator_type> eddl_parser;
eddl_parser parser;
eddlVariable storedEDDLData;
char const* filename;
if (argc > 1)
    filename = argv[1];
std::ifstream filestream(filename, std::ios_base::in);

std::string storedString;
filestream.unsetf(std::ios::skipws);
std::copy(std::istream_iterator<char>(filestream), std::istream_iterator<char>(), std::back_inserter(storedString));
std::cout << "Stored string is: " << storedString << std::endl;

std::string::const_iterator begin_iter = storedString.begin();
std::string::const_iterator end_iter = storedString.end();
/* Invoke the parser */
bool r = phrase_parse(begin_iter, end_iter, parser, ascii::space, storedEDDLData);

if (r && begin_iter == end_iter)
{
    std::cout << "Parsing succeeded\n";
} else {
    std::cout << "Parsing failed\n";
}

return 0;
}

请在上面找到我的完整代码,也许这可以让我的问题更加清晰。我的代码仍未运行。我必须通过的数据的一个例子如下所示:

VARIABLE phys_soft_desc
{
LABEL       [phys_soft_desc_label];
HELP        [phys_soft_desc_help];
CLASS       CONTAINED;
TYPE        ASCII (32)
{
    DEFAULT_VALUE "";
}
HANDLING    READ;
}

0 个答案:

没有答案