-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcalc.y
60 lines (45 loc) · 877 Bytes
/
rpcalc.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
56
57
58
59
60
%{
// Транслятор обратной польской нотации
// Сборка:
// bison rpcalc.y
// gcc rpcalc.tab.c --std=c99 -Wall -o rpcalc.exe
// rpcalc.exe
#include <ctype.h>
#include <stdio.h>
#define YYSTYPE double
int yylex(void);
void yyerror(const char *s);
%}
%token NUM
%%
input: /* ---- */
| input line
;
line: '\n'
| exp '\n' { printf("\t%.3f\n", $1); }
;
exp: NUM { $$ = $1; }
| exp exp '+' { $$ = $1 + $2; }
| exp exp '-' { $$ = $1 - $2; }
| exp exp '*' { $$ = $1 * $2; }
| exp exp '/' { $$ = $1 / $2; }
;
%%
int yylex(void) {
int c;
while ((c=getchar()) == ' ' || '\t' == c)
;
if ('.' == c || isdigit(c)) {
ungetc(c, stdin);
scanf("%lf", &yylval);
return NUM;
}
if (EOF == c) return 0;
return c;
}
void yyerror(const char *s) {
printf("%s\n", s);
}
int main(int argc, char *argv[]) {
return yyparse();
}