我正在尝试以下 yacc 代码并且我收到 shift/reduce 错误。我对此很陌生
The Purpose of the code is to prepare the syntax for if - else with logical operators also incorporated
%{
#include<stdio.h>
#include"lex.yy.c"
int syntax_status=0;
%}
%token IF ELS id EE LE GE closep openp num openb closeb SP logicop
%start S
%%
S : S SP IF SP openp SP EXP SP closep SP openb SP closeb SP ELS SP openb SP closeb SP {syntax_status=1;}
| S SP IF SP openp SP EXP SP closep SP openb SP closeb SP {syntax_status = 1;}
|
;
EXP : EXP CMP logicop CMP
| EXP CMP
|
;
CMP : CMP id EE id
| CMP id LE id
| CMP id GE id
| CMP id EE num
| CMP id GE num
| CMP id LE num
| CMP num EE id
| CMP num GE id
| CMP num LE id
|
;
%%
int main()
{
printf("\n\n\n Enter the Syntax : ");
yyparse();
if(syntax_status==1)
{
printf("\n\n\n The Syntax is Correct ");
}
else
{
printf("\n\n\n The Syntax is Imcorrect");
}
return 0;
}
yyerror(char *s)
{
syntax_status=0;
}
此相应 yacc 代码的 Lex 程序如下:
%{
#include<stdio.h>
#include"y.tab.h"
%}
IF (if)
ELS (else)
iden [a-zA-Z][a-zA-Z0-9]*
num [0-9]+
space [ ]*
%%
{IF} { return IF; }
{ELS} {return ELSE;}
{iden} {return id;}
(==) {return EE;}
(<=) { return LE;}
(>=) { return GE;}
")" { return closep;}
"(" { return openp;}
{num} { return num;}
{space} { return SP; }
"{" { return openb;}
"}" { return closeb;}
"||"|"&&"|"!=" {return logicop;}
%%