【发布时间】:2016-09-16 18:08:37
【问题描述】:
我有一个解析器用于解析像foo, bar, baz 这样的标识符,还有一个用于解析像foo::bar, foo::bar.baz, foo::bar.baz.baham 这样的嵌套标识符
它们都解析为相同的 ast 结构,如下所示:
struct identifier : x3::position_tagged{
std::vector <std::string> namespaces;
std::vector <std::string> classes;
std::string identifier;
};
identifier 的解析器如下所示:
#define VEC_ATR x3::attr(std::vector<std::string>({})) //ugly hack
auto const identifier_def =
VEC_ATR
>> VEC_ATR
>> id_string;
对于nested_identifier,像这样:
auto const nested_identifier_def =
x3::lexeme[
(+(id_string >> "::") >> +(id_string >> ".") > id_string)
| (+(id_string >> "::") >> VEC_ATR > id_string)
| (VEC_ATR >> +(id_string >> ".") > id_string)
| identifier
];
我知道我为宏感到羞耻。
标识符解析器工作正常,但
nested_identifier 有一个奇怪的行为
如果我尝试解析 foo::bar::baz 之类的东西,则从解析器中掉出的 ast 对象具有所有命名空间,在这种情况下,namespaces 向量中有两次 foo 和 bar。
我有一个这种奇怪行为的小例子
here。
谁能解释一下为什么会发生这种情况,以及如何避免这种情况?
【问题讨论】:
-
是什么让区分类和命名空间有用?例如。在 C++ 中,类是命名空间。
-
你说得对,我希望 ast 能保存尽可能多的信息。这就是为什么我将一个 id 后跟一个点解析到 classes 向量中。也许类不是最好的名字,但我找不到另一个快速
标签: c++ parsing boost-spirit boost-spirit-x3