annotate s-tree.c @ 27:c4e2ca100ab9 default tip

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