Lex and Yacc program to calculate the value of a binary number.

Lex Program: 

%{
#include"y.tab.h"
extern int yylval;
%}
%%
[0] {yylval=atoi(yytext); return ZERO;}
[1] {yylval=atoi(yytext); return ONE;}
[\t] ;
[\n] return 0;
. return yytext[0];
%%

Yacc Program:
%{
#include<stdio.h>
#include<string.h>

%token ZERO ONE;
%%
s : e {printf("Value : %d",$1);}
e : e s {$$=($1*2)+$2} 
   | s {$$=$1;}
s : ZERO {$$=0;}
   |ONE {$$=1;}
   ;
%%
 main()
{
yyparse();
}
 yyerror(char * arg)
{
printf("Invalid expression");
}

Comments

Popular posts from this blog

A program to find FIRST of non-terminals of the given grammar

Implementation of Symbol Table

Desk Calculator