YACC program to recognize string with grammar { anbn | n≥0 }
Problem: Write YACC program to recognize string with grammar { anbn | n≥0 }.
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: ab Output: valid string Input: aab Output: invalid string Input: aabb Output: valid string Input: abb Output: invalid string Input: aaabbb Output: valid string
Lexical Analyzer Source Code :
%{ /* Definition section */ #include "y.tab.h" %} /* Rule Section */ %% [aA] {return A;} [bB] {return B;} \n {return NL;} . {return yytext[0];} %% int yywrap() { return 1; } |
Parser Source Code :
%{ /* Definition section */ #include<stdio.h> #include<stdlib.h> %} %token A B NL /* Rule Section */%% stmt: S NL { printf("valid string\n"); exit(0); } ; S: A S B | ; %% int yyerror(char *msg) { printf("invalid string\n"); exit(0); } //driver code main() { printf("enter the string\n"); yyparse(); } |
Output:

Recommended Posts:
- YACC program to recognize strings of { anb | n≥5 }
- YACC program to implement a Calculator and recognize a valid Arithmetic expression
- Lex program to recognize valid arithmetic expression and identify the identifiers and operators
- Yacc Program to evaluate a given arithmetic expression
- YACC program for Binary to Decimal Conversion
- YACC program for Conversion of Infix to Postfix expression
- YACC program which accept strings that starts and ends with 0 or 1
- Program to calculate First and Follow sets of given grammar
- Introduction to YACC
- Compiler Design | Ambiguous Grammar
- Regular Expression Vs Context Free Grammar
- Theory of Computation | Operator grammar and precedence parser
- Lex program to check whether given string is Palindrome or Not
- Lex program to find the Length of a String
- YAAC program to check whether given string is Palindrome or not
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.



