<br />//(Yacc File : Save as .y) //to compile and Run //:-> yacc -d FileName.y //:-> lex FileName.l //:-> gcc lex.yy.c y.tab.c -ll //:-> ./a.output //:-> enter input as - 101 //:-> output:-> 5 %{ #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); } // (Lex File : save as .l) %{ #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]; %%
Output :-
101
5