我已经使用以下规则定义了 LEX 扫描器来扫描(嵌套)注释:
"(*" {
int linenoStart, level, ch;
linenoStart = yylineno;
level = 1;
do {
ch = input();
switch (ch) {
case '(':
ch = input();
if (ch == '*') {
level++;
} else {
unput(ch);
}
break;
case '*':
ch = input();
if (ch == ')') {
level--;
} else {
unput(ch);
}
break;
case '\n':
yylineno++;
break;
}
} while ((level > 0) && (ch > 0));
assert((ch >= 0) || (ch == EOF));
if (level > 0) {
fprintf(stderr, "error: unterminated comment starting at line %d", linenoStart);
exit(EXIT_FAILURE);
}
}
使用 FLEX 2.6.4 进行编译时,当我在包含超过 16382 个字符的注释的输入文件上运行扫描程序时,会收到以下错误消息:
input buffer overflow, can't enlarge buffer because scanner uses REJECT
这是为什么?如何解决这个问题?
当模式匹配时,在本例中
(*
,只能使用从该大小的缓冲区读取的函数YY_BUF_SIZE
检索input
= 16384个字符。这将评论的大小限制为 YY_BUF_SIZE
个字符。要启用任意长度的注释,我们可以使用带有上下文的模式,如下所示:
"(*" {
BEGIN(comment);
commentNestingLevel = 1;
commentStartLine = yylineno;
}
<comment>[^*(\n]+
<comment>\n {
yylineno++;
}
<comment>"*"+/[^)]
<comment>"("+/[^*]
<comment>"(*" commentNestingLevel++;
<comment>"*)" {
commentNestingLevel--;
if (commentNestingLevel == 0) {
BEGIN(INITIAL);
}
}
<comment><<EOF>> {
fprintf(stderr, "error: unterminated comment starting at line %d", linenoStart);
exit(EXIT_FAILURE);
}