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:

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.



