Mercurial > hg > Members > masakoha > testcode
comparison 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 |
comparison
equal
deleted
inserted
replaced
54:f540de861cd6 | 55:883e3473a9f5 |
---|---|
1 /* | |
2 Very Simple Calculator | |
3 $Id$ | |
4 */ | |
5 | |
1 #include <stdio.h> | 6 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 | 7 |
5 typedef struct node { | 8 static char *ptr,*last_ptr; |
6 struct node *left; | 9 static int value,lvalue; |
7 struct node *right; | 10 static int last_token; |
8 int type; | 11 static int variable[48]; |
9 int value; | 12 |
10 } Node, *NodePtr; | 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 *); | |
11 | 19 |
12 | 20 |
13 NodePtr newNode(int type, int value, NodePtr left, NodePtr right) { | 21 static int |
14 NodePtr d = (NodePtr)malloc(sizeof(Node)); | 22 token() |
15 d->type = type; | 23 { |
16 d->value = value; | 24 int c,d; |
17 d->left = left; | 25 |
18 d->right = right; | 26 last_ptr = ptr; /* for error position */ |
19 return d; | 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 } | |
20 } | 64 } |
21 | 65 |
22 int main(int argc, char **argv) { | 66 static int |
67 expr() | |
68 { | |
69 int d,assign; | |
23 | 70 |
24 for (int i = 1; i < argc ; i++) { | 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 } | |
94 } | |
25 | 95 |
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; | |
26 } | 111 } |
112 } | |
27 | 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; | |
135 | |
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 } | |
163 | |
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++; | |
184 } | |
28 return 0; | 185 return 0; |
29 } | 186 } |