2

我正在尝试设置一个解析器,给定一个值,可以将其分配给向量的某个元素,但我不完全确定如何实现它。

假设以下代码解析字符串(0){**+*+}。它应该每增加bar.a[0]一次,每+增加bar.b[0]一次*。我遇到的问题是我不确定如何使用以下方法获取对向量元素的引用_a

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <vector>

//The struct containing the vector.
struct testStruct {
    std::vector<int> a, b;
};

BOOST_FUSION_ADAPT_STRUCT (
    testStruct,
    (std::vector<int>, a)
    (std::vector<int>, b)
)

namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
namespace ascii = boost::spirit::ascii;

template<typename Iterator>
struct foo : qi::grammar<Iterator, testStruct(), qi::locals<unsigned>, ascii::space_type> {
    foo() : foo::base_type(start) {
        using namespace qi::labels;
        using qi::eps;
        using qi::lit;
        using qi::uint_;
        using phoenix::at_c;

        start = lit('(')
                >> uint_                [_a = _1]
                >> ')'
                >> '{'
                >>  starsOrPlus(
                        at_c<_a>(at_c<0>(_val)),    //This is where I'm not sure what to do.
                        at_c<_a>(at_c<1>(_val))
                    )
                >> '}'
                ;

        starsOrPlus = eps               [_r1 = 0]
                                        [_r2 = 0]
            >> (
                * (
                    (
                        +lit('+')       [_r1 += 1]
                    )
                    ^ (
                        +lit('*')       [_r2 += 1]
                    )
                )
            )
        ;
    }

    qi::rule<Iterator, testStruct(), qi::locals<unsigned>, ascii::space_type> start;

    //Parses stars and pluses. Sets the first uint8_t to the number of *, and the
    //second to the number of +.
    qi::rule<Iterator, void(int&, int&), ascii::space_type> starsOrPlus;
};

//Main program
int main() {
    std::string testString = "(2){**++*+}";

    typedef foo<std::string::const_iterator> foo;
    foo grammar;

    testStruct bar;
    std::string::const_iterator iter = testString.begin();
    std::string::const_iterator end = testString.end();
    bool parsed = phrase_parse(iter, end, grammar, ascii::space, bar);

    if (parsed) {
        //Do something with the data...
    }

    return 0;
}

这无法编译并出现以下错误:

main.cpp||In constructor 'foo<Iterator>::foo()':|
main.cpp|36|error: 'boost::spirit::_a' cannot appear in a constant-expression|
main.cpp|36|error: no matching function for call to 'at_c(boost::phoenix::actor<boost::phoenix::composite<boost::phoenix::at_eval<0>, boost::fusion::vector<boost::spirit::attribute<0>, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_> > >&)'|
main.cpp|37|error: 'boost::spirit::_a' cannot appear in a constant-expression|
main.cpp|37|error: no matching function for call to 'at_c(boost::phoenix::actor<boost::phoenix::composite<boost::phoenix::at_eval<1>, boost::fusion::vector<boost::spirit::attribute<0>, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_> > >&)'|

所以,很明显,我不能在at_c. 我也知道,即使可以,如果给定位置超出范围,也会出现重新调整矢量大小的问题。

我将如何实现这样的事情?我只是以完全错误的方式去做这件事吗?

4

1 回答 1

2

这应该让你开始:

#include <vector>
#include <string>
#include <iostream>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>

struct testStruct
{
    std::vector<int> a, b;
};

BOOST_FUSION_ADAPT_STRUCT
(
    testStruct,
    (std::vector<int>, a)
    (std::vector<int>, b)
)

namespace bp = boost::phoenix;
namespace bs = boost::spirit;
namespace bsq = bs::qi;

template<typename Iterator>
struct foo : bsq::grammar<Iterator, testStruct(), bsq::locals<unsigned> >
{
    foo() : foo::base_type(start)
    {
        using namespace bs::labels;
        using bp::at_c;
        using bp::resize;
        using bs::lit;
        using bs::uint_;

        start
            =   '('
                >> uint_
                   [
                       _a = _1,
                       resize(at_c<0>(_val), _1 + 1),
                       resize(at_c<1>(_val), _1 + 1)
                   ]
                >> "){"
                >> starsOrPlus(at_c<0>(_val)[_a], at_c<1>(_val)[_a])
                >> '}'
            ;

        starsOrPlus
            =  *(
                    lit('+')[_r1 += 1]
                |   lit('*')[_r2 += 1]
                )
            ;
    }

    bsq::rule<Iterator, testStruct(), bsq::locals<unsigned> > start;
    bsq::rule<Iterator, void(int&, int&)> starsOrPlus;
};

void printvec(std::vector<int> const& vec)
{
    bool first = true;
    for (std::vector<int>::const_iterator it = vec.begin(), it_end = vec.end();
         it != it_end;
         ++it)
    {
        if (first)
            first = false;
        else
            std::cout << ", ";
        std::cout << *it;
    }
}

int main()
{
    foo<std::string::const_iterator> grammar;
    testStruct bar;
    std::string const input = "(2){**++*}";
    std::string::const_iterator first = input.begin(), last = input.end();
    if (bsq::parse(first, last, grammar, bar) && first == last)
    {
        std::cout << "bar.a: ";
        printvec(bar.a);
        std::cout << "\nbar.b: ";
        printvec(bar.b);
        std::cout << '\n';
    }
    else
        std::cout << "parse failed\n";
}

这里的显着变化是:

  • 必须将vectors 内部的testStruct大小调整为足以使所需索引有效的大小
  • operator[]用于boost::phoenix::at_c访问 a 的索引vector,就像在“普通”代码中一样

请注意,我取出了跳过解析器来简化事情(并且因为它似乎没有必要);如果需要,可以将其添加回来——它在这里没有真正的相关性。

于 2011-07-23T03:43:07.360 回答