0
|
1 /*
|
|
2 Very Simple Calculator (Parser Part, Tree Printer)
|
|
3 $Id$
|
|
4 */
|
|
5
|
|
6 #include <stdio.h>
|
19
|
7 #include <stdlib.h>
|
|
8 #include "s-token.h"
|
|
9
|
|
10 // extern int label;
|
|
11 // extern char *comments;
|
0
|
12
|
|
13 int variable[48];
|
|
14
|
|
15 typedef struct node {
|
|
16 struct node *left;
|
|
17 struct node *right;
|
|
18 int type;
|
|
19 int value;
|
|
20 } node;
|
|
21
|
|
22 static node *expr();
|
|
23 static node *aexpr();
|
|
24 static node *mexpr();
|
|
25 static node *term();
|
|
26 static node *new_node();
|
|
27 static void print_node();
|
|
28
|
|
29 static node *
|
|
30 new_node(type,value,left,right)
|
|
31 int type;
|
|
32 int value;
|
|
33 node *left;
|
|
34 node *right;
|
|
35 {
|
|
36 node *d;
|
|
37 d = (node *)malloc(sizeof(node));
|
|
38 d->type = type;
|
|
39 d->value = value;
|
|
40 d->left = left;
|
|
41 d->right = right;
|
|
42 return d;
|
|
43 }
|
|
44
|
|
45 static int tree_level = 0;
|
|
46
|
|
47 static void
|
|
48 print_node(d)
|
|
49 node *d;
|
|
50 {
|
|
51 int i;
|
|
52 for(i=0;i<tree_level;i++) { putchar(' '); }
|
|
53 switch(d->type) {
|
|
54 case '0':
|
|
55 printf("value(%d",d->value);
|
|
56 break;
|
|
57 case 'v':
|
|
58 printf("variable(%c",d->value+'a');
|
|
59 break;
|
|
60 default:
|
|
61 printf("node(%c",d->type);
|
|
62 }
|
|
63 if(d->left) {
|
|
64 tree_level++;
|
|
65 printf(",(\n"); print_node(d->left); printf(")");
|
|
66 tree_level--;
|
|
67 }
|
|
68 if(d->right) {
|
|
69 tree_level++;
|
|
70 printf(",(\n"); print_node(d->right); printf(")");
|
|
71 tree_level--;
|
|
72 }
|
|
73 printf(")");
|
|
74 }
|
|
75
|
|
76 static node *
|
|
77 expr()
|
|
78 {
|
|
79 node *d;
|
|
80
|
|
81 d = aexpr();
|
|
82 while(last_token!=EOF) {
|
|
83 switch(last_token) {
|
|
84 case '>':
|
|
85 d = new_node('>',0,d,aexpr());
|
|
86 break;
|
|
87 case '=':
|
|
88 d = new_node('=',0,d,aexpr());
|
|
89 break;
|
|
90 case ')':
|
|
91 return d;
|
|
92 default:
|
|
93 error("Bad expression");
|
|
94 return d;
|
|
95 }
|
|
96 }
|
|
97 return d;
|
|
98 }
|
|
99
|
|
100 static node *
|
|
101 aexpr()
|
|
102 {
|
|
103 node *d;
|
|
104
|
|
105 d = mexpr();
|
|
106 while(last_token!=EOF) {
|
|
107 switch(last_token) {
|
|
108 case '-':
|
|
109 d = new_node('-',0,d,mexpr());
|
|
110 break;
|
|
111 case '+':
|
|
112 d = new_node('+',0,d,mexpr());
|
|
113 break;
|
|
114 default:
|
|
115 return d;
|
|
116 }
|
|
117 }
|
|
118 return d;
|
|
119 }
|
|
120
|
|
121 static node *
|
|
122 mexpr()
|
|
123 {
|
|
124 node *d;
|
|
125
|
|
126 d = term();
|
|
127 while(last_token!=EOF) {
|
|
128 switch(last_token) {
|
|
129 case '*':
|
|
130 d = new_node('*',0,d,term());
|
|
131 break;
|
|
132 case '/':
|
|
133 d = new_node('/',0,d,term());
|
|
134 break;
|
|
135 default:
|
|
136 return d;
|
|
137 }
|
|
138 }
|
|
139 return d;
|
|
140 }
|
|
141
|
|
142 static node *
|
|
143 term()
|
|
144 {
|
|
145 node *d;
|
|
146
|
|
147 lvalue= -1;
|
|
148 token();
|
|
149 if(last_token==EOF) {
|
|
150 error("Term expected");
|
|
151 }
|
|
152 switch(last_token) {
|
|
153 case '0':
|
|
154 d = new_node('0',value,NULL,NULL);
|
|
155 token();
|
|
156 return d;
|
|
157 case 'v':
|
|
158 d = new_node('v',value,NULL,NULL);
|
|
159 token();
|
|
160 return d;
|
|
161 case '(':
|
|
162 d = expr();
|
|
163 if(last_token != ')') {
|
|
164 error("Unbalanced parenthsis");
|
|
165 }
|
|
166 token();
|
|
167 return d;
|
|
168 default:
|
|
169 token();
|
|
170 error("Unknown term");
|
|
171 return 0;
|
|
172 }
|
|
173 }
|
|
174
|
|
175 int
|
|
176 main()
|
|
177 {
|
|
178 node *d;
|
|
179 char buf[BUFSIZ];
|
|
180
|
|
181 while (fgets(buf,BUFSIZ,stdin)) {
|
|
182 printf("%s\n",buf);
|
|
183 ptr = buf;
|
|
184 d = expr();
|
|
185 tree_level = 0;
|
|
186 print_node(d);
|
|
187 printf(".\n\n");
|
|
188 fflush(stdout);
|
|
189 }
|
|
190 return 0;
|
|
191 }
|
|
192
|
|
193
|
|
194 /* end */
|