Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <math.h>
- double vbltable[26];
- %}
- %union {
- double dval;
- int vblno;
- }
- %token <vblno> NOMBRE
- %token <dval> NUMERO
- %left '+' '-'
- %left '*' '/'
- %left '^'
- %nonassoc UMINUS
- %type <dval> expresion
- %%
- list_instruccion: instruccion '\n'
- | list_instruccion instruccion '\n'
- ;
- instruccion: NOMBRE '=' expresion {vbltable[$1] =$3;}
- | expresion { printf("= %g\n", $1); }
- ;
- expresion: expresion '+' expresion {$$ = $1 + $3;}
- | '-' expresion %prec UMINUS {$$ = - $2; }
- | '(' expresion ')' {$$ = $2; }
- | expresion '-' expresion {$$ = $1 - $3;}
- | expresion '*' expresion {$$ = $1 * $3;}
- | expresion '/' expresion {
- if ($3 == 0){
- printf("Division entre 0 ");
- $$ = 999999;
- } else {
- $$ = $1 / $3;
- }
- }
- | expresion '^' expresion {
- $$ = pow($1,$3);
- }
- | NUMERO
- | NOMBRE {$$ = vbltable[$1]; }
- ;
- %%
- main(){yyparse();yylex();while (yylex());
- return 0;}
- int yywrap() {
- return 1;
- }
- yyerror(char *s) {fprintf(stderr, "%s\n",s);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement