YACC program to recognize strings of { anb | n≥5 }
Problem: Write YACC program to recognize strings of { anb | n≥5 }
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: invalid string Input: aaaaab Output: valid string Input: aabb Output: invalid string Input: aaaaaaab Output: valid string Input: aaaaaabb Output: invalid 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: A A A A A S B NL {printf("valid string\n"); exit(0);} ; S: S A | ; %% 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 string with grammar { anbn | n≥0 }
- YACC program to implement a Calculator and recognize a valid Arithmetic expression
- YACC program which accept strings that starts and ends with 0 or 1
- 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
- Introduction to YACC
- DFA in LEX code which accepts strings ending with 11
- Lex Program to remove comments from C program
- C program to detect tokens in a C program
- Lex program to identify the identifier
- Lex Program For checking a valid URL
- Lex program to check whether a given number is even or odd
- Lex Program to count number of words
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.



