I have write a grammar for number palindrome in yacc. But it didn't accept the input string. Here is my grammar :
T: PAL '\n' {printf("Accepted\n");return 0;};
PAL: "1" PAL "1"|
"2" PAL "2"|
"3" PAL "3"|
"4" PAL "4"|
"5" PAL "5"|
"6" PAL "6"|
"7" PAL "7"|
"8" PAL "8"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"|"0"|;
When I input the number it will show the error message.
Here is my yacc file contents
pal.y
%{
#include<stdio.h>
%}
%%
T: PAL '\n' {printf("Accepted\n");return 0;};
PAL: "1" PAL "1"|
"2" PAL "2"|
"3" PAL "3"|
"4" PAL "4"|
"5" PAL "5"|
"6" PAL "6"|
"7" PAL "7"|
"8" PAL "8"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"|"0"|;
%%
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
int yyerror(char *S)
{
printf("Not Accepted\n");
}
And here is the lex file
pal.l
%{
#include "y.tab.h"
%}
%%
\n {return *yytext;}
. {return *yytext;}
%%
please help me.
"1"
does NOT match an input of1
-- you need to use'1'
in the .y file to match single character tokens. Using double-quotes ("
) in the .y file does something very non-intuitive and useless in most cases. – Chris Dodd"..."
tokens in bison because I don't know any good way to return them from the lexer: I believe that it is important that you post your comment as an answer to somehow clarify this tricky trap ☺ – JJoao