Yacc/Bison yyerror

问题描述 投票:0回答:3

我正在尝试创建自己的错误,但显然,调用 yyerror() 不足以告诉解析器存在错误。我做了一个小例子来更好地描述我的问题。 因此,这里的解析器必须检查语句是否是两个之间有逗号的数字。而且数字不能以0开头。

yacc 的输入:

%token DIGIT            

%{      
#include <stdio.h> 
#include <stdlib.h>
void yyerror(char *s);    
%}

%%
    list:    |
        list stat ';' {printf("The statement is correct!\n\n");} |
        list error ';' {printf("The statement is incorrect!\n\n");}

    stat:   number ',' number

    number: DIGIT {if ($1==0) yyerror("number starts with 0");} | 
        number DIGIT {$$ = $1*10+$2;}
%%
extern int linenum;
void yyerror(char *s) {
    fprintf(stderr, " line %d: %s\n", linenum, s);
}

对于 lex:

%{
#include <stdio.h>
#include "y.tab.h"
int linenum = 1;
%} 

%% 

[0-9]               {
                 yylval = yytext[0] - '0';
                 return DIGIT;
                }

[ \t\r]+            ;
\n                  ++linenum;
.               return(yytext[0]);

解析器的输入:

34, 43;
32,fs;
03, 23;

这是输出:

The statement is correct!

 line 2: syntax error
The statement is incorrect!

 line 3: number starts with 0
The statement is correct!

即使发现了第3行的错误,解析仍然继续。我该如何解决它?

更新:使用 YYERROR 解决了问题;

c parsing bison yacc
3个回答
0
投票

如果您希望它在检测到一个错误后停止(为什么?),只需从相关生产返回即可。

默认情况下它将执行错误恢复。


0
投票

解析正在继续,因为其中有一条带有

error
的规则,这是一个错误恢复规则,告诉解析器如何从错误中恢复并继续。如果您在发生错误后不想想要继续,请删除错误恢复规则。然后
yyparse
将在出错后立即返回(非零)。


-1
投票

在我看来

yyerror()
只是打印错误消息,但不会在解析器中设置错误状态。也许你可以稍微修改一下语法?

莱克斯:

0          {
             yylval = 0;
             return ZERO;
           }
[1-9]      {
             yylval = yytext[0] - '0';
             return DIGITNOZERO;
           }

yacc:

number: DIGITNOZERO | 
        number DIGITNOZERO  {$$ = $1*10+$2;} | 
        number ZERO {$$ = $1*10;}
© www.soinside.com 2019 - 2024. All rights reserved.