Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/agcore/rpk.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, A System for Syntax Directed Programming | |
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved. | |
4 * See the file COPYING for license and usage terms. | |
5 * | |
6 * rpk.cpp | |
7 */ | |
8 | |
9 #include <stdarg.h> | |
10 #include "port.h" | |
11 | |
12 #include "arrays.h" | |
13 #include "config.h" | |
14 #include "csexp.h" | |
15 #include "error.h" | |
16 #include "p.h" | |
17 #include "pgg24-defs.h" | |
18 #include "pgg24.h" | |
19 #include "q1glbl.h" | |
20 #include "rpk.h" | |
21 #include "rproc.h" | |
22 #include "rpz.h" | |
23 #include "rule.h" | |
24 #include "stacks.h" | |
25 #include "symbol.h" | |
26 #include "token.h" | |
27 #include "tree.h" | |
28 #include "tsd.h" | |
29 | |
30 #undef INCLUDE_LOGGING | |
31 //#define INCLUDE_LOGGING | |
32 #include "log.h" | |
33 | |
34 | |
35 #define PCB pgcb | |
36 | |
37 | |
38 AgStack< AgStack<RuleElement> > ruleElementStack; | |
39 | |
40 int makeRule(const AgIndexedContainer<RuleElement> &elementList) { | |
41 LOGSECTION("makeRule(elementList)"); | |
42 LOGV(elementList.size()); | |
43 int j, n; | |
44 | |
45 Rule rule = Rule::create(); | |
46 rule->line = PRULE_CONTEXT(PCB)->y; | |
47 rule->col = PRULE_CONTEXT(PCB)->x; | |
48 rule->elementList = AgArray<RuleElement>(elementList); | |
49 LOGV(rule) LCV(elementList.size()); | |
50 n = elementList.size(); | |
51 for (j = 0; j < n; j++) { | |
52 RuleElement element = elementList[j]; | |
53 #ifdef INCLUDE_LOGGING | |
54 int cVariable = element.cVariable; | |
55 LOGV(cVariable) LCV(cVariableList[cVariable]); | |
56 #endif | |
57 if (element.cVariable) { | |
58 valueToken.insert(element.token); | |
59 } | |
60 } | |
61 LOGV(n) LCV(j); | |
62 return rule; | |
63 } | |
64 | |
65 int makeRule(void) { | |
66 LOGSECTION("makeRule"); | |
67 Rule rule = Rule::create(); | |
68 rule->line = PRULE_CONTEXT(PCB)->y; | |
69 rule->col = PRULE_CONTEXT(PCB)->x; | |
70 return rule; | |
71 } | |
72 | |
73 int makeRule(Token token) { | |
74 LOGSECTION("makeRule(token)"); | |
75 LOGV(token); | |
76 Rule rule = Rule::create(); | |
77 rule->elementList = AgArray<RuleElement>(1); | |
78 rule->line = PRULE_CONTEXT(PCB)->y; | |
79 rule->col = PRULE_CONTEXT(PCB)->x; | |
80 rule->elementList[0] = RuleElement(token, 0); | |
81 LOGV(rule) LCV(rule->elementList.size()); | |
82 return rule; | |
83 } | |
84 | |
85 int makeRule(Token token1, Token token2) { | |
86 LOGSECTION("makeRule(token1, token2)"); | |
87 LOGV(token1) LCV(token2); | |
88 | |
89 Rule rule = Rule::create(); | |
90 rule->elementList = AgArray<RuleElement>(2); | |
91 rule->line = PRULE_CONTEXT(PCB)->y; | |
92 rule->col = PRULE_CONTEXT(PCB)->x; | |
93 rule->elementList[0] = RuleElement(token1, 0); | |
94 rule->elementList[1] = RuleElement(token2, 0); | |
95 LOGV(rule) LCV(rule->elementList.size()); | |
96 return rule; | |
97 } | |
98 | |
99 int id_token(int name) { | |
100 LOGSECTION("id_token"); | |
101 LOGV(name) LCV(map_token_name[name].token_number); | |
102 Symbol symbol(name); | |
103 Token token = symbol->token_number; | |
104 LOGV(symbol) LCV(token); | |
105 if (token.isNull()){ | |
106 token = Token::create(); | |
107 token->token_name = symbol; | |
108 symbol->token_number = token; | |
109 } | |
110 return token; | |
111 } | |
112 | |
113 int form_element_1(CharSetExpression *x) { | |
114 LOGSECTION("form_element_1"); | |
115 LOGV((int) x); | |
116 | |
117 if (x->nameNode()) { | |
118 int name = ((NamedCharSet *)x)->name; | |
119 delete x; | |
120 //LOGV(netAllocations()); | |
121 int returnValue = id_token(name); | |
122 LOGV(returnValue) LCV(ntkns); | |
123 return returnValue; | |
124 } | |
125 ParseTree tree(x); | |
126 Token token = tree->token_number; | |
127 if (token.isNotNull()) return token; | |
128 | |
129 token = Token::create(); | |
130 LOGV(token) LCV(kits); | |
131 token->parse_tree = tree; | |
132 token->operatorCandidate = 1; | |
133 | |
134 tree->token_number = token; | |
135 LOGV((int) tree->expression); | |
136 token->value_type = default_input_type; | |
137 return token; | |
138 } | |
139 | |
140 void shell_production(int t, int tn) { | |
141 LOGSECTION("shell_production"); | |
142 Token token(t); | |
143 | |
144 Rule rule = makeRule(tn); | |
145 rule->prim_tkn = t; | |
146 rule->fast_loop = 1; | |
147 at(bnf_table,t,(int) rule); | |
148 token->non_terminal_flag = 1; | |
149 token->value_type = Token(t)->value_type; | |
150 } | |
151 | |
152 void defineImmediateProc(Procedure p) { | |
153 LOGSECTION("defineImmediateProc"); | |
154 token_name(); | |
155 Token token(fis()); | |
156 Rule rule = makeRule(); | |
157 rule->proc_name = p; | |
158 LOGV(rule) LCV(rule->proc_name); | |
159 p->form_number = rule; | |
160 token->non_terminal_flag = 1; | |
161 token->zero_length_flag = 1; | |
162 rule->prim_tkn = token; | |
163 at(bnf_table, (int) token, (int) rule); | |
164 } | |
165 | |
166 void gen_vp_prods(int tn, int zl, int vptn) { | |
167 LOGV("get_vp_prods"); | |
168 int nf = tis(); | |
169 int j; | |
170 token_number_map *tp = &map_token_number[tn]; | |
171 | |
172 tp->vp_prod_number = vptn; | |
173 tp->value_type = default_token_type; | |
174 if (nf) { | |
175 tp->non_terminal_flag = 1; | |
176 } | |
177 tp->fine_structure = 1; | |
178 tp->zero_length_flag = zl; | |
179 for (j = 0; j < nf; j++) { | |
180 map_form_number[list_base[j]].prim_tkn = tn; | |
181 at(bnf_table,tn,list_base[j]); | |
182 } | |
183 rws(); | |
184 } | |
185 | |
186 /* | |
187 form1 creates an empty grammar rule. Attach it to a token in the bnf and | |
188 you have a null production. | |
189 */ | |
190 | |
191 int form1(void) { | |
192 LOGSECTION("form1"); | |
193 Rule rule = Rule::create(); | |
194 LOGV(rule) LCV(kits); | |
195 rule->line = PRULE_CONTEXT(PCB)->y; | |
196 rule->col = PRULE_CONTEXT(PCB)->x; | |
197 return rule; | |
198 } | |
199 | |
200 | |
201 void vp_forms(void) { | |
202 LOGSECTION("vp_forms"); | |
203 int *lb, nl, k; | |
204 | |
205 nl = tis(); | |
206 list_space(2*nl); | |
207 lb = list_base; | |
208 for (k = 0; k < nl; k++) { | |
209 VpRule vpRule = lb[k]; | |
210 LOGV(vpRule) LCV(vpRule->procedureName); | |
211 Rule rule = makeRule(vpRule->elementList); | |
212 Procedure proc(vpRule->procedureName); | |
213 proc->form_number = rule; | |
214 rule->proc_name = vpRule->procedureName; | |
215 LOGV(rule) LCV(rule->proc_name); | |
216 lb[k] = rule; | |
217 } | |
218 } | |
219 | |
220 /* -> left brace, vp rules, right brace =vp_1(); */ | |
221 | |
222 int vp_1(void) { | |
223 LOGSECTION("vp_1"); | |
224 int nf, j; | |
225 int i, zl; | |
226 int tn; | |
227 int flag, vptn; | |
228 token_number_map *tp; | |
229 | |
230 aws(1); | |
231 flag = idl(vp_prod_dict); | |
232 vptn = fis(); | |
233 check_size(map_vp_prod_number, vptn, vptn + vptn/2); | |
234 if (flag == 0) { | |
235 tn = map_vp_prod_number[vptn].token_number; | |
236 LOGV(tn) LCV(ntkns); | |
237 return tn; | |
238 } | |
239 | |
240 zl = 0; | |
241 fdl(vp_prod_dict,vptn); | |
242 fws(); /* get rid of type word that was appended above */ | |
243 Token::create(); | |
244 vp_forms(); | |
245 nf = tis(); | |
246 map_vp_prod_number[vptn].token_number = ntkns; | |
247 tp = &map_token_number[ntkns]; | |
248 tp->vp_prod_number = vptn; | |
249 tp->value_type = default_token_type; | |
250 if (nf) tp->non_terminal_flag = 1; | |
251 tp->fine_structure = 1; | |
252 for (i = 0; i < nf; i++) { | |
253 Rule rule(list_base[i]); | |
254 rule->prim_tkn = ntkns; | |
255 if (rule->length() == 0) { | |
256 zl = 1; | |
257 } | |
258 } | |
259 tp->zero_length_flag = zl; | |
260 for (j = 0; j < nf; j++) { | |
261 at(bnf_table, ntkns, list_base[j]); | |
262 } | |
263 rws(); | |
264 LOGV(ntkns); | |
265 return ntkns; | |
266 } | |
267 | |
268 /* -> left brace, vp rules, "}...", blank?... =vp_2(); */ | |
269 | |
270 int vp_2(void) { | |
271 LOGSECTION("vp_2"); | |
272 int nf, j, rt; | |
273 int i, zl = 0; | |
274 int tn; | |
275 int flag, vptn; | |
276 | |
277 aws(2); | |
278 flag = idl(vp_prod_dict); | |
279 vptn = fis(); | |
280 check_size(map_vp_prod_number,vptn,vptn + vptn/2); | |
281 if (flag == 0) { | |
282 tn = map_vp_prod_number[vptn].token_number; | |
283 LOGV(tn) LCV(ntkns); | |
284 return tn; | |
285 } | |
286 fdl(vp_prod_dict,vptn); | |
287 fws(); | |
288 rt = vp_1(); | |
289 Token token = Token::create(); | |
290 map_vp_prod_number[vptn].token_number = token; | |
291 token->vp_prod_number = vptn; | |
292 token->value_type = default_token_type; | |
293 | |
294 sws(makeRule(rt)); | |
295 aws(makeRule(ntkns, rt)); | |
296 | |
297 nf = tis(); | |
298 if (nf) { | |
299 token->non_terminal_flag = 1; | |
300 } | |
301 token->fine_structure = 1; | |
302 for (i = 0; i < nf; i++) { | |
303 Rule rule(list_base[i]); | |
304 rule->prim_tkn = ntkns; | |
305 if (rule->length() == 0) { | |
306 zl = 1; | |
307 } | |
308 rule->fast_loop = 1; | |
309 } | |
310 //fp->fast_loop = 1; //must be an error | |
311 token->zero_length_flag = zl; | |
312 for (j = 0; j < nf; j++) { | |
313 at(bnf_table, (int) token, list_base[j]); | |
314 } | |
315 rws(); | |
316 LOGV(ntkns); | |
317 return token; | |
318 } | |
319 | |
320 /* -> left bracket, proper vp rule specs, "]...", blank?... =vp_4(); */ | |
321 Token vpRepeatToken; | |
322 | |
323 int vp_4(void) { | |
324 LOGSECTION("vp_4"); | |
325 int rt; | |
326 int tn; | |
327 int flag, vptn; | |
328 extern int vp_2(void); | |
329 extern int form1(void); | |
330 | |
331 aws(4); | |
332 flag = idl(vp_prod_dict); | |
333 vptn = fis(); | |
334 check_size(map_vp_prod_number,vptn,vptn + vptn/2); | |
335 if (flag == 0) { | |
336 tn = map_vp_prod_number[vptn].token_number; | |
337 LOGV(tn) LCV(ntkns); | |
338 return tn; | |
339 } | |
340 fdl(vp_prod_dict, vptn); | |
341 fws(); | |
342 rt = vp_2(); | |
343 vpRepeatToken = rt; | |
344 map_vp_prod_number[vptn].token_number = Token::create(); | |
345 sws(form1()); | |
346 aws(makeRule(rt)); | |
347 gen_vp_prods(ntkns,1,vptn); | |
348 map_form_number[nforms].fast_loop = 1; | |
349 LOGV(ntkns); | |
350 return ntkns; | |
351 } | |
352 | |
353 int vp_form3(int pn) { | |
354 LOGSECTION("vp_form3"); | |
355 int vpfn; | |
356 | |
357 AgStack<RuleElement> elementList = ruleElementStack.pop(); | |
358 vpfn = VpRule(elementList, pn); | |
359 return vpfn; | |
360 } | |
361 | |
362 void warning_here(const char *fs, ...) { | |
363 LOGSECTION("warning_here"); | |
364 LOGV(fs); | |
365 va_list ap; | |
366 va_start(ap,fs); | |
367 char buf[500]; | |
368 vsprintf(buf,fs,ap); | |
369 log_error(buf); | |
370 va_end(ap); | |
371 LOGS("warning_here complete"); | |
372 } | |
373 | |
374 /* -> union:n, "?...", blank?... =vp_6(form_element_1(n)); */ | |
375 | |
376 int vp_6(int tn) { | |
377 LOGSECTION("vp_6"); | |
378 int flag, vptn; | |
379 int rt; | |
380 | |
381 sws(tn); aws(6); | |
382 flag = idl(vp_prod_dict); | |
383 vptn = fis(); | |
384 check_size(map_vp_prod_number,vptn,vptn + vptn/2); | |
385 if (flag == 0) { | |
386 tn = map_vp_prod_number[vptn].token_number; | |
387 return tn; | |
388 } | |
389 rt = vp_7(tn); | |
390 vpRepeatToken = rt; | |
391 map_vp_prod_number[vptn].token_number = Token::create(); | |
392 sws(form1()); | |
393 aws(makeRule(rt)); | |
394 gen_vp_prods(ntkns,0,vptn); | |
395 map_form_number[nforms].fast_loop = 1; | |
396 return ntkns; | |
397 } | |
398 | |
399 /* -> union:n, ellipsis =vp_7(form_element_1(n)); */ | |
400 | |
401 int vp_7(int tn) { | |
402 LOGSECTION("vp_7"); | |
403 int flag, vptn; | |
404 | |
405 sws(tn); aws(7); | |
406 flag = idl(vp_prod_dict); | |
407 vptn = fis(); | |
408 check_size(map_vp_prod_number,vptn,vptn + vptn/2); | |
409 if (flag == 0) { | |
410 tn = map_vp_prod_number[vptn].token_number; | |
411 return tn; | |
412 } | |
413 map_vp_prod_number[vptn].token_number = Token::create(); | |
414 sws(makeRule(tn)); | |
415 aws(makeRule(ntkns, tn)); | |
416 gen_vp_prods(ntkns,0,vptn); | |
417 map_form_number[nforms].fast_loop = 1; | |
418 return ntkns; | |
419 } | |
420 |