I have written a yacc program for generating palindromic strings consisting of 0s and 1s. Here is the rules section of the yacc program below:
%%
program: expr NL { printf("Valid string.\n"); exit(0); }
;
expr: ZERO expr ZERO
| ONE expr ONE
| ZERO
| ONE
|
;
%%
Here ZERO
is the token representing 0, ONE
is the token representing 1, and NL
represents \n
. Using yacc on the above grammar, I'm given the following warnings.
yacc -dt --verbose 7b.y
7b.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
7b.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
To the best of my knowledge, the grammar above seems to be unambiguous. My questions are:
- Why is yacc giving me this error?
- What should I change to resolve this?
Here are the complete lex and yacc programs if they should help:
Lex:
%{
#include <stdlib.h>
void yyerror(char *);
#include "y.tab.h"
%}
%%
[0] { yylval = 0; return ZERO; }
[1] { yylval = 1; return ONE; }
\n { return NL; }
. yyerror("invalid character");
%%
int yywrap(void) {
return 1;
}
Yacc:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror(char *);
%}
%token ZERO ONE NL
%%
program: expr NL { printf("Valid string.\n"); exit(0); }
;
expr: ZERO expr ZERO
| ONE expr ONE
| ZERO
| ONE
|
;
%%
void yyerror(char *s) {
fprintf(stderr, "Invalid string.\n");
}
int main(void) {
yyparse();
return 0;
}
Thanks for your help in advance.