2

a) So far this is my revised full code. It is not running fully as i am having errors such as

"error: no type named 'type' in 'struct boost::spirit::traits::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;
}

Please find my full code above, perhaps this could make my question clearer. My code is still not running. One example of the data i will have to pass is as shown below:

VARIABLE phys_soft_desc
{
LABEL       [phys_soft_desc_label];
HELP        [phys_soft_desc_help];
CLASS       CONTAINED;
TYPE        ASCII (32)
{
    DEFAULT_VALUE "";
}
HANDLING    READ;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kofi
  • 43
  • 7
  • If you want to use Boost.Spirit, you will absolutely need to familiarize yourself with EBNF: https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form. After doing that, the Spirit EDSL will make a lot more sense/ – KitsuneYMG Sep 22 '16 at 14:15
  • thanks Kitsune, i have gone through your link...but could you suggest also how the grammer definition for the question? – Kofi Sep 22 '16 at 16:59
  • Not going to spend a lot of time (perhaps if you make the sample selfcontained - see http://sscce.org) but here's a hint: http://paste.ubuntu.com/23219362/ – sehe Sep 23 '16 at 09:54
  • thanks @sehe, after aplying your grammer, i had some errors am trying to fix. I will reframe my question again as you suggested and post it here again. – Kofi Sep 23 '16 at 12:28
  • (Note: be sure to pass `qi::blank`, not `qi::space` into the parse invocation) – sehe Sep 23 '16 at 12:41
  • sorry, i just saw your last comment, i have changed the qi::space to qi::blank in the parse invocation, but still the errors exist. @sehe – Kofi Sep 23 '16 at 12:59
  • @sehe please find the code as i have posted the code am using currently. – Kofi Sep 23 '16 at 13:01
  • How (and why) did you manage to break all the earlier fixes to the question formatting... – sehe Sep 23 '16 at 13:01
  • sorry...i didnt get you...do you mean the formatting of the code i just posted? If you mean how it is displayed...i am surprised myself. Would you like that i format it again? @sehe – Kofi Sep 23 '16 at 13:06
  • Look at the edit history http://stackoverflow.com/revisions/39640893/2 – sehe Sep 23 '16 at 13:07
  • i think is my mistake...instead of adding the code to the already existing question...i replaced the older question with this current question...partly because it contains the full source code. Is the current code enough and clear for you? – Kofi Sep 23 '16 at 13:11

0 Answers0