forked from utec-2019/Compiladores
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.y
55 lines (43 loc) · 780 Bytes
/
calc.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
%{
#include <stdio.h>
#include <ctype.h>
%}
%token NUMBER
%%
command: exp {printf("%d\n",$1);}
; /* permite la impresi\'on del resultado */
exp: exp '+' term {$$ = $1 + $3;}
| exp '-' term {$$ = $1 - $3;}
| term {$$ = $1;}
;
term: term '*' factor {$$ = $1 * $3;}
| factor {$$ = $1;}
;
factor: NUMBER {$$=$1;}
| '('exp')' {$$=$2;}
;
%%
main()
{
extern int yydebug;
yydebug=1;
return yyparse();
}
int yylex(void)
{
int c;
while ((c=getchar())== ' ');
// elimina blancos
if (isdigit(c)){
ungetc(c,stdin);
scanf("%d",&yylval);
return NUMBER;
}
if(c=='\n') return 0;
// hace que se detenga el análisis sintáctico
return(c);
}
int yyerror(char * s)
{
fprintf(stderr, "%s\n",s);
} /* permite la impresión de un mensaje de error */