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:
%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
0 {yylval=0; return ZERO;}
1 {yylval=1; return ONE;}
[ \t] {;}
\n return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
|
Parser Source Code:
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror( char *s);
%}
%token ZERO ONE
%%
N: L { printf ( "\n%d" , $$);}
L: L B {$$=$1*2+$2;}
| B {$$=$1;}
B:ZERO {$$=$1;}
|ONE {$$=$1;};
%%
int main()
{
while (yyparse());
}
yyerror( char *s)
{
fprintf (stdout, "\n%s" , s);
}
|
Output:
Last Updated :
09 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...