Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include<stdio.h>
- #include<math.h>
- #include<stdlib.h>
- %}
- %union {int num; float NUM;}
- %start line
- %token cos1
- %token sin1
- %token tan1
- %token pow1
- %token <num> number
- %token <NUM> NUMBER
- %type <num> exp
- %%
- line : exp
- | line exp
- ;
- exp : number {$$=$1;}
- | exp '+' number {$$=$1+$3;printf("\n%d+%d=%d\n",$1,$3,$$);}
- | exp '-' number {$$=$1-$3;printf("\n%d-%d=%d\n",$1,$3,$$);}
- | exp '*' number {$$=$1*$3;printf("\n%d*%d=%d\n",$1,$3,$$);}
- | exp '/' number {$$=$1/$3;printf("\n%d/%d=%d\n",$1,$3,$$);}
- | exp '^' number {$$=$1^$3;printf("\n%d^%d=%d\n",$1,$3,$$);}
- | exp '&' number {$$=$1&$3;printf("\n%d&%d=%d\n",$1,$3,$$);}
- | '!' number {$$=!$2;printf("\n!%d=%d\n",$2,$$);}
- | exp '|' number {$$=$1|$3;printf("\n%d|%d=%d\n",$1,$3,$$);}
- | cos1 NUMBER {printf("%f",cos(($2/180)*3.14));}
- | sin1 NUMBER {printf("%f",sin(($2/180)*3.14));}
- | tan1 NUMBER {printf("%f",tan(($2/180)*3.14));}
- | exp pow1 number {printf("%f",pow($1,$3));}
- ;
- %%
- int main(){
- yyparse();
- return 0;
- }
- int yyerror(){
- exit(0);
- }
Add Comment
Please, Sign In to add comment