YACC program to check whether given string is Palindrome or not
Problem: Write a YACC program to check whether given string is Palindrome or not.
Explanation:
Yacc (for “yet another compiler compiler.”) is the standard parser generator for the Unix operating system. An open source program, yacc generates code for the parser in the C programming language. The acronym is usually rendered in lowercase but is occasionally seen as YACC or Yacc.
Examples:
Input: naman Output: palindrome Input: geeksforgeeks Output: not palindrome
Lexical Analyzer Source Code :
%{ /* Definition section */ #include <stdio.h> #include <stdlib.h> #include "y.tab.h" %} /* %option noyywrap */ /* Rule Section */%% [a-zA-Z]+ {yylval.f = yytext; return STR;} [-+()*/] {return yytext[0];} [ \t\n] {;} %% int yywrap() { return -1; } |
Parser Source Code:
%{ /* Definition section */ #include <stdio.h> #include <string.h> #include <stdlib.h> extern int yylex(); void yyerror(char *msg); int flag; int i; int k =0; %} %union { char* f; } %token <f> STR %type <f> E /* Rule Section */%% S : E { flag = 0; k = strlen($1) - 1; if(k%2==0){ for (i = 0; i <= k/2; i++) { if ($1[i] == $1[k-i]) { } else { flag = 1; } } if (flag == 1) printf("Not palindrome\n"); else printf("palindrome\n"); printf("%s\n", $1); }else{ for (i = 0; i < k/2; i++) { if ($1[i] == $1[k-i]) { } else { flag = 1; } } if (flag == 1) printf("Not palindrome\n"); else printf("palindrome\n"); printf("%s\n", $1); } } ; E : STR {$$ = $1;} ; %% void yyerror(char *msg) { fprintf(stderr, "%s\n", msg); exit(1); } //driver code int main() { yyparse(); return 0; } |
Output:

Recommended Posts:
- Lex program to check whether given string is Palindrome or Not
- YACC program to recognize string with grammar { anbn | n≥0 }
- YACC program to recognize strings of { anb | n≥5 }
- YACC program for Binary to Decimal Conversion
- Yacc Program to evaluate a given arithmetic expression
- YACC program for Conversion of Infix to Postfix expression
- YACC program which accept strings that starts and ends with 0 or 1
- YACC program to implement a Calculator and recognize a valid Arithmetic expression
- Introduction to YACC
- Lex program to check whether a given number is even or odd
- Lex program to check if a Date is valid or not
- Lex program to check whether the input is digit or not
- Lex Program to check whether a number is Prime or Not
- Lex program to check whether input number is odd or even
- Lex program to check perfect numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rahulsangwan3



