【发布时间】:2016-09-06 01:26:35
【问题描述】:
我试图了解将语义动作“附加”到解析器的确切含义,更准确地说,我想了解语义动作何时以及多长时间绑定到解析器。
为此,我对boost精神库的employee.cpp示例进行了如下修改:
1°/ 添加了一个print() 函数,其输出仅在调用时进行跟踪:
void print(const struct employee & e) { std::cout << e.surname << "\n"}
2°/在employee_parser类的构造函数末尾,我将print()函数绑定到start解析器:
employee_parser() : employee_parser::base_type(start)
{
using qi::int_;
using qi::lit;
using qi::double_;
using qi::lexeme;
using ascii::char_;
quoted_string %= lexeme['"' >> +(char_ - '"') >> '"'];
start %=
lit("employee")
>> '{'
>> int_ >> ','
>> quoted_string >> ','
>> quoted_string >> ','
>> double_
>> '}'
;
start[&print];
}
虽然我已经将start 解析器与语义操作print 联系起来,但如文档中所示,print() 函数从未被调用。它将语义动作需要附加到解析器定义的右端,与解析器出现在同一个定义中的次数一样多。有人可以详细说明一下吗?
【问题讨论】:
标签: c++ parsing boost boost-spirit boost-spirit-qi