comparison examples/mpp/jrc.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 Copyright (C) 1989,1990 James A. Roskind, All rights reserved.
4
5 This grammar was developed and written by James A. Roskind.
6 Copying of this grammar description, as a whole, is permitted
7 providing this notice is intact and applicable in all complete
8 copies. Translations as a whole to other parser generator input
9 languages (or grammar description languages) is permitted
10 provided that this notice is intact and applicable in all such
11 copies, along with a disclaimer that the contents are a
12 translation. The reproduction of derived text, such as modified
13 versions of this grammar, or the output of parser generators, is
14 permitted, provided the resulting work includes the copyright
15 notice "Portions Copyright (c) 1989, 1990 James A. Roskind".
16 Derived products, such as compilers, translators, browsers, etc.,
17 that use this grammar, must also provide the notice "Portions
18 Copyright (c) 1989, 1990 James A. Roskind" in a manner
19 appropriate to the utility, and in keeping with copyright law
20 (e.g.: EITHER displayed when first invoked/executed; OR displayed
21 continuously on display terminal; OR via placement in the object
22 code in form readable in a printout, with or near the title of
23 the work, or at the end of the file). No royalties, licenses or
24 commissions of any kind are required to copy this grammar, its
25 translations, or derivative products, when the copies are made in
26 compliance with this notice. Persons or corporations that do make
27 copies in compliance with this notice may charge whatever price
28 is agreeable to a buyer, for such copies or derivative works.
29 THIS GRAMMAR IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
30 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 PURPOSE.
33
34 James A. Roskind
35 Independent Consultant
36 516 Latania Palm Drive
37 Indialantic FL, 32903
38 (407)729-4348
39 jar@ileaf.com
40 or ...!uunet!leafusa!jar
41
42
43 ******************************************************************************
44
45 Translation to AnaGram and Interfacing
46 Copyright 1993 Parsifal Software. All rights reserved.
47
48 This software is provided 'as-is', without any express or implied
49 warranty. In no event will the authors be held liable for any damages
50 arising from the use of this software.
51
52 Permission is granted to anyone to use this software for any purpose,
53 including commercial applications, and to alter it and redistribute it
54 freely, subject to the following restrictions:
55
56 1. The origin of this software must not be misrepresented; you must not
57 claim that you wrote the original software. If you use this software
58 in a product, an acknowledgment in the product documentation would be
59 appreciated but is not required.
60 2. Altered source versions must be plainly marked as such, and must not be
61 misrepresented as being the original software.
62 3. This notice may not be removed or altered from any source distribution.
63
64 ******************************************************************************
65
66 */
67
68 #include "mpp.h"
69
70 }
71
72 [
73 grammar token = prog start
74 ~nest comments
75 sticky { statement} //Resolves if-then-else conflict
76 event driven
77 far tables // Comment out for 32 bit compilers
78 ~allow macros
79 input values
80 line numbers
81 //escape backslashes // uncomment if using MSVC++
82 error trace
83 parser name = cc
84 default input type = token
85 header file name = "jrc.h"
86 parser file name = "jrc.cpp"
87 ~test range
88 enum {
89 eof =0,
90 SPACE =' ',
91 ANDAND ='A', // "&&"
92 ANDassign, // "&="
93 ARROW, // "->"
94 CONCAT, // "##"
95 DECR, // "--"
96 DIVassign, // "/="
97 ELLIPSIS, // "..."
98 EQ, // "=="
99 ERassign, // "^="
100 GE, // ">="
101 ICR, // "++"
102 LE, // "<="
103 LS, // "<<"
104 LSassign, // "<<="
105 MODassign, // "%="
106 MINUSassign, // "-="
107 MULTassign, // "*="
108 NE, // "!="
109 ORassign, // "|="
110 OROR, // "||"
111 PLUSassign, // "+="
112 RS, // ">>"
113 RSassign, // ">>="
114 CHARACTERconstant, // character constant
115 STRINGliteral, // character string
116 HEXconstant =129,
117 OCTconstant,
118 DECconstant,
119 FLOATconstant, // real
120 NAME,
121 AUTO, // "auto"
122 BREAK, // "break"
123 CASE, // "case"
124 CHAR, // "char"
125 CONSTANT, // "const"
126 CONTINUE, // "continue"
127 DEFAULT, // "default"
128 DO, // "do"
129 DOUBLE, // "double"
130 ELSE, // "else"
131 ENUM, // "enum"
132 EXTERN, // "extern"
133 FLOAT, // "float"
134 FOR, // "for"
135 GOTO, // "goto"
136 IF, // "if"
137 INT, // "int"
138 LONG, // "long"
139 REGISTER, // "register"
140 RETURN, // "return"
141 SHORT, // "short"
142 SIGNED, // "signed"
143 SIZEOF, // "sizeof"
144 STATIC, // "static"
145 STRUCT, // "struct"
146 SWITCH, // "switch"
147 TYPEDEF, // "typedef"
148 UNION, // "union"
149 UNSIGNED, // "unsigned"
150 VOIDkey, // "void"
151 VOLATILE, // "volatile"
152 WHILE, // "while"
153 UNRECOGNIZED,
154 }
155 ]
156
157 OCTALconstant = OCTconstant
158 INTEGERconstant = DECconstant
159 FLOATINGconstant = FLOATconstant
160 CONST = CONSTANT
161 VOID = VOIDkey
162
163 prog start
164 -> translation unit?, eof
165
166 /* CONSTANTS */
167
168 constant
169 -> FLOATINGconstant
170 -> INTEGERconstant
171 -> OCTALconstant
172 -> HEXconstant
173 -> CHARACTERconstant
174
175 /* STRING LITERALS */
176
177 string literal list
178 -> STRINGliteral
179 -> string literal list, STRINGliteral
180
181 /* EXPRESSIONS */
182
183 (void) primary expression
184 -> IDENTIFIER
185 -> constant
186 -> string literal list
187 -> '(', expression, ')'
188
189 postfix expression
190 -> primary expression
191 -> postfix expression, '[', expression, ']'
192 -> postfix expression, '(', ')'
193 -> postfix expression, '(', argument expression list, ')'
194 -> postfix expression, '.', identifier or typedef name
195 -> postfix expression, ARROW, identifier or typedef name
196 -> postfix expression, ICR
197 -> postfix expression, DECR
198
199 argument expression list
200 -> assignment expression
201 -> argument expression list, ',', assignment expression
202
203 unary expression
204 -> postfix expression
205 -> ICR, unary expression
206 -> DECR, unary expression
207 -> unary operator, cast expression
208 -> SIZEOF, unary expression
209 -> SIZEOF, '(', type name, ')'
210
211 unary operator
212 -> '&'
213 -> '*'
214 -> '+'
215 -> '-'
216 -> '~'
217 -> '!'
218
219 cast expression
220 -> unary expression
221 -> '(', type name, ')', cast expression
222
223 multiplicative expression
224 -> cast expression
225 -> multiplicative expression, '*', cast expression
226 -> multiplicative expression, '/', cast expression
227 -> multiplicative expression, '%', cast expression
228
229 additive expression
230 -> multiplicative expression
231 -> additive expression, '+', multiplicative expression
232 -> additive expression, '-', multiplicative expression
233
234 shift expression
235 -> additive expression
236 -> shift expression, LS, additive expression
237 -> shift expression, RS, additive expression
238
239 relational expression
240 -> shift expression
241 -> relational expression, '<', shift expression
242 -> relational expression, '>', shift expression
243 -> relational expression, LE, shift expression
244 -> relational expression, GE, shift expression
245
246 equality expression
247 -> relational expression
248 -> equality expression, EQ, relational expression
249 -> equality expression, NE, relational expression
250
251 AND expression
252 -> equality expression
253 -> AND expression, '&', equality expression
254
255 exclusive OR expression
256 -> AND expression
257 -> exclusive OR expression, '^', AND expression
258
259 inclusive OR expression
260 -> exclusive OR expression
261 -> inclusive OR expression, '|', exclusive OR expression
262
263 logical AND expression
264 -> inclusive OR expression
265 -> logical AND expression, ANDAND, inclusive OR expression
266
267 logical OR expression
268 -> logical AND expression
269 -> logical OR expression, OROR, logical AND expression
270
271 conditional expression
272 -> logical OR expression
273 -> logical OR expression, '?', expression, ':', conditional expression
274
275 assignment expression
276 -> conditional expression
277 -> unary expression, assignment operator, assignment expression
278
279 assignment operator
280 -> '='
281 -> MULTassign
282 -> DIVassign
283 -> MODassign
284 -> PLUSassign
285 -> MINUSassign
286 -> LSassign
287 -> RSassign
288 -> ANDassign
289 -> ERassign
290 -> ORassign
291
292 expression
293 -> assignment expression
294 -> expression, ',', assignment expression
295
296 constant expression
297 -> conditional expression
298
299 /* The following was used for clarity */
300
301 expression opt
302 ->
303 -> expression
304
305 /* DECLARATIONS */
306
307 declaration
308 -> sue declaration specifier, ';'
309 -> sue type specifier, ';'
310 -> declaring list, ';'
311 -> default declaring list, ';'
312
313 default declaring list
314 -> declaration qualifier list, identifier declarator, initializer opt
315 -> type qualifier list, identifier declarator, initializer opt
316 -> default declaring list, ',', identifier declarator, initializer opt
317
318 declaring list
319 -> declaration specifier, declarator, initializer opt
320 -> type specifier, declarator, initializer opt
321 -> declaring list, ',', declarator, initializer opt
322
323 declaration specifier
324 -> basic declaration specifier
325 -> sue declaration specifier
326 -> typedef declaration specifier
327
328 type specifier
329 -> basic type specifier
330 -> sue type specifier
331 -> typedef type specifier
332
333 declaration qualifier list
334 -> storage class
335 -> type qualifier list, storage class
336 -> declaration qualifier list, declaration qualifier
337
338 type qualifier list
339 -> type qualifier
340 -> type qualifier list, type qualifier
341
342 declaration qualifier
343 -> type qualifier
344 -> storage class
345
346 type qualifier
347 -> CONST
348 -> VOLATILE
349
350 basic declaration specifier
351 -> basic type specifier, storage class
352 -> declaration qualifier list, basic type name
353 -> basic declaration specifier, declaration qualifier
354 -> basic declaration specifier, basic type name
355
356 basic type specifier
357 -> basic type name
358 -> type qualifier list, basic type name
359 -> basic type specifier, type qualifier
360 -> basic type specifier, basic type name
361
362 sue declaration specifier
363 -> sue type specifier, storage class
364 -> declaration qualifier list, elaborated type name
365 -> sue declaration specifier, declaration qualifier
366
367 sue type specifier
368 -> elaborated type name
369 -> type qualifier list, elaborated type name
370 -> sue type specifier, type qualifier
371
372 (void) typedef declaration specifier
373 -> typedef type specifier, storage class
374 -> declaration qualifier list, TYPEDEFname
375 -> typedef declaration specifier, declaration qualifier
376
377 (void) typedef type specifier
378 -> TYPEDEFname
379 -> type qualifier list, TYPEDEFname
380 -> typedef type specifier, type qualifier
381
382 storage class
383 -> TYPEDEF =typedef_flag = 1;
384 -> EXTERN
385 -> STATIC
386 -> AUTO
387 -> REGISTER
388
389 basic type name
390 -> VOID
391 -> CHAR
392 -> SHORT
393 -> INT
394 -> LONG
395 -> FLOAT
396 -> DOUBLE
397 -> SIGNED
398 -> UNSIGNED
399
400 elaborated type name
401 -> save flag:f, struct or union specifier =typedef_flag = f;
402 -> enum specifier
403
404 (int) save flag
405 -> ={int f = typedef_flag; typedef_flag = 0; return f;}
406
407 struct or union specifier
408 -> struct or union, '{', struct declaration list, '}'
409 -> struct or union, identifier or typedef name, '{', struct declaration list, '}'
410 -> struct or union, identifier or typedef name
411
412 struct or union
413 -> STRUCT
414 -> UNION
415
416 struct declaration list
417 -> struct declaration
418 -> struct declaration list, struct declaration
419
420 struct declaration
421 -> struct declaring list, ';'
422 -> struct default declaring list, ';'
423
424 struct default declaring list
425 -> type qualifier list, struct identifier declarator
426 -> struct default declaring list, ',', struct identifier declarator
427
428 struct declaring list
429 -> type specifier, struct declarator
430 -> struct declaring list, ',', struct declarator
431
432 struct declarator
433 -> declarator, bit field size opt
434 -> bit field size
435
436 struct identifier declarator
437 -> identifier declarator, bit field size opt
438 -> bit field size
439
440 bit field size opt
441 ->
442 -> bit field size
443
444 bit field size
445 -> ':', constant expression
446
447 enum specifier
448 -> ENUM, '{', enumerator list, '}'
449 -> ENUM, identifier or typedef name, '{', enumerator list, '}'
450 -> ENUM, identifier or typedef name
451
452 enumerator list
453 -> identifier or typedef name, enumerator value opt
454 -> enumerator list, ',', identifier or typedef name, enumerator value opt
455
456 enumerator value opt
457 ->
458 -> '=', constant expression
459
460 parameter type list
461 -> parameter list
462 -> parameter list, ',', ELLIPSIS
463
464 parameter list
465 -> parameter declaration
466 -> parameter list, ',', parameter declaration
467
468 parameter declaration
469 -> declaration specifier
470 -> declaration specifier, abstract declarator
471 -> declaration specifier, identifier declarator
472 -> declaration specifier, parameter typedef declarator
473 -> declaration qualifier list
474 -> declaration qualifier list, abstract declarator
475 -> declaration qualifier list, identifier declarator
476 -> type specifier
477 -> type specifier, abstract declarator
478 -> type specifier, identifier declarator
479 -> type specifier, parameter typedef declarator
480 -> type qualifier list
481 -> type qualifier list, abstract declarator
482 -> type qualifier list, identifier declarator
483
484 (void) identifier list
485 -> IDENTIFIER
486 -> identifier list, ',', IDENTIFIER
487
488 (void) identifier or typedef name
489 -> IDENTIFIER
490 -> TYPEDEFname
491
492 type name
493 -> type specifier
494 -> type specifier, abstract declarator
495 -> type qualifier list
496 -> type qualifier list, abstract declarator
497
498 initializer opt
499 ->
500 -> '=', initializer
501
502 initializer
503 -> '{', initializer list, '}'
504 -> '{', initializer list, ',', '}'
505 -> assignment expression
506
507 initializer list
508 -> initializer
509 -> initializer list, ',', initializer
510
511 /* STATEMENTS */
512
513 statement
514 -> labeled statement
515 -> compound statement
516 -> expression statement
517 -> selection statement
518 -> iteration statement
519 -> jump statement
520
521 labeled statement
522 -> identifier or typedef name, ':', statement
523 -> CASE, constant expression, ':', statement
524 -> DEFAULT, ':', statement
525
526 compound statement
527 -> '{', '}'
528 -> '{', declaration list, '}'
529 -> '{', statement list, '}'
530 -> '{', declaration list, statement list, '}'
531
532 declaration list
533 -> declaration
534 -> declaration list, declaration
535
536 statement list
537 -> statement
538 -> statement list, statement
539
540 expression statement
541 -> expression opt, ';'
542
543 selection statement
544 -> IF, '(', expression, ')', statement
545 -> IF, '(', expression, ')', statement, ELSE, statement
546 -> SWITCH, '(', expression, ')', statement
547
548 iteration statement
549 -> WHILE, '(', expression, ')', statement
550 -> DO, statement, WHILE, '(', expression, ')', ';'
551 -> FOR, '(', expression opt, ';', expression opt, ';', expression opt, ')', statement
552
553 jump statement
554 -> GOTO, identifier or typedef name, ';'
555 -> CONTINUE, ';'
556 -> BREAK, ';'
557 -> RETURN, expression opt, ';'
558
559 /* EXTERNAL DEFINITIONS */
560
561 translation unit
562 -> external definition
563 -> translation unit, external definition
564
565 external definition
566 -> function definition
567 -> declaration
568 -> identifier declarator, ';' /* Added by JTH! */
569
570 function definition
571 -> identifier declarator, compound statement
572 -> declaration specifier, identifier declarator, compound statement
573 -> type specifier, identifier declarator, compound statement
574 -> declaration qualifier list, identifier declarator, compound statement
575 -> type qualifier list, identifier declarator, compound statement
576 -> old function declarator, compound statement
577 -> declaration specifier, old function declarator, compound statement
578 -> type specifier, old function declarator, compound statement
579 -> declaration qualifier list, old function declarator, compound statement
580 -> type qualifier list, old function declarator, compound statement
581 -> old function declarator, declaration list, compound statement
582 -> declaration specifier, old function declarator, declaration list, compound statement
583 -> type specifier, old function declarator, declaration list, compound statement
584 -> declaration qualifier list, old function declarator, declaration list, compound statement
585 -> type qualifier list, old function declarator, declaration list, compound statement
586
587 declarator
588 -> typedef declarator
589 -> identifier declarator
590
591 typedef declarator
592 -> paren typedef declarator
593 -> parameter typedef declarator
594
595 parameter typedef declarator
596 -> TYPEDEFname
597 -> TYPEDEFname, postfixing abstract declarator
598 -> clean typedef declarator
599
600 clean typedef declarator
601 -> clean postfix typedef declarator
602 -> '*', parameter typedef declarator
603 -> '*', type qualifier list, parameter typedef declarator
604
605 clean postfix typedef declarator
606 -> '(', clean typedef declarator, ')'
607 -> '(', clean typedef declarator, ')', postfixing abstract declarator
608
609 paren typedef declarator
610 -> paren postfix typedef declarator
611 -> '*', '(', simple paren typedef declarator, ')'
612 -> '*', type qualifier list, '(', simple paren typedef declarator, ')'
613 -> '*', paren typedef declarator
614 -> '*', type qualifier list, paren typedef declarator
615
616 paren postfix typedef declarator
617 -> '(', paren typedef declarator, ')'
618 -> '(', simple paren typedef declarator, postfixing abstract declarator, ')'
619 -> '(', paren typedef declarator, ')', postfixing abstract declarator
620
621 simple paren typedef declarator
622 -> TYPEDEFname
623 -> '(', simple paren typedef declarator, ')'
624
625 identifier declarator
626 -> unary identifier declarator
627 -> paren identifier declarator
628
629 unary identifier declarator
630 -> postfix identifier declarator
631 -> '*', identifier declarator
632 -> '*', type qualifier list, identifier declarator
633
634 postfix identifier declarator
635 -> paren identifier declarator, postfixing abstract declarator
636 -> '(', unary identifier declarator, ')'
637 -> '(', unary identifier declarator, ')', postfixing abstract declarator
638
639 paren identifier declarator
640 -> IDENTIFIER:s =do_typedef(s);
641 -> '(', paren identifier declarator, ')'
642
643 old function declarator
644 -> postfix old function declarator
645 -> '*', old function declarator
646 -> '*', type qualifier list, old function declarator
647
648 postfix old function declarator
649 -> paren identifier declarator, '(', identifier list, ')'
650 -> '(', old function declarator, ')'
651 -> '(', old function declarator, ')', postfixing abstract declarator
652
653 abstract declarator
654 -> unary abstract declarator
655 -> postfix abstract declarator
656 -> postfixing abstract declarator
657
658 postfixing abstract declarator
659 -> array abstract declarator
660 -> '(', ')'
661 -> '(', parameter type list, ')'
662
663 array abstract declarator
664 -> '[', ']'
665 -> '[', constant expression, ']'
666 -> array abstract declarator, '[', constant expression, ']'
667
668 unary abstract declarator
669 -> '*'
670 -> '*', type qualifier list
671 -> '*', abstract declarator
672 -> '*', type qualifier list, abstract declarator
673
674 postfix abstract declarator
675 -> '(', unary abstract declarator, ')'
676 -> '(', postfix abstract declarator, ')'
677 -> '(', postfixing abstract declarator, ')'
678 -> '(', unary abstract declarator, ')', postfixing abstract declarator
679
680 (token) IDENTIFIER, TYPEDEFname
681 -> NAME:t =check_typedef(t);
682
683
684 { // Embedded C
685
686 #define SYNTAX_ERROR syntax_error(PCB.error_message)
687 #define INPUT_CODE(T) (T).id
688
689 static int typedef_flag = 0;
690 static int use_count = 0;
691
692 symbol_type_enum symbol_table[N_SYMBOLS];
693
694
695 static void do_typedef(token t) {
696 if (typedef_flag == 0) return;
697 typedef_flag = 0;
698 symbol_table[t.handle] = typedef_name;
699 }
700
701 static token check_typedef(token t) {
702 if (symbol_table[t.handle] == typedef_name)
703 CHANGE_REDUCTION(TYPEDEFname);
704 return t;
705 }
706
707 // Member Functions for Class c_parser
708
709 // Constructor
710
711 /*
712 This parser has no provisions for multiple simultaneous parses or for
713 recursion. The purpose of use_count is to make sure that there is only one
714 copy of the parser active at any time.
715 */
716
717
718 c_parser::c_parser() {
719 assert(use_count == 0);
720 use_count++;
721 typedef_flag = 0;
722 memset(symbol_table, 0, sizeof(symbol_table));
723 init_cc(); // init parse
724 }
725
726
727 // Destructor
728
729 c_parser::~c_parser() {
730 use_count--; // Makes parser available
731 }
732
733
734 // Reset Parser
735
736 c_parser &reset(c_parser &c) {
737 typedef_flag = 0;
738 memset(symbol_table, 0, sizeof(symbol_table));
739 init_cc(); // init parse
740 return c;
741 }
742
743
744 // Transmit token to c_parser
745
746 /*
747 The overloaded operator "<<" is used to transmit data to a parser.
748 Newline tokens are filtered out, since they are passed along by the
749 token scanner only in case text output of the preprocessor is
750 required.
751
752 If the parser has encountered an error, there is no point in giving
753 it any further input.
754
755 Otherwise, the input_code and input_value fields of the pcb are set
756 up and cc() is called to deal with the token.
757 */
758
759 token_sink &c_parser::operator << (token c) {
760 if (PCB.exit_flag != AG_RUNNING_CODE || (int) c.id == '\n') return *this;
761 PCB.input_code = c.id;
762 PCB.input_value = c;
763 cc();
764 return *this;
765 }
766
767 token_sink &c_parser::operator << (token *s) {
768 while (s->id != END_OF_FILE && PCB.exit_flag == AG_RUNNING_CODE) {
769 if ((int) s->id == 10) continue;
770 PCB.input_code = s->id;
771 PCB.input_value = *s++;
772 cc();
773 }
774 return *this;
775 }
776
777 } // End Embedded C
778