我正在尝试使用 win flex 和 bison 解析两个文件,但遇到了 lex 未处于我期望的状态的问题。在 lex 文件中:
include[ \t]+\" { BEGIN(include_state); }
<include_state>([^\\\"\n]|\\.)+ {
yyin = fopen(yytext, "r");
if (!yyin) {
printf("Error opening include file: %s\n", yytext);
return 1;
}
yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE, yyscanner),
yyscanner);
BEGIN(INITIAL);
}
<include_state>\"[ \t]*";" { BEGIN(INITIAL); }
<<EOF>> {
yypop_buffer_state(yyscanner);
if (!YY_CURRENT_BUFFER)
yyterminate();
}
正在解析的第一个文件包含第二个文件,如下所示:
include "hello.txt";
解析时发生的情况是,第二个文件(“hello.txt”)解析正常,没有问题,但返回第一个文件时出现问题。读取行尾的引号和分号,但 lex 处于 INITIAL 状态。所以 lex 与我期望它匹配的规则不匹配。我确信这一点,因为如果我添加以下规则(它匹配):
<INITIAL>\"[ \t]*";" { printf("Right matching, wrong state.\n"); return 1; }
为什么它没有返回到 include_state?我该如何解决这个问题?
最佳答案
看起来它进入了 INITIAL,因为这是您在调用 yypush_buffer_state() 后告诉它要做的事情。它如何才能匹配第二个<include_state>
如果你这样做呢?如果删除该状态更改会发生什么?
关于c - 解析多个文件时错误的 lex 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28216977/