首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在精神X3中添加条件期望点

如何在精神X3中添加条件期望点
EN

Stack Overflow用户
提问于 2016-08-20 11:51:51
回答 1查看 282关注 0票数 1

我现在在X3中将期望点添加到语法中。现在我违反了一条规则,看起来是这样的。

代码语言:javascript
复制
auto const id_string = +x3::char("A-Za-z0-9_);

auto const nested_identifier_def =
        x3::lexeme[
                *(id_string >> "::")
                >> *(id_string >> ".")
                >> id_string
        ];

我想知道如何在这个规则中添加条件期望点。比如“如果有一个"::”然后跟着一个id_string“,或者”当有一个.时,就会跟着一个id_string“等等。对于这样的规则,我怎样才能做到这样的行为呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-20 14:33:49

我会按照你的意愿写出来:

代码语言:javascript
复制
auto const identifier 
    = lexeme [+char_("A-Za-z0-9_")];

auto const qualified_id 
    = identifier >> *("::" > identifier);

auto const simple_expression // only member expressions supported now
    = qualified_id >> *('.' > identifier);

与之对应的AST:

代码语言:javascript
复制
namespace AST {
    using identifier = std::string;

    struct qualified_id : std::vector<identifier> { using std::vector<identifier>::vector; };

    struct simple_expression {
        qualified_id lhs;
        std::vector<identifier> rhs;
    };
}

现场演示

住在Coliru

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <vector>

namespace AST {
    using identifier = std::string;

    struct qualified_id : std::vector<identifier> { using std::vector<identifier>::vector; };

    struct simple_expression {
        qualified_id lhs;
        std::vector<identifier> rhs;
    };
}

#include <boost/fusion/adapted.hpp>
BOOST_FUSION_ADAPT_STRUCT(AST::simple_expression, lhs, rhs)

#include <boost/spirit/home/x3.hpp>

namespace Parser {
    using namespace boost::spirit::x3;

    auto const identifier 
        = rule<struct identifier_, AST::identifier> {}
        = lexeme [+char_("A-Za-z0-9_")];

    auto const qualified_id 
        = rule<struct qualified_id_, AST::qualified_id> {}
        = identifier >> *("::" > identifier);

    auto const simple_expression // only member expressions supported now
        = rule<struct simple_expression_, AST::simple_expression> {}
        = qualified_id >> *('.' > identifier);
}

int main() {
    using It = std::string::const_iterator;

    for (std::string const input : { "foo", "foo::bar", "foo.member", "foo::bar.member.subobject" }) {
        It f = input.begin(), l = input.end();
        AST::simple_expression data;

        bool ok = phrase_parse(f, l, Parser::simple_expression, boost::spirit::x3::space, data);

        if (ok) {
            std::cout << "Parse success: ";
            for (auto& el : data.lhs) std::cout << "::" << el;
            for (auto& el : data.rhs) std::cout << "." << el;
            std::cout << "\n";
        }
        else {
            std::cout << "Parse failure ('" << input << "')\n";
        }

        if (f != l)
            std::cout << "Remaining unparsed input: '" << std::string(f, l) << "'\n";
    }
}

打印

代码语言:javascript
复制
Parse success: ::foo
Parse success: ::foo::bar
Parse success: ::foo.member
Parse success: ::foo::bar.member.subobject
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39053814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档