我使用Spirit Qi作为我的解析器,将数学表达式解析为表达式树。我会跟踪诸如解析时遇到的符号类型以及必须在我正在解析的文本中声明的内容。也就是说,我正在解析Bertini input files,一个简单的例子是here,一个复杂的例子是here,为了完整性,如下所示:
%input: our first input file
variable_group x,y;
function f,g;
f = x^2 - 1;
g = y^2 - 4;
END;
我一直在研究的语法理想
variable_group x, y;
f = x^2 - 1;
这部分我大部分都在掌控之中。=
,并将其解析为子函数。我想我也可以解决这个问题。我一直在努力解决的问题似乎是如此微不足道,但经过几个小时的搜索,我仍然没有到达那里。我已经阅读了许多Boost Spirit邮件列表帖子,SO帖子,手册以及Spirit本身的标题,但仍然没有完全了解一些关于Spirit Qi解析的关键事项。
这是有问题的基本语法定义,它将放在system_parser.hpp
:
#define BOOST_SPIRIT_USE_PHOENIX_V3 1
#include <boost/spirit/include/qi_core.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <string>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
template<typename Iterator>
struct SystemParser : qi::grammar<Iterator, std::vector<std::string>(), boost::spirit::ascii::space_type>
{
SystemParser() : SystemParser::base_type(variable_group_)
{
namespace phx = boost::phoenix;
using qi::_1;
using qi::_val;
using qi::eps;
using qi::lit;
qi::symbols<char,int> encountered_variables;
qi::symbols<char,int> declarative_symbols;
declarative_symbols.add("variable_group",0);
// wraps the vector between its appropriate declaration and line termination.
BOOST_SPIRIT_DEBUG_NODE(variable_group_);
debug(variable_group_);
variable_group_.name("variable_group_");
variable_group_ %= lit("variable_group") >> genericvargp_ >> lit(';');
// creates a vector of strings
BOOST_SPIRIT_DEBUG_NODE(genericvargp_);
debug(genericvargp_);
genericvargp_.name("genericvargp_");
genericvargp_ %= new_variable_ % ',';
// will in the future make a shared pointer to an object using the string
BOOST_SPIRIT_DEBUG_NODE(new_variable_);
debug(new_variable_);
new_variable_.name("new_variable_");
new_variable_ %= unencountered_symbol_;
// this rule gets a string.
BOOST_SPIRIT_DEBUG_NODE(unencountered_symbol_);
debug(unencountered_symbol_);
unencountered_symbol_.name("unencountered_symbol");
unencountered_symbol_ %= valid_variable_name_ - ( encountered_variables | declarative_symbols);
// get a string which fits the naming rules.
BOOST_SPIRIT_DEBUG_NODE(valid_variable_name_);
valid_variable_name_.name("valid_variable_name_");
valid_variable_name_ %= +qi::alpha >> *(qi::alnum | qi::char_('_') | qi::char_('[') | qi::char_(']') );
}
// rule declarations. these are member variables for the parser.
qi::rule<Iterator, std::vector<std::string>(), ascii::space_type > variable_group_;
qi::rule<Iterator, std::vector<std::string>(), ascii::space_type > genericvargp_;
qi::rule<Iterator, std::string(), ascii::space_type> new_variable_;
qi::rule<Iterator, std::string(), ascii::space_type > unencountered_symbol_;// , ascii::space_type
// the rule which determines valid variable names
qi::rule<Iterator, std::string()> valid_variable_name_;
};
以及使用它的一些代码:
#include "system_parsing.hpp"
int main(int argc, char** argv)
{
std::vector<std::string> V;
std::string str = "variable_group x, y, z;";
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
SystemParser<std::string::const_iterator> S;
bool s = phrase_parse(iter, end, S, boost::spirit::ascii::space, V);
std::cout << "the unparsed string:\n" << std::string(iter,end);
return 0;
}
它在OSX上的Clang 4.9.x下编译就好了。当我运行它时,我得到:
Assertion failed: (px != 0), function operator->, file /usr/local/include/boost/smart_ptr/shared_ptr.hpp, line 648.
或者,如果我在>
规则的定义中使用期望运算符>>
而不是variable_group_
,我会得到亲爱的老朋友Segmentation fault: 11
。
在我的学习过程中,我遇到过如how to tell the type spirit is trying to generate,attribute propagation,how to interact with symbols,an example of infinite left recursion这样导致段错误的优秀帖子,{{3} }它有一个使用自定义点的链接(但链接不包含任何示例),information on parsing into classes, not structs将关键字与行为结合起来,也许与我正在尝试做的事情最相关the Nabialek trick这当然是我的事情因为符号集的增长需要,我以后不再使用它们作为另一种类型,因为已经遇到的符号集开始为空,并且增长 - 解析的规则是动态的。
所以这就是我所处的位置。我当前的问题是这个特定示例生成的断言/段错误。但是,我不清楚某些事情,并且需要指导性的建议,我只是没有从我咨询的任何来源中加入,并且希望这个SO问题的请求与先前提出的其他人不相符:
lexeme
?我只是不知道何时使用lexeme,而不是。>
而不是>>
时,有哪些指导原则?对这位初学者的任何建议都是最受欢迎的。
答案 0 :(得分:0)
您引用了symbols
个变量。但它们是本地人,因此一旦构造函数返回它们就不存在。 会调用Undefined Behaviour 。任何事情都可能发生。
使symmbol表成为该类的成员。
还简化了舞蹈
lexeme[]
。例如,在您的示例中,您lexeme[]
周围缺少encountered_variables|declarative_symbols
。operator%=
,以及一些通常未使用过的东西symbols<>
的映射类型(因为没有消耗int
),简化了那里的初始化<强> Live On Coliru 强>
#define BOOST_SPIRIT_USE_PHOENIX_V3 1
#define BOOST_SPIRIT_DEBUG 1
#include <boost/spirit/include/qi_core.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <string>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
template <typename Iterator, typename Skipper = ascii::space_type>
struct SystemParser : qi::grammar<Iterator, std::vector<std::string>(), Skipper> {
SystemParser() : SystemParser::base_type(variable_group_)
{
declarative_symbols += "variable_group";
variable_group_ = "variable_group" >> genericvargp_ >> ';';
genericvargp_ = new_variable_ % ',';
valid_variable_name_ = qi::alpha >> *(qi::alnum | qi::char_("_[]"));
unencountered_symbol_ = valid_variable_name_ - (encountered_variables|declarative_symbols);
new_variable_ = unencountered_symbol_;
BOOST_SPIRIT_DEBUG_NODES((variable_group_) (valid_variable_name_) (unencountered_symbol_) (new_variable_) (genericvargp_))
}
private:
qi::symbols<char, qi::unused_type> encountered_variables, declarative_symbols;
// rule declarations. these are member variables for the parser.
qi::rule<Iterator, std::vector<std::string>(), Skipper> variable_group_;
qi::rule<Iterator, std::vector<std::string>(), Skipper> genericvargp_;
qi::rule<Iterator, std::string()> new_variable_;
qi::rule<Iterator, std::string()> unencountered_symbol_; // , Skipper
// the rule which determines valid variable names
qi::rule<Iterator, std::string()> valid_variable_name_;
};
//#include "system_parsing.hpp"
int main() {
using It = std::string::const_iterator;
std::string const str = "variable_group x, y, z;";
SystemParser<It> S;
It iter = str.begin(), end = str.end();
std::vector<std::string> V;
bool s = phrase_parse(iter, end, S, boost::spirit::ascii::space, V);
if (s)
{
std::cout << "Parse succeeded: " << V.size() << "\n";
for (auto& s : V)
std::cout << " - '" << s << "'\n";
}
else
std::cout << "Parse failed\n";
if (iter!=end)
std::cout << "Remaining unparsed: '" << std::string(iter, end) << "'\n";
}
打印
Parse succeeded: 3
- 'x'
- 'y'
- 'z'