/ / व्याकरण अप्रत्याशित रूप से बढ़ावा देता है :: भावना व्याकरण परिभाषा - सी ++, बूस्ट, व्याकरण, बूस्ट-भावना

व्याकरण अप्रत्याशित रूप से बढ़ावा देता है :: भावना व्याकरण परिभाषा - सी ++, बूस्ट, व्याकरण, बूस्ट-भावना

मैं बढ़ावा देने के लिए पूरी तरह से नया हूं: भावना और अनुमान लगाएं कि यह प्रश्न एक उन्नत उपयोगकर्ताओं के लिए छोटा है। निम्नलिखित व्याकरण जो मैं अपेक्षा करता हूं वह नहीं करता है:

    #pragma once

#include <string>
#include <vector>

#define BOOST_SPIRIT_UNICODE
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/qi_alternative.hpp>


namespace overmath
{
using namespace std;
namespace qi = boost::spirit::qi;
using namespace boost::spirit::unicode;

struct identifier
{
wstring name;
};

struct function
{
identifier name;
};

struct program
{
vector<function> functions;
};
}

BOOST_FUSION_ADAPT_STRUCT(
overmath::identifier,
(std::wstring, name)
)

BOOST_FUSION_ADAPT_STRUCT(
overmath::function,
(overmath::identifier, name)
)

BOOST_FUSION_ADAPT_STRUCT(
overmath::program,
(std::vector<overmath::function>, functions)
)


namespace overmath
{
using namespace boost::spirit::unicode;
namespace qi = boost::spirit::qi;
using boost::spirit::lit;

template<typename Iterator> struct function_parser : qi::grammar<Iterator, program(), space_type>
{
function_parser() : function_parser::base_type(program)
{
identifier %=
qi::eps
>> +alnum;

function %=
lit("def ")
>> identifier
>> "("
>> ")"
>> lit(" enddef");

program %=
qi::eps
>> +function;
}

qi::rule<Iterator, identifier(), space_type> identifier;
qi::rule<Iterator, function(), space_type> function;
qi::rule<Iterator, program(), space_type> program;
};

template<typename Iterator> wstring parse(Iterator first, Iterator last)
{
using boost::spirit::qi::phrase_parse;

program f;
function_parser<Iterator> fp;

auto b = phrase_parse(first, last, fp, space, f);
if(b)
{
return wstring(L"OK");
}
return wstring(L"FAIL");
}

}

जब मैं इसे "def abc () enddef" स्ट्रिंग के साथ परीक्षण करता हूं तो पार्सिंग विफल हो जाती है। मुझे कोई जानकारी नहीं है की क्यों। मैंने गलत तरीके से क्या किया? आपका अग्रिम रूप से बोहोत धन्यवाद।

उत्तर:

जवाब के लिए 3 № 1

आपका कप्तान रिक्त स्थान खाता है, इसलिए "def " तथा " enddef" कभी मेल नहीं खाएगा

यह भी देखें भावना कप्तान मुद्दों को बढ़ावा देना

Coliru पर लाइव

#include <string>
#include <vector>

#define BOOST_SPIRIT_UNICODE
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/qi_alternative.hpp>

namespace overmath {
using namespace std;
namespace qi = boost::spirit::qi;
using namespace boost::spirit::unicode;

struct identifier { wstring name;               } ;
struct function   { identifier name;            } ;
struct program    { vector<function> functions; } ;
}

BOOST_FUSION_ADAPT_STRUCT(overmath::identifier, (std::wstring,                    name))
BOOST_FUSION_ADAPT_STRUCT(overmath::function,   (overmath::identifier,            name))
BOOST_FUSION_ADAPT_STRUCT(overmath::program,    (std::vector<overmath::function>, functions))

namespace overmath {
using namespace boost::spirit::unicode;
namespace qi = boost::spirit::qi;
using boost::spirit::lit;

template <typename Iterator> struct function_parser : qi::grammar<Iterator, program(), space_type> {
function_parser() : function_parser::base_type(program) {

identifier = qi::eps >> +alnum;
function   = "def" >> identifier >> "(" >> ")" >> "enddef";
program    = qi::eps >> +function;
}

qi::rule<Iterator, identifier()> identifier;
qi::rule<Iterator, function(),   space_type> function;
qi::rule<Iterator, program(),    space_type> program;
};

template <typename Iterator> wstring parse(Iterator first, Iterator last) {
using boost::spirit::qi::phrase_parse;

program f;
function_parser<Iterator> fp;

auto b = phrase_parse(first, last, fp, space, f);
if (b) {
return wstring(L"OK");
}
return wstring(L"FAIL");
}
}

int main() {
std::wstring const s = L"def abc() enddef";
std::wcout << overmath::parse(s.begin(), s.end());
}