1 /* Generated by re2c */
2 #line 1 "calc_007.re"
3 // re2c $INPUT -o $OUTPUT -b
4 /* re2c lesson 001_upn_calculator, calc_007, (c) M. Boerger 2006 - 2007 */
5 #line 9 "calc_007.re"
6
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 #define DEBUG(stmt) stmt
13
14 int stack[4];
15 int depth = 0;
16
push_num(const unsigned char * t,const unsigned char * l,int radix)17 int push_num(const unsigned char *t, const unsigned char *l, int radix)
18 {
19 int num = 0;
20
21 if (depth >= sizeof(stack))
22 {
23 return 3;
24 }
25
26 --t;
27 while(++t < l)
28 {
29 num = num * radix + (*t - (unsigned char)'0');
30 }
31 DEBUG(printf("Num: %d\n", num));
32
33 stack[depth++] = num;
34 return 0;
35 }
36
stack_add()37 int stack_add()
38 {
39 if (depth < 2) return 4;
40
41 --depth;
42 stack[depth-1] = stack[depth-1] + stack[depth];
43 DEBUG(printf("+\n"));
44 return 0;
45 }
46
stack_sub()47 int stack_sub()
48 {
49 if (depth < 2) return 4;
50
51 --depth;
52 stack[depth-1] = stack[depth-1] - stack[depth];
53 DEBUG(printf("+\n"));
54 return 0;
55 }
56
scan(char * s)57 int scan(char *s)
58 {
59 unsigned char *p = (unsigned char*)s;
60 unsigned char *t;
61 int res = 0;
62
63 #define YYCTYPE unsigned char
64 #define YYCURSOR p
65
66 while(!res)
67 {
68 t = p;
69
70 #line 71 "calc_007.c"
71 {
72 YYCTYPE yych;
73 static const unsigned char yybm[] = {
74 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 64, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0,
78 64, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0,
80 128, 128, 128, 128, 128, 128, 128, 128,
81 128, 128, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0,
96 0, 0, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 0, 0, 0, 0, 0,
98 0, 0, 0, 0, 0, 0, 0, 0,
99 0, 0, 0, 0, 0, 0, 0, 0,
100 0, 0, 0, 0, 0, 0, 0, 0,
101 0, 0, 0, 0, 0, 0, 0, 0,
102 0, 0, 0, 0, 0, 0, 0, 0,
103 0, 0, 0, 0, 0, 0, 0, 0,
104 0, 0, 0, 0, 0, 0, 0, 0,
105 0, 0, 0, 0, 0, 0, 0, 0,
106 };
107 yych = *YYCURSOR;
108 if (yybm[0+yych] & 64) {
109 goto yy6;
110 }
111 if (yych <= ',') {
112 if (yych <= 0x00) goto yy2;
113 if (yych == '+') goto yy9;
114 goto yy4;
115 } else {
116 if (yych <= '/') {
117 if (yych <= '-') goto yy11;
118 goto yy4;
119 } else {
120 if (yych <= '0') goto yy13;
121 if (yych <= '9') goto yy15;
122 goto yy4;
123 }
124 }
125 yy2:
126 ++YYCURSOR;
127 #line 86 "calc_007.re"
128 { res = depth == 1 ? 0 : 2; break; }
129 #line 130 "calc_007.c"
130 yy4:
131 ++YYCURSOR;
132 #line 87 "calc_007.re"
133 { res = 1; continue; }
134 #line 135 "calc_007.c"
135 yy6:
136 yych = *++YYCURSOR;
137 if (yybm[0+yych] & 64) {
138 goto yy6;
139 }
140 #line 81 "calc_007.re"
141 { continue; }
142 #line 143 "calc_007.c"
143 yy9:
144 ++YYCURSOR;
145 #line 84 "calc_007.re"
146 { res = stack_add(); continue; }
147 #line 148 "calc_007.c"
148 yy11:
149 ++YYCURSOR;
150 #line 85 "calc_007.re"
151 { res = stack_sub(); continue; }
152 #line 153 "calc_007.c"
153 yy13:
154 yych = *++YYCURSOR;
155 if (yych <= '/') goto yy14;
156 if (yych <= '9') goto yy17;
157 yy14:
158 #line 83 "calc_007.re"
159 { res = push_num(t, p, 10); continue; }
160 #line 161 "calc_007.c"
161 yy15:
162 yych = *++YYCURSOR;
163 if (yybm[0+yych] & 128) {
164 goto yy15;
165 }
166 goto yy14;
167 yy17:
168 yych = *++YYCURSOR;
169 if (yych <= '/') goto yy19;
170 if (yych <= '9') goto yy17;
171 yy19:
172 #line 82 "calc_007.re"
173 { res = push_num(t, p, 8); continue; }
174 #line 175 "calc_007.c"
175 }
176 #line 88 "calc_007.re"
177
178 }
179 return res;
180 }
181
main(int argc,char ** argv)182 int main(int argc, char **argv)
183 {
184 if (argc > 1)
185 {
186 char *inp;
187 int res = 0, argp = 0, len;
188
189 while(!res && ++argp < argc)
190 {
191 inp = strdup(argv[argp]);
192 len = strlen(inp);
193 if (inp[0] == '\"' && inp[len-1] == '\"')
194 {
195 inp[len - 1] = '\0';
196 ++inp;
197 }
198 res = scan(inp);
199 free(inp);
200 }
201 switch(res)
202 {
203 case 0:
204 printf("Result: %d\n", stack[0]);
205 return 0;
206 case 1:
207 fprintf(stderr, "Illegal character in input.\n");
208 return 1;
209 case 2:
210 fprintf(stderr, "Premature end of input.\n");
211 return 2;
212 case 3:
213 fprintf(stderr, "Stack overflow.\n");
214 return 3;
215 case 4:
216 fprintf(stderr, "Stack underflow.\n");
217 return 4;
218 }
219 }
220 else
221 {
222 fprintf(stderr, "%s <expr>\n", argv[0]);
223 return 0;
224 }
225 }
226