Mercurial > ~dholland > hg > ag > index.cgi
comparison tests/agcl/oldagsrc/y2ag.syn @ 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 | |
3 YACC to AnaGram converter. | |
4 Copyright (c) Jerome T. Holland, 1993 | |
5 All Rights Reserved. | |
6 | |
7 Converts YACC files to AnaGram, attempting to preserve comments. | |
8 Does not convert semantic actions. | |
9 | |
10 */} | |
11 | |
12 | |
13 [ | |
14 line numbers | |
15 nest comments | |
16 parser file name = "#.cpp" | |
17 parser name = y2ag | |
18 sticky { name} | |
19 ] | |
20 | |
21 | |
22 /***** | |
23 | |
24 Character Set Definitions | |
25 | |
26 *****/ | |
27 | |
28 anything = ~eof | |
29 backslash = '\\' | |
30 c literal elem = ~(single quote + double quote + backslash + eof) | |
31 digit = '0-9' | |
32 dot = '.' | |
33 double quote = '"' | |
34 eof = -1 + 0 + 26 | |
35 escapes = 'n' + 'r' + 'b' + 't' + 'f' | |
36 letter = 'a-z' + 'A-Z' + '_' | |
37 printing char = 33..126 | |
38 simple c char = ~('$' + '{' + '}' + single quote + double quote + eof) | |
39 single quote = '\'' | |
40 | |
41 | |
42 /***** | |
43 | |
44 YACC file specification | |
45 | |
46 *****/ | |
47 | |
48 grammar | |
49 -> elc?, | |
50 definition block, | |
51 mark, elc?, | |
52 c code block?, | |
53 rule section, | |
54 tail, eof | |
55 | |
56 mark = "%%" | |
57 | |
58 definition block | |
59 -> definitions ={if (size(config)) | |
60 out << "\n[\n" << (char *) config << "]\n";} | |
61 | |
62 /***** | |
63 | |
64 White space definitions | |
65 | |
66 *****/ | |
67 | |
68 white | |
69 -> ' ' + '\t' + '\v' + '\f' + '\r' + '\n' | |
70 -> comment | |
71 | |
72 comment | |
73 -> comment text, "*/" =comments << "*/"; | |
74 | |
75 comment text | |
76 -> "/*" =comments << "/*"; | |
77 -> comment text, anything:x =comments << x; | |
78 | |
79 elc | |
80 -> '\n' + '\v' + '\f', comment? =reset(white),out << '\n',dump_comments("", ""); | |
81 -> ' ' + '\t', comment? =reset(white), dump_comments("\t", ""); | |
82 -> comment =reset(white), dump_comments("", ""); | |
83 -> elc, '\n' + '\v' + '\f', comment? =out << '\n', dump_comments("", ""); | |
84 -> elc, ' ' + '\t', comment? =dump_comments("\t", ""); | |
85 | |
86 blc | |
87 -> '\n' + '\v' + '\f':c, comment? =reset(white), dump_comments("", ""); | |
88 -> ' ' + '\t':c, comment? =reset(white) << c,dump_comments("", ""); | |
89 -> comment =reset(white),dump_comments("", ""); | |
90 -> blc, '\n' + '\v' + '\f':c, comment? =dump_comments("\t", ""); | |
91 -> blc, ' ' + '\t':c, comment? =white << c, dump_comments("", ""); | |
92 | |
93 clc | |
94 -> '\n' + '\v' + '\f':c, comment? =reset(white),out << cs, cs = "", dump_comments("", ""); | |
95 -> ' ' + '\t':c, comment? =reset(white) << c,dump_comments("", ""); | |
96 -> comment =reset(white),dump_comments("", ""); | |
97 -> clc, '\n' + '\v' + '\f':c, comment? =out << cs, cs = "",dump_comments("", ""); | |
98 -> clc, ' ' + '\t':c, comment? =white << c, dump_comments("", ""); | |
99 | |
100 /***** | |
101 | |
102 Definition statements | |
103 | |
104 *****/ | |
105 | |
106 definitions | |
107 -> | |
108 -> definitions, definition =dump_comments("\n","\n"); | |
109 | |
110 definition | |
111 -> start, identifier, elc? ={ | |
112 out.printf("[ grammar token = %s ]\n",(char *) name); | |
113 grammar_tag = ""; | |
114 } | |
115 -> union, union text, elc? | |
116 -> c code block | |
117 -> token, print token list, elc? =out << '\n'; | |
118 -> left, print prec list, blc? =config << "}\n"; | |
119 -> right, print prec list, blc? =config << "}\n"; | |
120 -> nonassoc, print prec list, blc? =config << "}\n"; | |
121 -> type, type name, print token list, | |
122 elc? =out << '\n'; | |
123 -> expect statement, elc? =out << '\n'; | |
124 | |
125 union text | |
126 -> '{', decl, '}' | |
127 | |
128 decl | |
129 -> {decl name | decl white | decl star | decl term}/... =reset(name); | |
130 | |
131 decl term | |
132 -> ';' ={ | |
133 int decl_ndx = decl_dict << decl_name; | |
134 int type_ndx = decl_dict << (char *) type_acc; | |
135 decl_table[decl_ndx] = type_ndx; | |
136 reset(type_acc); | |
137 reset(name); | |
138 *decl_name = 0; | |
139 } | |
140 | |
141 decl name | |
142 -> name =type_acc << (char *) decl_name, strcpy(decl_name, name), reset(name); | |
143 | |
144 decl white | |
145 -> white... ={if (*decl_name) type_acc << (char *) decl_name << ' ', *decl_name = 0;} | |
146 | |
147 decl star | |
148 -> {'*' =type_acc << (char *) decl_name << '*', *decl_name = 0;}... | |
149 | |
150 expect statement | |
151 -> "%expect", elc?, number:n =out.printf("/* expect %d unresolved conflicts */\n",n); | |
152 | |
153 left | |
154 -> {"%left" | "%<"}, | |
155 white?... =config << " left {"; | |
156 | |
157 nonassoc | |
158 -> {"%nonassoc" | "%binary" | "%2"}, | |
159 white?... =config << " nonassoc {"; | |
160 | |
161 right | |
162 -> {"%right" | "%>"}, | |
163 white?... =config << " right {"; | |
164 | |
165 start | |
166 -> "%start", white?... | |
167 | |
168 token | |
169 -> {"%token" | "%term" | "%0"}, | |
170 white?... | |
171 | |
172 type | |
173 -> "%type", white... // =out << '('; | |
174 | |
175 type name | |
176 -> '<', white?..., name, white?..., | |
177 '>', elc? =out << '(' << decl_dict[decl_table[decl_dict[name]]] << ") ", reset(name); | |
178 | |
179 union | |
180 -> "%union", white?... | |
181 | |
182 print token list | |
183 -> tag?, identifier =out << (char *) name, cs = ", "; | |
184 | |
185 -> print token list, elc, identifier =out << cs << (char *) name; | |
186 -> print token list, elc?, ',', elc?, | |
187 identifier =out << cs << (char *) name; | |
188 -> print token list, elc, | |
189 number:n =out.printf(" = %d\n",n); | |
190 | |
191 print prec list | |
192 -> tag?, identifier =config<< (char *) name, cs = ", "; | |
193 | |
194 -> print prec list, blc, identifier =config << cs << (char *) name; | |
195 -> print prec list, blc?, ',', blc?, | |
196 identifier =config << cs << (char *) name; | |
197 -> print prec list, blc, | |
198 number:n =config.printf("/* = %d*/",n); | |
199 | |
200 tag | |
201 -> '<', elc?, identifier, elc?, '>', elc? | |
202 | |
203 | |
204 /***** | |
205 | |
206 Syntax to copy C code, watching for character constants, literal strings, | |
207 comments and nested blocks, and picking up value stack references. | |
208 | |
209 *****/ | |
210 | |
211 c code block | |
212 -> "%{", c text, "%}", | |
213 white?... =out << "\n{\n" << (char *) c_code << "\n}\n",reset(c_code); | |
214 | |
215 action | |
216 -> embedded c | |
217 -> '=', white?..., embedded c | |
218 | |
219 embedded c | |
220 -> '{', c text, '}' | |
221 | |
222 c text | |
223 -> =reset(c_code), rvflag = 0; | |
224 -> c text, c char | |
225 | |
226 c char //c char represents the content of embedded c | |
227 -> simple c char:x =c_code << x; | |
228 -> '$' =c_code << '$'; | |
229 -> c code comment | |
230 -> '{', nested c text, '}' =c_code << '}'; | |
231 -> '\'', c literal character, '\'' | |
232 -> literal string, '"' =c_code << '"'; | |
233 | |
234 c code comment | |
235 -> c code comment text, "*/" =c_code << "*/"; | |
236 | |
237 c code comment text | |
238 -> "/*" =c_code << "/*"; | |
239 -> c code comment text, anything:x =c_code << x; | |
240 | |
241 (int) number | |
242 -> digit:d =d - '0'; | |
243 -> number:n, digit:d =10*n + d - '0'; | |
244 | |
245 [ sticky {number}] | |
246 | |
247 nested c text | |
248 -> =c_code << '{'; | |
249 -> nested c text, c char | |
250 | |
251 c literal character | |
252 -> 32..176 - '\\' :x =c_code << '\'' << x << '\''; | |
253 -> '\\', octal digits | |
254 | |
255 octal digits | |
256 -> '0-7':x =c_code << x; | |
257 -> octal digits, '0-7':x =c_code << x; | |
258 | |
259 literal string | |
260 -> '"' =c_code << '"'; | |
261 -> literal string, string character | |
262 | |
263 string character | |
264 -> 32..176 - '\\' -'"':x =c_code << x; | |
265 -> '\\', 32..176:x =c_code << '\\' << x; | |
266 | |
267 | |
268 /***** | |
269 | |
270 Rules Section | |
271 | |
272 *****/ | |
273 | |
274 rule section | |
275 -> [production, elc?]... | |
276 | |
277 | |
278 production | |
279 -> production head, complete rules, ';' =output_r(); | |
280 | |
281 production head | |
282 -> identifier, white?..., ':' ={ | |
283 out << (char *) name << grammar_tag; | |
284 grammar_tag = ""; | |
285 cs = "\n -> "; | |
286 dump_comments("\t",""); | |
287 } | |
288 | |
289 complete rules | |
290 -> complete rule | |
291 -> rule set continuation, complete rule | |
292 | |
293 rule set continuation | |
294 -> complete rules, '|' =output_r(), out << "\n -> ", cs = ""; | |
295 | |
296 complete rule | |
297 -> rule | |
298 -> rule, explicit precedence | |
299 | |
300 rule | |
301 -> clc? =out << cs, cs = ""; | |
302 -> rule name continuation, blc? | |
303 -> rule literal continuation, blc? | |
304 -> rule, action, blc? | |
305 | |
306 rule name continuation | |
307 -> rule, name ={ | |
308 output_il(); | |
309 out.printf("%s%s",cs,(char *) name); cs = ", "; | |
310 } | |
311 rule literal continuation | |
312 -> rule, literal:x ={ | |
313 char *fmt = "%d"; | |
314 if (isprint(x)) fmt ="'%c'"; | |
315 reset(name).printf(fmt,x); | |
316 output_il(); | |
317 out.printf("%s%s",cs,(char *) name); cs = ", "; | |
318 } | |
319 | |
320 explicit precedence | |
321 -> explicit precedence head, blc | |
322 -> explicit precedence head, blc?, action, blc? | |
323 | |
324 explicit precedence head | |
325 -> prec, identifier =out.printf(" /* %prec %s */",(char *) name); | |
326 | |
327 prec | |
328 -> {"%prec" | "%="}, white?... =dump_comments("\t",""); | |
329 | |
330 identifier | |
331 -> name | |
332 -> literal:x ={ | |
333 char *fmt = "%d"; | |
334 if (isprint(x)) fmt ="'%c'"; | |
335 reset(name).printf(fmt,x); | |
336 } | |
337 | |
338 (int) literal | |
339 -> '\'', literal character:x, '\'' =x; | |
340 -> '"', literal character:x, '"' =x; | |
341 | |
342 (int) literal character | |
343 -> 32..176 - '\\' | |
344 -> '\\', 'n' = '\n'; | |
345 -> '\\', 'r' = '\r'; | |
346 -> '\\', 'b' = '\b'; | |
347 -> '\\', 't' = '\b'; | |
348 -> '\\', 'f' = '\v'; | |
349 -> '\\', 32..176 - '0-7' - escapes :x =x&037; | |
350 -> '\\', octal number:x =x; | |
351 | |
352 name | |
353 -> letter+dot:x =reset(name) << x; | |
354 -> name, letter+digit:x =name << x; | |
355 -> name, dot =name << ' '; | |
356 | |
357 | |
358 (int) octal number | |
359 -> '0-7':x =x-'0'; | |
360 -> octal number:v, '0-7':x =8*v + x-'0'; | |
361 | |
362 tail | |
363 -> | |
364 -> mark, tail code =out << "\n}\n"; | |
365 | |
366 tail code | |
367 -> =out << "\n{\n"; | |
368 -> tail code, anything:x =out << x; | |
369 | |
370 | |
371 { | |
372 #include <ctype.h> | |
373 #include <stdio.h> | |
374 #include "charsink.h" | |
375 #include "strdict.h" | |
376 | |
377 string_accumulator c_code(64000); | |
378 string_accumulator comments(10000); | |
379 string_accumulator name(100); | |
380 string_accumulator white(100); | |
381 string_accumulator config(1000); | |
382 string_accumulator type_acc(100); | |
383 string_dictionary decl_dict(100); | |
384 | |
385 char decl_name[100] = ""; | |
386 int decl_table[100]; | |
387 | |
388 output_file out; | |
389 | |
390 char *cs; | |
391 | |
392 char *type_string = "int AG_RTV;\n"; | |
393 | |
394 | |
395 int rvflag; | |
396 | |
397 char *grammar_tag = " $"; | |
398 | |
399 void dump_comments(char *ps, char *cs) { | |
400 if (size(comments) == 0) return; | |
401 out << (char *) white << ps << (char *) comments << cs; | |
402 reset(comments); | |
403 } | |
404 | |
405 void output_r(void) { | |
406 char *rs = ""; | |
407 char *ts = ""; | |
408 if (size(c_code) == 0) return; | |
409 out.printf(" ={%s%s%s}", ts,(char *) c_code,rs); | |
410 reset(c_code); | |
411 } | |
412 | |
413 void output_il(void) { | |
414 char *rs = ""; | |
415 char *ts = ""; | |
416 if (size(c_code) == 0) return; | |
417 out.printf("%s!{\n%s%s%s}", cs, ts, (char *) c_code,rs); | |
418 reset(c_code); | |
419 cs = ",\n "; | |
420 } | |
421 | |
422 void main(void) { | |
423 y2ag(); | |
424 } | |
425 | |
426 } // End of Embedded C | |
427 |