Flex/Bison:令牌管理不好?

gni*_*moo 5 c++ parsing lex bison flex-lexer

我的词法分析器和解析器中存在问题.

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

"if"    beginScope(stOTHER); return IF;
Run Code Online (Sandbox Code Playgroud)

在我的解析器中:

stmt: IF '(' exp ')' stmts
...
stmts: stmt
       | '{' stmt_list '}'
       | '{' '}'
Run Code Online (Sandbox Code Playgroud)

在这样的代码中:

if(sth) {
    dosth;
}

if(other) {
    doothersth;
}
Run Code Online (Sandbox Code Playgroud)

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

请帮我...

vit*_*aut 1

正如扎克在评论中提到的,您应该beginScope从解析器操作中调用:

stmt: IF { beginScope(stOTHER); } '(' exp ')' stmts
Run Code Online (Sandbox Code Playgroud)