forked from mneko22/Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.l
105 lines (88 loc) · 1.58 KB
/
main.l
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
OPERATER [\+\-\*\/]
BRANK [\t ]
%{
#include <stdlib.h>
//#define DEBUG
//数値をスタック
int num_count = 0;
int num_stack[100];
//演算子をスタック
int operater_count = 0;
char operater_stack[100];
int ans = 0;
void setNum(char* num) {
num_stack[++num_count] = atoi(num);
#ifdef DEBUG
printf("debug - setNum(): %d\n", num_stack[num_count]);
#endif
}
void setOperater(char* ope) {
operater_stack[++operater_count] = ope[0];
#ifdef DEBUG
printf("debug - setOperater(): %c\n", operater_stack[operater_count]);
#endif
}
void add() {
while(1) {
#ifdef DEBUG
printf("debug - add(): %d\n", num_stack[num_count-1]);
#endif
ans += num_stack[num_count--];
if(num_count == 0) {
break;
}
}
}
void sub() {
}
void mul() {
}
void divi() {
}
void sum() {
switch(operater_stack[operater_count--]) {
case '+':
add();
break;
case '-':
sub();
break;
case '*':
mul();
break;
case '/':
divi();
break;
default:
printf("Not define OPERATER: %s", operater_stack[operater_count+1]);
break;
}
return;
}
void check_action_stack() {
if(operater_count == 0) {
sum();
printf("ans = %d\n", ans);
}
if(operater_count < 0) {
printf("operater_count ERROR");
operater_count = 0;
num_count = 0;
}
}
%}
%%
"(" { }
")" { }
{OPERATER} { printf("Ope: %s\n", yytext); setOperater(yytext);}
[[:digit:]]+ { printf("Num: %s\n", yytext); setNum(yytext);}
{BRANK} { }
[\r\n] { check_action_stack(); }
. { printf("undefine: %s", yytext); }
%%
int main() {
yylex();
sum();
printf("Answer = %d\n", ans);
return 0;
}