Mercurial > hg > Applications > Grep
annotate c/regexParser/main.cc @ 55:883e3473a9f5
fix
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 08 Jun 2015 23:40:09 +0900 |
parents | f540de861cd6 |
children | 8901bc071d33 |
rev | line source |
---|---|
55 | 1 /* |
2 Very Simple Calculator | |
3 $Id$ | |
4 */ | |
5 | |
45 | 6 #include <stdio.h> |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
7 |
55 | 8 static char *ptr,*last_ptr; |
9 static int value,lvalue; | |
10 static int last_token; | |
11 static int variable[48]; | |
12 | |
13 static int expr(); | |
14 static int aexpr(); | |
15 static int mexpr(); | |
16 static int term(); | |
17 static int token(); | |
18 static void error(char *); | |
52
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
19 |
51
898e8d9e1c67
implement variable bitContainers array
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
50
diff
changeset
|
20 |
55 | 21 static int |
22 token() | |
23 { | |
24 int c,d; | |
25 | |
26 last_ptr = ptr; /* for error position */ | |
27 c= *ptr; | |
28 if(!c) { | |
29 last_token = EOF; | |
30 return last_token; | |
31 } | |
32 ptr++; | |
33 if (c<=' ') { /* comment */ | |
34 while(*ptr++); | |
35 ptr--; | |
36 last_token = EOF; | |
37 last_ptr = ptr; | |
38 return last_token; | |
39 } | |
40 | |
41 if('0'<=c && c<='9') { /* Decimal */ | |
42 d = c-'0'; | |
43 while((c= *ptr++)) { | |
44 if('0'<=c && c<='9') { | |
45 d = d*10 + (c - '0'); | |
46 } else { | |
47 break; | |
48 } | |
49 } | |
50 c && ptr--; | |
51 value = d; | |
52 last_token = '0'; | |
53 return last_token; | |
54 | |
55 } else if ('a'<=c && c<='z') { /* variable */ | |
56 value = c-'a'; /* return variable reference */ | |
57 last_token = 'v'; | |
58 return last_token; | |
59 } else { | |
60 last_token = c; | |
61 return last_token; | |
62 return c; | |
63 } | |
64 } | |
65 | |
66 static int | |
67 expr() | |
68 { | |
69 int d,assign; | |
70 | |
71 d = aexpr(); | |
72 assign = lvalue; | |
73 switch(last_token) { | |
74 case '>': | |
75 d = (d > aexpr()); | |
76 return d; | |
77 case '=': | |
78 if(assign>=0) { | |
79 d = expr(); | |
80 variable[assign] = d; | |
81 return d; | |
82 } else { | |
83 error("Bad assignment"); | |
84 return 0; | |
85 } | |
86 case ')': | |
87 return d; | |
88 case EOF: | |
89 return d; | |
90 default: | |
91 error("Bad expression"); | |
92 return d; | |
93 } | |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
94 } |
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
95 |
55 | 96 static int |
97 aexpr() | |
98 { | |
99 int d; | |
100 | |
101 d = mexpr(); | |
102 switch(last_token) { | |
103 case '-': | |
104 d -= aexpr(); | |
105 return d; | |
106 case '+': | |
107 d += aexpr(); | |
108 return d; | |
109 default: | |
110 return d; | |
111 } | |
112 } | |
113 | |
114 static int | |
115 mexpr() | |
116 { | |
117 int d; | |
118 d = term(); | |
119 switch(last_token) { | |
120 case '*': | |
121 d *= mexpr(); | |
122 return d; | |
123 case '/': | |
124 d /= mexpr(); | |
125 return d; | |
126 default: | |
127 return d; | |
128 } | |
129 } | |
130 | |
131 static int | |
132 term() | |
133 { | |
134 int d; | |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
135 |
55 | 136 lvalue= -1; |
137 token(); | |
138 if(last_token==EOF) { | |
139 error("Term expected"); | |
140 } | |
141 switch(last_token) { | |
142 case '0': | |
143 d = value; | |
144 token(); | |
145 return d; | |
146 case 'v': | |
147 d = lvalue = value; | |
148 token(); | |
149 return variable[d]; | |
150 case '(': | |
151 d = expr(); | |
152 if(last_token != ')') { | |
153 error("Unbalanced parenthsis"); | |
154 } | |
155 token(); | |
156 return d; | |
157 default: | |
158 token(); | |
159 error("Unknown term"); | |
160 return 0; | |
161 } | |
162 } | |
50
bb0e88fbbe02
impl bitGet and bitSet
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
49
diff
changeset
|
163 |
55 | 164 static int lineno = 0; |
165 | |
166 void | |
167 error(char *msg) | |
168 { | |
169 fprintf(stderr,"%s on line %d\n",msg, lineno); | |
170 } | |
171 | |
172 int | |
173 main() | |
174 { | |
175 int d; | |
176 char buf[BUFSIZ]; | |
177 | |
178 while (fgets(buf,BUFSIZ,stdin)) { | |
179 ptr = buf; | |
180 d = expr(); | |
181 printf("%s = 0x%08x = %d\n",buf,d,d); | |
182 fflush(stdout); | |
183 lineno++; | |
49
f76fe618d5a7
implement bitset
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
48
diff
changeset
|
184 } |
45 | 185 return 0; |
186 } |