flex bison C lex yacc 扫描字符串缓冲区

发布于 2024-10-16 06:01:25 字数 771 浏览 3 评论 0原文

有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-10-23 06:01:25

你能尝试一下调试吗?检查读取了哪些令牌。是否及时切换,输入是什么。

我不使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文