YACC program which accept strings that starts and ends with 0 or 1
Problem: Write a YACC program which accept strings that starts and ends with Zero or One
Explanation:
YACC (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: 001100 Output: Sequence Accepted Input: 1001001 Output: Sequence Accepted Input: 0011101 Output: Sequence Rejected Input: 100110 Output: Sequence Rejected
Lexical Analyzer Source Code :
%{ /* Definition section */ extern int yylval; %} /* Rule Section */%% 0 {yylval = 0; return ZERO;} 1 {yylval = 1; return ONE;} .|\n {yylval = 2; return 0;} %% |
Parser Source Code :
%{ /* Definition section */ #include<stdio.h> #include <stdlib.h> void yyerror(const char *str) { printf("\nSequence Rejected\n"); } %} %token ZERO ONE /* Rule Section */%% r : s {printf("\nSequence Accepted\n\n");} ; s : n | ZERO a | ONE b ; a : n a | ZERO ; b : n b | ONE ; n : ZERO | ONE ; %% #include"lex.yy.c" //driver code int main() { printf("\nEnter Sequence of Zeros and Ones : "); yyparse(); printf("\n"); return 0; } |
Output:

Recommended Posts:
- YACC program to recognize strings of { anb | n≥5 }
- Yacc Program to evaluate a given arithmetic expression
- YACC program for Binary to Decimal Conversion
- YACC program to recognize string with grammar { anbn | n≥0 }
- YACC program for Conversion of Infix to Postfix expression
- YACC program to implement a Calculator and recognize a valid Arithmetic expression
- Lex Program to accept string starting with vowel
- Lex Program to accept a valid integer and float value
- Introduction to YACC
- DFA in LEX code which accepts strings ending with 11
- C program to detect tokens in a C program
- Lex Program to remove comments from C program
- Lex Program For checking a valid URL
- Lex program to check whether a given number is even or odd
- Lex program to identify the identifier
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.



