Flex/Bison:糟糕的令牌管理

Flex/Bison: Bad token management?

本文关键字:令牌 管理 Bison Flex      更新时间:2023-10-16

我的词法器和解析器有问题。

首先,在我的词法分析器中,我有这样一行:

"if"    beginScope(stOTHER); return IF;

在我的解析器中:

stmt: IF '(' exp ')' stmts
...
stmts: stmt
       | '{' stmt_list '}'
       | '{' '}'

在这样的代码中:

if(sth) {
    dosth;
}
if(other) {
    doothersth;
}

beginScope 会被调用两次,因为(我认为)Bison 不知道if语句的结尾在哪里,所以当它找到IF令牌时,他将其作为if语句的结尾,并再次阅读它以开始另一个if语句......

请帮帮我...

正如 Zack 在注释中提到的,您应该从解析器操作调用 beginScope

stmt: IF { beginScope(stOTHER); } '(' exp ')' stmts