在boost :: spirit语法中翻转规则内的子规则的顺序会导致段错误

时间:2013-09-04 11:00:20

标签: boost segmentation-fault boost-spirit boost-spirit-qi boost-spirit-lex

警告;而我试图将代码缩短到最低限度。我仍然需要提供相当多的信息,以确保所需的信息存在。

此代码,编译文件并运行导致语法错误;

name = simple_name      [ qi::_val = qi::_1 ]
     | qualified_name   [ qi::_val = qi::_1 ]
     ;

虽然这个;

name = qualified_name   [ qi::_val = qi::_1 ]
     | simple_name      [ qi::_val = qi::_1 ]
     ;

SIGSEGV,分段错误的结果;

boost::detail::function::function_obj_invoker4<boost::spirit::qi::detail::parser_binder<boost::spirit::qi::alternative<boost::fusion::cons<boost::spirit::qi::action<boost::spirit::qi::reference<boost::spirit::qi::rule<boost::spirit::lex::lexertl::iterator<boost::spirit::lex::lexertl::functor<boost::spirit::lex::lexertl::token<__gnu_cxx::__normal_iterator<char*, std::string>, boost::mpl::vector<std::string, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, mpl_::bool_<false>, unsigned long>, boost::spirit::lex::lexertl::detail::data, __gnu_cxx::__normal_iterator<char*, std::string>, mpl_::bool_<true>, mpl_::bool_<false> > >, Ast::name* (), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> const>, boost::phoenix::actor<boost::proto::exprns_::expr<boost::proto::tagns_::tag::assign, boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::attribute<0> >,0l>,boost::phoenix::actor<boost::spirit::argument<0> > >, 2l> > >,boost::fusion::cons<boost::spirit::qi::action<boost::spirit::qi::reference<boost::spirit::qi::rule<boost::spirit::lex::lexertl::iterator<boost::spirit::lex::lexertl::functor<boost::spirit::lex::lexertl::token<__gnu_cxx::__normal_iterator<char*, std::string>,boost::mpl::vector<std::string, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>, mpl_::bool_<false>,unsigned long>, boost::spirit::lex::lexertl::detail::data, __gnu_cxx::__normal_iterator<char*,std::string>, mpl_::bool_<true>, mpl_::bool_<false> > >, Ast::name* (), ... more to come ...

其中;

simple_name = (tok.identifier) [ qi::_val = build_simple_name_(qi::_1) ];

qualified_name = (name >> qi::raw_token(DOT) >> tok.identifier) [ qi::_val = build_qualified_name_(qi::_1, qi::_2) ] ;

所有这些规则都会返回Ast::name*();

qi::rule<Iterator, Ast::name*()> name;
qi::rule<Iterator, Ast::name*()> simple_name;
qi::rule<Iterator, Ast::name*()> qualified_name;

辅助函数定义为;

Ast::name* build_simple_name(std::string str)
{
    return (new Ast::name_simple(Ast::identifier(str)));
}
BOOST_PHOENIX_ADAPT_FUNCTION(Ast::name*, build_simple_name_, build_simple_name, 1)

Ast::name* build_qualified_name(Ast::name* name, std::string str)
{
    std::list<Ast::identifier> qualified_name = Ast::name_to_identifier_list(name);
    qualified_name.push_back(Ast::identifier(str));

    return (new Ast::name_qualified(qualified_name));
}
BOOST_PHOENIX_ADAPT_FUNCTION(Ast::name*, build_qualified_name_, build_qualified_name, 2)

使用的词法分析器定义定义为;

lex::token_def<std::string> identifier = "{JAVA_LETTER}{JAVA_LETTER_OR_DIGIT}*";

('.', DOT)

模式{JAVA_LETTER}{JAVA_LETTER_OR_DIGIT}定义为;

("DIGIT",           "[0-9]")
("LATIN1_LETTER",   "[A-Z]|[a-z]")
("JAVA_LETTER",     "{LATIN1_LETTER}|$|_")
("JAVA_LETTER_OR_DIGIT", "{JAVA_LETTER}|{DIGIT}")

我的输入是一个简单的字符串;

package a.D;

令牌的哪个词汇;

Keywords : package
Identifier : a
Delimiters : .
Identifier : D
Delimiters : ;

如果第一个示例(首先使用simple_name),则会将语法错误抛出为;

Syntax Error at line 1:
package a.D;
          ^^

最后一个例子只是抛出一个段错误,之前发布了错误。

显然第二个例子就是我想要的,因为它应该在简单表达之前尝试匹配复杂的表达式。

有谁知道代码崩溃的原因,或者我将如何搞清楚?      - 这还应该在代码审查中吗?

1 个答案:

答案 0 :(得分:2)

问题是你有left recursive grammar并且不能与Boost.Spirit一起使用。 你拥有的基本上是:

name = identifier | name >> dot >> identifier;

正如您所见here,以便在您有类似内容时删除左递归:

A = A >> alpha | beta;

您需要创建2个新“规则”:

A = beta >> A_tail;
A_tail = eps | alpha >> A_tail;

在你的情况下:

A := name
alpha := dot >> identifier
beta := identifier

所以你的“规则”将是:

name = identifier >> name_tail;
name_tail = eps | dot >> identifier >> A_tail;

如果仔细查看name_tail,您可以看到它的字面意思是:无论是什么,或者dot >> identifier后面都没有,或dot >> identifier等等。这意味着name_tail是:

name_tail = *(dot >> identifier);

最后,您的name规则将是:

name = identifier >> *(dot >> identifier);

所有这些都是正确的,但很有可能它不适用于你的属性。