fork download
  1. /* definition section*/ %{ #include<stdio.h> #include<stdlib.h> #include<math.h> //#define YYSTYPE double void yyerror(char *s); float x = 0; %}
  2.  
  3. // creating tokens whose values are given by lex %token ZERO ONE POINT
  4.  
  5. // following a grammer rule which is converting binary number to decimal number (float value) %%
  6.  
  7. L: X POINT Y {printf("%f",$1+x);} | X {printf("%d", $$);} X: X B {$$=$1*2+$2;} | B {$$=$1;} Y: B Y {x=$1*0.5+x*0.5;} | {;} B:ZERO {$$=$1;} |ONE {$$=$1;};
  8.  
  9. %% // main function int main() { printf("Enter the binary number : "); // calling yyparse function which execute grammer rules and lex while(yyparse()); printf("\n"); } // if any error void yyerror(char *s) { fprintf(stdout,"\n%s",s); }
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. /* definitions */ %{ // including required header files #include<stdio.h> #include<stdlib.h> #include"y.tab.h" // declaring a external variable yylval extern int yylval; %}
  23.  
  24. /* rules if 0 is matched ,make yylval to 0 and return ZERO which is variable in Yacc program if 1 is matched ,make yylval to 1 and return ONE which is variable in Yacc program if . is matched ,return POINT which is variable in Yacc program if line change , return 0 otherwise ,ignore*/ %%
  25.  
  26. 0 {yylval=0;return ZERO;} 1 {yylval=1;return ONE;} "." {return POINT;} [ \t] {;} \n return 0;
  27.  
  28. %%
  29.  
  30.  
Success #stdin #stdout #stderr 0.02s 6888KB
stdin
111.011
stdout
Standard output is empty
stderr
ERROR: /home/vnbOiX/prog:30:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit