boost :: spirit如何用于解析嵌套的for循环?

时间:2013-03-06 12:24:24

标签: c++ boost boost-spirit

我正在尝试解析具有此类语法的循环:

for(loop = 1:10) {


}

在我的语法中,我有规则:

genericString %= lexeme[+(char_("a-zA-Z"))];
intRule %= int_;
commandString %= lexeme[+(char_ - '}')];
forLoop %= string("for")
        >> '('
        >> genericString // variable e.g. c
        >> '='
        >> (intRule | genericString) // variable e.g. i
        >> ':'
        >> (intRule | genericString) // variable e.g. j
        >> ')' >> '{'
        >> (forLoop | commandString)
        >> '}';

虽然这适用于上面的简单示例,但它无法解析以下嵌套示例:

for(loop = 1:10) {
    for(inner = 1:10) {

    }
}

我猜这是因为解析器“与支架放置混淆”。我想我需要做一些像http://boost-spirit.com/distrib/spirit_1_7_0/libs/spirit/example/fundamental/lazy_parser.cpp那样的事情(唉,我发现很难遵循)。

干杯,

本。

编辑1:

我现在认为处理来自commandString(下面称为nestedBlock)而不是forLoop的递归会更好,例如:

forLoop %= string("for")
        >> '('
        >> genericString // variable e.g. c
        >> '='
        >> (intRule | genericString) // variable e.g. i
        >> ':'
        >> (intRule | genericString) // variable e.g. j
        >> ')' 
        >> nestedBlock;

nestedBlock %= lexeme['{' >> -(char_ - '}' - '{')
                          >> -nestedBlock
                          >> -(char_ - '}' - '{')
                          >> '}'];

失败了大量的boost :: spriti错误。规则定义为:

    qi::rule<Iterator, std::string(), ascii::space_type> nestedBlock;
    qi::rule<Iterator, Function(), ascii::space_type> forLoop;

Function是boost :: variants

的结构

编辑2:

所以这就是我现在拥有的(设计用于或不用嵌套结构):

commandCollection %= *start;

forLoop %= string("for")
        >> '('
        >> genericString // variable e.g. c
        >> '='
        >> (intRule | genericString) // variable e.g. i
        >> ':'
        >> (intRule | genericString) // variable e.g. j
        >> ')'
        >> '{'
        >>       commandCollection
        >> '}';

start %= loadParams  | restoreGenomeData | openGenomeData | initNeat | initEvo |
                 initAllPositions | initAllAgents | initCoreSimulationPointers |
                 resetSimulationKernel | writeStats | restoreSimState |
                 run | simulate | removeObjects | setGeneration |
                 setParam | getParam | pause | create | reset |
                 loadAgents | getAgent | setAgent | listParams | loadScript | forLoop
                 | wait | commentFunc | var | add | sub | mult | div | query;

我声明commandCollection规则如下:

qi::rule<Iterator, boost::fusion::vector<Function>, ascii::space_type> commandCollection;

我认为这会像我期望的那样。 commandCollection被定义为0或更多命令,它们应存储在boost :: fusion :: vector中。但是,当我从Function()结构中提取向量时(考虑到启动规则使用Function()迭代器),由于某种原因,类型不被识别为boost :: fusion :: vector,因此不能提取。我不确定为什么......

然而,如果我只是

commandCollection %= start;

并将规则视为

qi::rule<Iterator, Function(), ascii::space_type> commandCollection;

然后尝试将数据提取为单个Function()结构,它工作正常。但我希望它能在某种容器中存储多个命令(即* start)。我也尝试使用std :: vector,但这也失败了。

1 个答案:

答案 0 :(得分:3)

你的命令字符串不喜欢内循环上的空体。

通过在此更改+*来修复此问题:

commandString %= lexeme[*(char_ - '}')];

或者,如果您希望匹配可选块,而不是可能为空的块,请考虑@llonesmiz提到的修复。

测试用例:

#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
// #include <boost/spirit/include/phoenix.hpp>

namespace qi    = boost::spirit::qi;
namespace karma = boost::spirit::karma;
namespace phx   = boost::phoenix;

typedef boost::variant<int, std::string> Value;
typedef std::pair<Value, Value> Range;
typedef std::pair<std::string, Range> Iteration;

typedef Iteration attr_t;

template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, attr_t(), Skipper>
{
    parser() : parser::base_type(start)
    {
        using namespace qi;

        genericString %= lexeme[+(char_("a-zA-Z"))];// variable e.g. c
        intRule %= int_;
        commandString %= lexeme[*(char_ - '}')];
        value = intRule | genericString;
        range = value >> ':' >> value;
        forLoop %= lit("for")
                >> '(' >> genericString >> '=' >> range >> ')' 
                >> '{'
                >>      (forLoop | commandString)
                >> '}';

        start = forLoop;

        BOOST_SPIRIT_DEBUG_NODES(
                (start)(intRule)(genericString)(commandString)(forLoop)(value)(range)
                 );
    }

  private:
    qi::rule<It, std::string(), Skipper> genericString, commandString;
    qi::rule<It, int(), Skipper> intRule;
    qi::rule<It, Value(), Skipper> value;
    qi::rule<It, Range(), Skipper> range;
    qi::rule<It, attr_t(), Skipper> forLoop, start;
};

bool doParse(const std::string& input)
{
    typedef std::string::const_iterator It;
    auto f(begin(input)), l(end(input));

    parser<It, qi::space_type> p;
    attr_t data;

    try
    {
        bool ok = qi::phrase_parse(f,l,p,qi::space,data);
        if (ok)   
        {
            std::cout << "parse success\n";
        }
        else      std::cerr << "parse failed: '" << std::string(f,l) << "'\n";

        if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
        return ok;
    } catch(const qi::expectation_failure<It>& e)
    {
        std::string frag(e.first, e.last);
        std::cerr << e.what() << "'" << frag << "'\n";
    }

    return false;
}

int main()
{
    bool ok = doParse(
            "for(loop = 1:10) {\n"
            "   for(inner = 1:10) {\n"
            "   }\n"
            "}"
            );
    return ok? 0 : 255;
}

我衷心建议查看显示解析失败的DEBUG输出:

<forLoop>
  <try>\n   }\n}</try>
  <fail/>
</forLoop>
<commandString>
  <try>\n   }\n}</try>
  <fail/>
</commandString>
<fail/>