Bison调用yyerror并打印成功



由于某种原因,在尝试使用bison并在其上测试输入时,无论发生什么,我每次都会成功和失败。我不确定我做错了什么。这是我的y.y文件:

%{
#include <stdio.h>
int yylex(void);
int yyerror(char *);
%}
%token LEFTPAREN RIGHTPAREN ASSIGNMENT SEMICOLON IF THEN ELSE BEGIN END WHILE
%token DO PROGRAM VAR AS INT BOOL WRITEINT READINT NUMBER LITERAL
%token OP2 OP3 OP4 IDENTIFIER
%start program
%%
program : PROGRAM declerations BEGIN statementSequence END
;
declerations : VAR IDENTIFIER AS type SEMICOLON declerations |
;
type : INT | BOOL
;
statementSequence : statement SEMICOLON statementSequence |
;
statement : assignment | ifStatement | whileStatement | writeInt
;
assignment : IDENTIFIER ASSIGNMENT expression | IDENTIFIER ASSIGNMENT READINT
;
ifStatement : IF expression THEN statementSequence elseClause END
;
elseClause : ELSE statementSequence |
;
whileStatement : WHILE expression DO statementSequence END
;
writeInt : WRITEINT expression
;
expression : simpleExpression | simpleExpression OP4 simpleExpression
;
simpleExpression : term OP3 term | term
;
term : factor OP2 factor | factor
;
factor : IDENTIFIER | NUMBER | LITERAL | LEFTPAREN expression RIGHTPAREN
;
%%
int yyerror(char *s) {
printf("yyerror : %sn",s);
}
int main(){
yyparse();
printf("SUCCESSn");
return 0;
}
如果解析成功,则yyparse()返回0,如果解析失败,则返回1,如果内存不足,则返回2。但是您忽略了返回值,并且总是打印";成功";。因此,";成功;即使解析失败也会打印。怎样才能阻止这种情况的发生?

最新更新