我正在尝试设计一个使用 Ragel 和 C++ 作为主机语言的解析器。有一种特殊情况,参数可以以两种格式定义:
a. Integer : eg. SignalValue = 24
b. Hexadecimal : eg. SignalValue = 0x18
我有下面的代码来解析这样的参数:
INT = ((digit+)$incr_Count) %get_int >!(int_error); #[0-9]
HEX = (([0].'x'.[0-9A-F]+)$incr_Count) %get_hex >!(hex_error); #[hexadecimal]
SIGNAL_VAL = ( INT | HEX ) %/getSignalValue;
然而,在上面定义的解析器命令中,只有整数值(如 a 节中定义)被正确识别和解析。如果提供了十六进制数字(例如 0x24),则该数字将存储为 '0' 。如果是十六进制数,则不会调用错误。解析器识别十六进制,但存储的值为“0”。
我似乎遗漏了 Ragel 的一些小细节。有没有人遇到过类似的情况?
代码的剩余部分:
//Global
int lInt = -1;
action incr_Count {
iGenrlCount++;
}
action get_int {
int channel = 0xFF;
std::stringstream str;
while(iGenrlCount > 0)
{
str << *(p - iGenrlCount);
iGenrlCount--;
}
str >> lInt; //push the values
str.clear();
}
action get_hex {
std::stringstream str;
while(iGenrlCount > 0)
{
str << std::hex << *(p - iGenrlCount);
iGenrlCount--;
}
str >> lInt; //push the values
}
action getSignalValue {
cout << "lInt = " << lInt << endl;
}