flex bison C lex yacc 扫描字符串缓冲区
有一些 C 代码,类似于:
int doCommand(char* command)
{
// +2 on strlen is for the two extra '\0' characters
// needed by flex when scanning strings.
YY_BUFFER_STATE yybs = yy_scan_buffer(command, strlen(command)+2);
yy_switch_to_buffer(yybs);
yyparse();
yy_delete_buffer(yybs);
}
它在循环中被调用,类似于 (psuedocode):
read characters upto and including '\n' into a buffer;
add two '\0' characters;
call doCommand(buffer);
zero the buffer; // NOTE: same buffer will be used next loop.
问题是,在成功处理第一个命令后,输入的任何其他命令都不会得到处理。
我已经打印出 yylineno (当 Flex 扫描仪看到 '\n' 时它会增加),并且在第一个命令之后它只增加一次。
我无法确定是否是我在使用 Flex 时做错了什么,或者是否是 yyparse 在第一次执行后停止调用扫描仪。
如果有人能准确指出发生了什么,我将非常高兴。
Theres a bit of C code something like:
int doCommand(char* command)
{
// +2 on strlen is for the two extra '\0' characters
// needed by flex when scanning strings.
YY_BUFFER_STATE yybs = yy_scan_buffer(command, strlen(command)+2);
yy_switch_to_buffer(yybs);
yyparse();
yy_delete_buffer(yybs);
}
It gets called in a loop something like (psuedocode):
read characters upto and including '\n' into a buffer;
add two '\0' characters;
call doCommand(buffer);
zero the buffer; // NOTE: same buffer will be used next loop.
What goes wrong is that after the first command has been processed successfully, any further commands entered do not get processed.
I have printed out yylineno (which gets increased when the flex scanner sees '\n') and it only increases once, after the first command.
I cant work out if it is something Im doing wrong with flex, or if it is yyparse that stops calling the scanner after the first go.
I would be most pleased if someone could point out exactly what is happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能尝试一下调试吗?检查读取了哪些令牌。是否及时切换,输入是什么。
我不使用 Flex 增强功能,因为我需要可移植性,因此我通过 YY_INPUT 稍微不同地实现了此机制。可能生成的令牌与您期望的不同,所以我建议首先调试 lex 部分。
Can you please try this with a debug? Check which tokens are read. Whether it switches in time, what is the input.
I don't use flex enhancements, because i need portability, so i implement this mechanism a bit differently - through YY_INPUT. Probably the produced tokens are different from what you expect, so i advise to debug the lex part first.