我使用 Ragel 和 C++ 作为主机来解析一些命令。命令从文件中读取,然后使用以下语法进行解析。
该命令的语法如下:
Signal_representation {
[<signal_encoding_type_name>: <signal_name> ([, <signal_name>]) ;]
}
现在在上面的语法中 :
,信号名称之后或之后可以有一个 NEWLINE,后跟逗号,
example:
#1 Signal_representation{
#2 Activity:
#3 Button_Active,
#4 Buttons_Inactive;
#5 Switch:
#6 Horn,
#7 Up_Arrow,
#8 Right_Arrow,
#9 Down_Arrow,
#10 Audio,
#11 Day_Night, Sleep, SWM_Off;
#12 }
这是我在解析上述命令时遵循的 Ragel 语法。
action string_error {
cout << " ERROR::Expected string characters at line = "<< g_ReadLineNbr << endl;
}
action incr_Count {
//increment count to trace back and retrieve the string encountered
iGenrlCount++;
}
action getString {
std::stringstream str;
while(iGenrlCount > 0)
{
str << *(p - iGenrlCount);
iGenrlCount--;
}
str >> GeneralStr; //push the values
}
action getSglEncTyp {
cout << "Enc type = " << GeneralStr<< endl;
GeneralStr.clear();
}
action getSgnlName {
cout << "Signal name = " << GeneralStr<< endl;
GeneralStr.clear();
}
action getSgnlRepr {
cout << "parse ok" << endl;
}
action parse_error {
cout << "parse failed" << endl;
}
// my definition of Ragel grammar
OPEN_BRACES = '{';
BARE_STRING = ([a-zA-Z0-9_\.\-]+) $incr_Count %getString >!(string_error);
CLOSE_BRACES = '}';
//parsing starts from the parameter <signal_encoding_type_name>
signal_repr = (space* BARE_STRING%getSglEncTyp space* ':' space* BARE_STRING%getSgnlName (space* ',' space* BARE_STRING%getSgnlName)* space* ';' space*)%/getSgnlRepr $!parse_error;
main := signal_repr | space* ;
//global variables in C++ prgram visible across all actions
string GeneralStr;
int iGenrlCount = 0;
我面临的问题是文件中遇到的新行。对于上面给出的示例,我收到以下错误
ERROR::Expected string characters at line = 2
根据 Ragel 6.10 文档,FSMspace
必须检测以下内容
Whitespace. [\t\v\f\n\r ]
我还尝试用以下 FSM 替换空间:
_CR = ('\r' | '\n' | '\r\n' );
但即使是上述一个也不起作用。
有没有人遇到过类似的情况?我在 Stackoverflow 上看到了一些关于 Ragel 和换行的问题,但它似乎并没有特别解决这里的问题。