YACC program for Binary to Decimal Conversion
Problem: Write YACC program for Binary to Decimal Conversion.
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: 0101 Output: 5 Input: 1101 Output: 13 Input: 111001 Output: 57 Input: 1111111 Output: 127 Input: 100111000 Output: 312
Lexical Analyzer Source Code:
%{ /* Definition section */ #include<stdio.h> #include<stdlib.h> #include"y.tab.h" extern int yylval; %} /* Rule Section */ %% 0 {yylval=0;return ZERO;} 1 {yylval=1;return ONE;} [ \t] {;} \n return 0; . return yytext[0]; %% int yywrap() { return 1; } |
Parser Source Code:
%{ /* Definition section */ #include<stdio.h> #include<stdlib.h> void yyerror(char *s); %} %token ZERO ONE /* Rule Section */%% N: L {printf("\n%d", $$);} L: L B {$$=$1*2+$2;} | B {$$=$1;} B:ZERO {$$=$1;} |ONE {$$=$1;}; %% //driver code int main() { while(yyparse()); } yyerror(char *s) { fprintf(stdout, "\n%s", s); } |
Output:

Recommended Posts:
- Lex program for Decimal to Binary Conversion
- YACC program for Conversion of Infix to Postfix expression
- Lex program for Decimal to Hexadecimal Conversion
- YACC program to recognize strings of { anb | n≥5 }
- YACC program to recognize string with grammar { anbn | n≥0 }
- Yacc Program to evaluate a given arithmetic 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 remove comments from C program
- C program to detect tokens in a C program
- Lex program to identify the identifier
- Lex program to check whether a given number is even or odd
- Lex Program For checking a valid URL
- Lex Program to check whether a number is Prime 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.



