Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include <math.h>
  4. double vbltable[26];
  5. %}
  6. %union {
  7. double dval;
  8. int vblno;
  9. }
  10. %token <vblno> NOMBRE
  11. %token <dval> NUMERO
  12. %left '+' '-'
  13. %left '*' '/'
  14. %left '^'
  15. %nonassoc UMINUS
  16. %type <dval> expresion
  17. %%
  18. list_instruccion: instruccion '\n'
  19. | list_instruccion instruccion '\n'
  20. ;
  21. instruccion: NOMBRE '=' expresion {vbltable[$1] =$3;}
  22. | expresion { printf("= %g\n", $1); }
  23. ;
  24. expresion: expresion '+' expresion {$$ = $1 + $3;}
  25. | '-' expresion %prec UMINUS {$$ = - $2; }
  26. | '(' expresion ')' {$$ = $2; }
  27. | expresion '-' expresion {$$ = $1 - $3;}
  28. | expresion '*' expresion {$$ = $1 * $3;}
  29. | expresion '/' expresion {
  30. if ($3 == 0){
  31. printf("Division entre 0 ");
  32. $$ = 999999;
  33. } else {
  34. $$ = $1 / $3;
  35. }
  36. }
  37. | expresion '^' expresion {
  38. $$ = pow($1,$3);
  39. }
  40. | NUMERO
  41. | NOMBRE {$$ = vbltable[$1]; }
  42. ;
  43. %%
  44. main(){yyparse();yylex();while (yylex());
  45. return 0;}
  46. int yywrap() {
  47. return 1;
  48. }
  49. yyerror(char *s) {fprintf(stderr, "%s\n",s);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement