My question is kinda simple, i suppose. Here is my code,
%{
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int yylex(void);
void yyerror(char const *c);
int yywarp(){
return 1;
}
%}
%token LETTER MID
%%
input: {printf("Enter the polindrom:\n");}
| input line;
line: anal '\n' {printf("good\n\n");}
| error '\n' {yyerrok;}
;
anal: MID MID {
printf("%d %d\n", $1, $2);
if($1!=$2)
YYERROR;
}
|LETTER anal LETTER {
printf("%d %d\n", $1, $3);
if($1!=$3)
YYERROR;
}
;
%%
int yylex(void)
{
int c;
char a[10];
c = getchar();
if(c == 'a'){
yylval = c;
return MID;
}
if(isalpha(c)){
yylval = c;
return LETTER;
}
if(c == EOF)
return 0;
return c;
}
void yyerror(char const *c)
{
fprintf(stderr, "%s\n", c);
}
int main(){
return(yyparse());
}
As you can see, MID returns only when you input 'a'. My question is: how to get real middle element? The task is to recognize palindromes using YACC. But i think my teacher won't accept it if i just use "for" cycle for it.