comparison tests/agcl/parsifal/cpp.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 * 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 {
7 // class AstNode;
8
9 #include "cppdefs.h"
10 #include "agstk.h"
11 #include <stdio.h>
12 #include "mpp.h"
13
14 extern int columnNumber;
15
16 #define INPUT_CODE(t) inputCode(t)
17
18 #ifndef SYNTAX_ERROR
19 #define SYNTAX_ERROR printf("%s\n", (PCB).error_message)
20 #endif
21
22
23 parse_pcb_type *pcbPointer = 0;
24 #define PCB (*pcbPointer)
25
26 extern char *inputName[];
27 InputToken *trackPointer;
28
29 int inputCode(const InputToken &t) {
30 int code = t.code;
31 if (code == IDENTIFIER) code = getTokenType(t.handle);
32 PCB.column = t.column;
33 if (PCB.pointer == trackPointer) return code;
34 trackPointer = PCB.pointer;
35 if (code == LINE) printf("[%02d] %s(%d): %d\n", scopeStack.size(), inputName[code], code, t.handle);
36 else printf("[%02d] %s(%d): %s\n", scopeStack.size(), inputName[code], code, td[t.handle]);
37 return code;
38 }
39
40 AgStack<ParserState> stateStack;
41
42 #include "cpp.h"
43 }
44 [
45 pointer input
46 pointer type = InputToken *
47 default Input type = InputToken
48 //default token type = AstNode *
49 context type = ParserState
50 input values
51 ~declare PCB
52 ~lines and columns
53 parser name = parse
54 parser file name = "#.cpp"
55 line numbers
56 disregard line number
57 error trace
58 ~allow macros
59 sticky {
60 statement, // resolve if/else conflict
61 type specifier seq, // conflict between * as dereference and as multiply
62 ptr operator, // conflict between * as dereference and as multiply
63 class name, // qualifying :: on type and global :: on declarator
64 IDENTIFIER, // Resolves ctor/bitfield conflict
65 explicitly typed identifier,
66 nested name specifier,
67 NEW,
68 DELETE
69 }
70 enum { // See TOKEN.H
71 eof =0,
72 SPACE =' ',
73 AND ='A', // "&&"
74 ADD_EQ, // "+="
75 AND_EQ, // "&="
76 ARROW, // "->"
77 CONCAT, // "##"
78 DECR, // "--"
79 DIV_EQ, // "/="
80 ELLIPSIS, // "..."
81 EQ, // "=="
82 GE, // ">="
83 INCR, // "++"
84 LE, // "<="
85 LS, // "<<"
86 LS_EQ, // "<<="
87 MOD_EQ, // "%="
88 MULT_EQ, // "*="
89 NE, // "!="
90 OR, // "||"
91 OR_EQ, // "|="
92 RS, // ">>"
93 RS_EQ, // ">>="
94 SUB_EQ, // "-="
95 XOR_EQ, // "^="
96 CHARACTERconstant, // character constant
97 STRINGliteral, // character string
98 DOT_STAR = 'a',
99 ARROW_STAR,
100 QUAL,
101 LINE,
102 IDENTIFIER = 129,
103 CLASS_NAME,
104 ENUM_NAME,
105 TYPEDEF_NAME,
106 TEMPLATE_NAME,
107 NAMESPACE_NAME,
108 NAMESPACE_ALIAS,
109 UNSIGNEDqualifier,
110 LONGqualifier,
111 FLOATqualifier,
112 HEXconstant,
113 OCTconstant,
114 DECconstant,
115 FLOATconstant, // real
116 AUTO, // "auto"
117 BREAK, // "break"
118 CASE, // "case"
119 CHAR, // "char"
120 CONSTANT, // "const"
121 CONTINUE, // "continue"
122 DEFAULT, // "default"
123 DO, // "do"
124 DOUBLE, // "double"
125 ELSE, // "else"
126 ENUM, // "enum"
127 EXTERN, // "extern"
128 FLOAT, // "float"
129 FOR, // "for"
130 GOTO, // "goto"
131 IF, // "if"
132 INT, // "int"
133 LONG, // "long"
134 REGISTER, // "register"
135 RETURN, // "return"
136 SHORT, // "short"
137 SIGNED, // "signed"
138 SIZEOF, // "sizeof"
139 STATIC, // "static"
140 STRUCT, // "struct"
141 SWITCH, // "switch"
142 TYPEDEF, // "typedef"
143 UNION, // "union"
144 UNSIGNED, // "unsigned"
145 VOID, // "void"
146 VOLATILE, // "volatile"
147 WHILE, // "while"
148 ASM,
149 BITAND,
150 BITOR,
151 BOOL,
152 CATCH,
153 CLASS,
154 COMPL,
155 CONST_CAST,
156 DELETE,
157 DYNAMIC_CAST,
158 EXPLICIT,
159 EXPORT,
160 FALSE,
161 FRIEND,
162 INLINE,
163 MUTABLE,
164 NAMESPACE,
165 NEW,
166 NOT,
167 NOT_EQ,
168 OPERATOR,
169 PRIVATE,
170 PROTECTED,
171 PUBLIC,
172 REINTERPRET_CAST,
173 STATIC_CAST,
174 TEMPLATE,
175 THIS,
176 THROW,
177 TRUE,
178 TRY,
179 TYPEID,
180 TYPENAME,
181 USING,
182 VIRTUAL,
183 WCHAR_T,
184 CDECL,
185 PASCAL,
186 RTLENTRY,
187 NEAR,
188 FAR,
189 UNRECOGNIZED
190 }
191 ]
192
193 (void) line number
194 -> LINE:x =PCB.line = x.handle;
195 -> CDECL + PASCAL +RTLENTRY + NEAR + FAR //hack to skip non ANSI stuff
196
197 skip = LINE + CDECL + PASCAL + RTLENTRY + NEAR + FAR
198
199 translation unit $
200 -> declaration list, eof // 3.5
201
202 literal
203 -> HEXconstant
204 -> OCTconstant
205 -> DECconstant
206 -> FLOATconstant
207 -> CHARACTERconstant
208 -> STRINGliteral... //Concatenate adjacent strings
209
210 (TypeDefinition) class name
211 -> CLASS_NAME:n =TypeDefinition(n);
212 // -> TYPEDEF_NAME
213 -> template id:n =TypeDefinition(n);
214
215 // 5.1
216 primary expression
217 -> literal
218 -> THIS
219 -> QUAL, IDENTIFIER
220 -> QUAL, operator function id
221 -> qualified id
222 -> '(', expression hack 1, ')'
223 -> unqualified id
224
225 id expression
226 -> unqualified id
227 -> qualified id
228
229 unqualified id
230 -> IDENTIFIER:n ={
231 TypeDefinition d(n);
232 if (typedefFlag) {
233 d.tokenType = TYPEDEF_NAME;
234 typedefFlag = 0;
235 }
236 scopeStack.top()->addIdentifier(d);
237 }
238 -> operator function id
239 -> conversion function id
240
241 (Scope *) qualified id
242 -> nested name specifier:s, QUAL, unqualified id =s;
243 -> nested name specifier:s, QUAL, TEMPLATE, unqualified id =s;
244
245 (Scope *) nested name specifier
246 -> class name:n =n.scope;
247 -> namespace name:n =n.scope;
248 -> QUAL, class name:n = scopeStack[0]->getNestedScope(n.handle);
249 -> QUAL, namespace name:n = scopeStack[0]->getNestedScope(n.handle);
250 -> nested name specifier:s, QUAL, class name:n = (s == 0) ? 0 : s->getNestedScope(n.handle);
251 -> nested name specifier:s, QUAL, namespace name:n =(s == 0) ? 0 : s->getNestedScope(n.handle);
252
253 //5.2
254
255 postfix expression
256 -> primary expression
257 -> postfix expression, '[', expression, ']'
258 -> postfix expression, '(', expression list?, ')'
259 -> simple type specifier, '(', expression list?, ')'
260 -> postfix expression, '.', id expression
261 -> postfix expression, '.', TEMPLATE, id expression
262 -> postfix expression, ARROW, id expression
263 -> postfix expression, ARROW, TEMPLATE, id expression
264 -> postfix expression, '.', pseudo destructor name
265 -> postfix expression, ARROW, pseudo destructor name
266 -> postfix expression, INCR
267 -> postfix expression, DECR
268 -> DYNAMIC_CAST, '<', type id, '>', '(', expression, ')'
269 -> STATIC_CAST, '<', type id, '>', '(', expression, ')'
270 -> REINTERPRET_CAST, '<', type id, '>', '(', expression, ')'
271 -> CONST_CAST, '<', type id, '>', '(', expression, ')'
272 -> TYPEID, '(', expression hack 1, ')'
273 -> TYPEID, '(', type id, ')'
274
275 expression list
276 -> assignment expression
277 -> expression list, ',', assignment expression
278
279 pseudo destructor name
280 -> nonclass type name, QUAL, '~', nonclass type name
281 -> nested name specifier, QUAL, nonclass type name, QUAL, '~', nonclass type name
282 -> QUAL, nonclass type name, QUAL, '~', nonclass type name
283 -> '~', nonclass type name
284 -> nested name specifier, QUAL, '~', nonclass type name
285 -> QUAL, '~', nonclass type name
286
287 // 5.3
288
289 unary expression
290 -> postfix expression
291 -> INCR, cast expression
292 -> DECR, cast expression
293 -> unary operator, cast expression
294 -> SIZEOF, unary expression
295 -> SIZEOF, '(', type id, ')'
296 -> new expression
297 -> delete expression
298
299 unary operator
300 -> '&' | '*' | '+' | '-' | '~' | '!'
301
302 new expression
303 -> new, new type id, new initializer?
304 -> new, new placement, new type id, new initializer?
305 -> new, '(', type id, ')', new initializer?
306 -> new, new placement, '(', type id, ')', new initializer?
307
308 new
309 -> NEW
310 -> QUAL, NEW
311
312 new placement
313 -> '(', expression list hack 2, ')'
314
315 expression list hack 2
316 -> retry, expression list
317 -> retry, retry, resynch =diagnose("expression list hack 2");
318
319 new type id
320 -> type specifier seq
321 -> type specifier seq, new declarator
322
323 new declarator
324 -> ptr operator
325 -> ptr operator, new declarator
326 -> direct new declarator
327
328 direct new declarator
329 -> '[', expression, ']'
330 -> direct new declarator, '[', constant expression, ']'
331
332 new initializer
333 -> '(', expression list?, ')'
334
335 delete expression
336 -> delete, cast expression
337 -> delete, '[', ']', cast expression
338
339 delete
340 -> DELETE
341 -> QUAL, DELETE
342
343 cast expression
344 -> unary expression
345 -> '(', type id, ')', cast expression
346
347 expression hack 1
348 -> retry, expression
349 -> retry, error, resynch =diagnose("expression hack 1");
350
351 restricted assignment expression hack 5
352 -> retry, restricted assignment expression
353 -> retry, error, resynch =diagnose("expression hack 1");
354
355 pm expression
356 -> cast expression
357 -> pm expression, DOT_STAR, cast expression
358 -> pm expression, ARROW_STAR, cast expression
359
360 multiplicative expression
361 -> pm expression
362 -> multiplicative expression, '*', pm expression
363 -> multiplicative expression, '/', pm expression
364 -> multiplicative expression, '%', pm expression
365
366 additive expression
367 -> multiplicative expression
368 -> additive expression, '+', multiplicative expression
369 -> additive expression, '-', multiplicative expression
370
371 shift expression
372 -> additive expression
373 -> shift expression, LS, additive expression
374 -> shift expression, RS, additive expression
375
376 relational expression
377 -> shift expression
378 -> relational expression, '<', shift expression
379 -> relational expression, '>', shift expression
380 -> relational expression, LE, shift expression
381 -> relational expression, GE, shift expression
382
383 equality expression
384 -> relational expression
385 -> equality expression, EQ, relational expression
386 -> equality expression, NE, relational expression
387
388 and expression
389 -> equality expression
390 -> and expression, '&', equality expression
391
392 exclusive or expression
393 -> and expression
394 -> exclusive or expression, '^', and expression
395
396 inclusive or expression
397 -> exclusive or expression
398 -> inclusive or expression, '|', exclusive or expression
399
400 logical and expression
401 -> inclusive or expression
402 -> logical and expression, AND, inclusive or expression
403
404 logical or expression
405 -> logical and expression
406 -> logical or expression, OR, logical and expression
407
408 conditional expression
409 -> logical or expression
410 -> logical or expression, '?', expression, ':', conditional expression
411
412 assignment expression
413 -> conditional expression
414 -> logical or expression, assignment operator, assignment expression
415 -> throw expression
416
417 assignment operator
418 -> '=' | MULT_EQ | DIV_EQ | MOD_EQ | ADD_EQ | SUB_EQ
419 -> LS_EQ | RS_EQ | AND_EQ | OR_EQ | XOR_EQ
420
421 expression
422 -> assignment expression
423 -> expression, ',', assignment expression
424
425 constant expression
426 -> conditional expression
427
428 statement
429 -> labeled statement
430 -> compound statement
431 -> selection statement
432 -> iteration statement
433 -> jump statement
434 -> try block
435 -> block declaration
436 -> variable declaration
437 -> function definition 2
438 -> retry, statement try 2
439
440 statement try 2
441 -> type qualifier?, declarator list, ';'
442 -> function definition 1
443 -> retry, statement try 3
444
445 statement try 3
446 -> expression statement
447 -> retry, resynch =diagnose("statement try 3");
448
449 labeled statement
450 -> IDENTIFIER, ':', statement
451 -> CASE, constant expression, ':', statement
452 -> DEFAULT, ':', statement
453
454 expression statement
455 -> expression, ';'
456
457 compound statement
458 -> begin block , statement seq?, '}' =restoreScope();
459
460 begin block
461 -> '{' =nestScope();
462
463 statement seq
464 -> statement
465 -> statement seq, statement
466
467 selection statement
468 -> IF, '(', condition, ')', statement
469 -> IF, '(', condition, ')', statement, ELSE, statement
470 -> SWITCH, '(', condition, ')', statement
471
472 condition
473 -> condition kernel
474
475 condition kernel
476 -> type specifier seq, declarator, '=', assignment expression
477 -> retry, condition kernel 2
478
479 condition kernel 2
480 -> expression
481 -> retry, resynch =diagnose("condition kernel 2");
482
483
484 iteration statement
485 -> WHILE, '(', condition, ')', statement
486 -> DO, statement, WHILE, '(', condition, ')', ';'
487 -> for header, for init statement?, ';', condition?, ';', expression?, ')', statement =restoreScope();
488
489 for header
490 -> FOR, '(' =nestScope();
491
492 for init statement
493 -> for init
494
495
496 for init
497 -> for variable declaration
498 -> retry, for init 2
499
500 for variable declaration
501 -> decl specifier seq
502 -> decl specifier seq, init declarator list
503
504 for init 2
505 -> expression
506 -> retry, resynch =diagnose("for init 2");
507
508 jump statement
509 -> BREAK, ';'
510 -> CONTINUE, ';'
511 -> RETURN, expression?, ';'
512 -> GOTO, IDENTIFIER, ';'
513
514 (int) declaration seq
515 -> =0;
516 -> declaration seq, declaration hack
517
518 declaration list
519 -> declaration seq
520
521 declaration hack
522 -> declaration =printf("declaration hack complete\n");
523
524 declaration
525 -> block declaration
526 -> simple declaration
527 //-> template declaration
528 -> explicit instantiation
529 -> explicit specialization
530 -> linkage specification
531 -> namespace definition
532
533 block declaration
534 -> asm definition
535 -> namespace alias definition
536 -> using declaration
537 -> using directive
538
539 (void) retry
540 -> error =CONTEXT.restore();
541
542 simple declaration
543 -> variable declaration =printf("variable declaration\n");
544 -> template header, variable declaration =printf("variable declaration\n"), restoreScope();
545 -> EXPORT, template header, variable declaration =printf("variable declaration\n"), restoreScope();
546 -> function definition 2 =printf("function definition 2\n");
547 -> template header, function definition 2 =printf("function definition 2\n"), restoreScope();
548 -> EXPORT, template header, function definition 2 =printf("function definition 2\n"), restoreScope();
549 -> retry, attempt declarator list
550
551 attempt declarator list
552 -> type qualifier?, declarator list, ';' =printf("declarator list\n");
553 -> template header, type qualifier?, declarator list, ';' =printf("declarator list\n"), restoreScope();
554 -> function definition 1 =printf("function definition 1\n");
555 -> retry, resynch =diagnose("attempt declarator list");
556
557 resynch
558 -> resynch text, ';'
559 -> resynch text, balanced braces
560 -> resynch text, eof =PCB.pointer--;
561
562 resynch text
563 // -> resynch item
564 ->
565 -> resynch text, resynch item
566
567 resynch item
568 -> ~(eof + skip + '(' + ')' + '[' + ']' + '{' + '}' + ';')
569 -> balanced parens
570 -> balanced brackets
571
572 (void) balanced parens
573 -> '(', not right paren, ')'
574
575 (void) balanced brackets
576 -> '[', not right bracket, ']'
577
578 (void) balanced braces
579 -> '{', not right brace, '}'
580
581 not right paren
582 ->
583 -> not right paren, resynch item
584 -> not right paren, ';' + ']' + '}'
585 -> not right paren, balanced braces
586
587 not right bracket
588 ->
589 -> not right bracket, resynch item
590 -> not right bracket, ';' + ')' + '}'
591 -> not right bracket, balanced braces
592
593 not right brace
594 ->
595 -> not right brace, resynch item
596 -> not right brace, ';' + ')' + ']'
597 -> not right brace, balanced braces
598
599 variable declaration
600 -> decl specifier seq?, ';'
601 -> decl specifier seq, init declarator list, ';'
602
603 /*
604 If there are no decl specifiers, then the declarator must be a constructor,
605 destructor, or type conversion operator. In this case, there can be no
606 initialization, so we use a declarator list instead of an init declarator list
607 */
608
609 declarator list
610 -> declarator
611 -> declarator list, ',', declarator
612
613
614 decl specifier seq
615 -> fundamental type
616 -> fundamental type, predefined specifier...
617 -> type qualifier..., fundamental type, predefined specifier?...
618 -> user defined specifier, type qualifier?...
619 -> type qualifier..., user defined specifier, type qualifier?...
620
621 restricted decl specifier seq
622 -> fundamental type
623 -> fundamental type, predefined specifier...
624 -> type qualifier..., fundamental type, predefined specifier?...
625 -> restricted specifier, type qualifier?...
626 -> type qualifier..., restricted specifier, type qualifier?...
627
628 restricted specifier
629 -> qualified type name
630
631 storage class specifier
632 -> AUTO
633 -> REGISTER
634 -> STATIC
635 -> EXTERN
636 -> MUTABLE
637
638 function specifier
639 -> INLINE
640 -> VIRTUAL
641 -> EXPLICIT
642
643 simple type specifier
644 -> qualified type name
645 -> fundamental type
646
647
648 predefined specifier
649 -> type qualifier
650 -> fundamental type
651
652 type qualifier
653 -> storage class specifier
654 -> function specifier
655 -> FRIEND
656 -> TYPEDEF =typedefFlag = 1;
657 -> cv qualifier
658
659 fundamental type
660 -> CHAR
661 -> WCHAR_T
662 -> BOOL
663 -> SHORT
664 -> INT
665 -> LONG
666 -> SIGNED
667 -> UNSIGNED
668 -> FLOAT
669 -> DOUBLE
670 -> VOID
671
672 user defined specifier
673 -> class specifier
674 -> enum specifier
675 -> elaborated type specifier
676 -> qualified type name
677
678 user type specifier
679 -> elaborated type specifier
680 -> qualified type name
681
682 qualified type name
683 -> ENUM_NAME
684 -> TYPEDEF_NAME
685 -> nested name specifier
686 -> nested name specifier, QUAL, ENUM_NAME
687 -> nested name specifier, QUAL, TYPEDEF_NAME
688
689 nonclass type name
690 -> ENUM_NAME
691 -> TYPEDEF_NAME
692
693 elaborated type specifier
694 -> class key, IDENTIFIER:n ={
695 TypeDefinition d(n);
696 if (templateFlag) {
697 d.tokenType = TEMPLATE_NAME;
698 scopeStack[scopeStack.size() - 2]->addIdentifier(d);
699 }
700 else {
701 d.tokenType = CLASS_NAME;
702 scopeStack.top()->addIdentifier(d);
703 }
704 }
705 -> class key, class name:d ={
706 if (templateFlag) {
707 d.tokenType = TEMPLATE_NAME;
708 scopeStack[scopeStack.size() - 2]->addIdentifier(d);
709 }
710 else {
711 d.tokenType = CLASS_NAME;
712 scopeStack.top()->addIdentifier(d);
713 }
714 }
715 -> class key, name qualifier:scope, IDENTIFIER:n ={
716 TypeDefinition d(n);
717 d.tokenType = templateFlag ? TEMPLATE_NAME : CLASS_NAME;
718 scope->addIdentifier(d);
719 }
720 -> class key, name qualifier:scope, class name:d ={
721 d.tokenType = templateFlag ? TEMPLATE_NAME : CLASS_NAME;
722 scope->addIdentifier(d);
723 }
724 -> ENUM, IDENTIFIER
725 -> ENUM, name qualifier, IDENTIFIER
726 -> explicitly typed identifier
727 -> explicitly typed identifier, '<', template argument list, '>'
728
729 explicitly typed identifier
730 -> TYPENAME, nested name specifier, QUAL, IDENTIFIER
731
732 enum specifier
733 -> ENUM, '{', enumerator list?, '}'
734 -> ENUM, IDENTIFIER:n, '{', enumerator list?, '}' ={
735 TypeDefinition d(n);
736 d.tokenType = ENUM_NAME;
737 scopeStack.top()->addIdentifier(d);
738 }
739
740 enumerator list
741 -> enumerator definition
742 -> enumerator list, ',', enumerator definition
743
744 enumerator definition
745 -> IDENTIFIER
746 -> IDENTIFIER, '=', constant expression
747
748 (TypeDefinition) namespace name
749 -> NAMESPACE_NAME:n =TypeDefinition(n);
750 -> NAMESPACE_ALIAS:n =TypeDefinition(n);
751
752 namespace definition
753 -> named namespace definition
754 -> unnamed namespace definition
755
756 named namespace definition
757 -> original namespace definition
758 -> extension namespace definition
759
760 original namespace definition
761 -> NAMESPACE, IDENTIFIER, '{', namespace body, '}'
762
763 extension namespace definition
764 -> NAMESPACE, NAMESPACE_NAME, '{', namespace body, '}'
765
766 unnamed namespace definition
767 -> NAMESPACE, '{', namespace body, '}'
768
769 namespace body
770 -> declaration seq
771
772 namespace alias definition
773 -> NAMESPACE, IDENTIFIER, '=', qualified namespace specifier, ';'
774
775 qualified namespace specifier
776 -> name qualifier, namespace name
777
778 using declaration
779 -> USING, nested name specifier, QUAL, unqualified id, ';'
780 -> USING, TYPENAME, nested name specifier, QUAL, unqualified id, ';'
781 -> USING, QUAL, unqualified id, ';'
782
783 using directive
784 -> USING, NAMESPACE, namespace name, ';'
785 -> USING, NAMESPACE, name qualifier, namespace name, ';'
786
787 (Scope *) name qualifier
788 -> QUAL =scopeStack[0];
789 -> class name:n, QUAL =getNestedScope(n.handle);
790 -> namespace name:n, QUAL =getNestedScope(n.handle);
791 -> name qualifier:s, class name:n, QUAL =s->getNestedScope(n.handle);
792 -> name qualifier:s, namespace name:n, QUAL =(s == 0) ? 0 : s->getNestedScope(n.handle);
793
794 asm definition
795 -> ASM, '(', STRINGliteral, ')', ';'
796
797 linkage specification
798 -> EXTERN, STRINGliteral, '{', declaration list, '}'
799 -> EXTERN, STRINGliteral, declaration
800
801 init declarator list
802 -> init declarator
803 -> init declarator list, ',', init declarator
804
805 init declarator
806 -> direct declarator
807 -> direct declarator, initializer
808 -> ptr operator, init declarator
809
810 (Scope *) declarator
811 -> direct declarator:s =s;
812 -> ptr operator, declarator:s =s;
813
814 (Scope *) direct declarator
815 -> declarator id:s =s;
816 -> direct declarator:s, '(', parameter declaration clause hack 4, ')', cv qualifier seq?, exception specification? =s;
817 -> direct declarator:s, '[', constant expression?, ']' =s;
818 -> '(', declarator:s, ')' =s;
819
820 declarator hack
821 -> direct declarator hack
822 -> ptr operator, declarator hack
823
824 direct declarator hack
825 -> declarator id
826 -> direct declarator hack, '(', parameter declaration clause hack 4, ')', cv qualifier seq?, exception specification?
827 -> direct declarator hack, '[', constant expression?, ']'
828 -> '(', declarator hack 3, ')'
829
830 ptr operator
831 -> '*', cv qualifier seq?
832 -> '&'
833 -> nested name specifier, QUAL, '*', cv qualifier seq?
834
835 cv qualifier seq
836 -> cv qualifier, cv qualifier seq?
837
838 cv qualifier
839 -> CONSTANT
840 -> VOLATILE
841
842 (Scope *) declarator id
843 -> unqualified id =new Scope;
844 -> nested name specifier:s, QUAL, unqualified id =s;
845 -> nested name specifier:s, QUAL, CLASS_NAME =s;
846 -> nested name specifier:s, QUAL, '~', CLASS_NAME =s;
847 -> nested name specifier:s, QUAL, TEMPLATE_NAME =s;
848 -> nested name specifier:s, QUAL, '~', TEMPLATE_NAME =s;
849 -> CLASS_NAME:n ={
850 TypeDefinition d(n);
851 if (typedefFlag) {
852 d.tokenType = TYPEDEF_NAME;
853 typedefFlag = 0;
854 scopeStack.top()->addIdentifier(d);
855 }
856 return new Scope;
857 }
858 -> TEMPLATE_NAME:n ={
859 TypeDefinition d(n);
860 if (typedefFlag) {
861 d.tokenType = TYPEDEF_NAME;
862 typedefFlag = 0;
863 scopeStack.top()->addIdentifier(d);
864 }
865 return new Scope;
866 }
867 -> '~', CLASS_NAME:n =TypeDefinition(n).scope; // '~' is a unary operator
868 -> '~', TEMPLATE_NAME:n =TypeDefinition(n).scope; // '~' is a unary operator
869
870 type id
871 -> type specifier seq, abstract declarator?
872
873 type specifier seq
874 -> fundamental type
875 -> fundamental type, predefined specifier...
876 -> cv qualifier..., fundamental type
877 -> cv qualifier..., fundamental type, predefined specifier...
878 -> user type specifier, cv qualifier?...
879 -> cv qualifier..., user type specifier, cv qualifier?...
880
881
882 abstract declarator hack
883 -> ptr operator, abstract declarator hack?
884 -> direct abstract declarator hack
885
886 direct abstract declarator hack
887 -> '(', ')', cv qualifier seq?, exception specification?
888 -> '(', parameter declaration clause hack 3, ')', cv qualifier seq?, exception specification?
889 -> '[', constant expression?, ']'
890 -> '(', /* hack 3 */ abstract declarator, ')'
891 -> direct abstract declarator hack, '(', parameter declaration clause?, ')', cv qualifier seq?, exception specification?
892 -> direct abstract declarator hack, '[', constant expression?, ']'
893
894 abstract declarator
895 -> ptr operator, abstract declarator?
896 -> direct abstract declarator
897
898 direct abstract declarator
899 -> '(', ')', cv qualifier seq?, exception specification?
900 -> '(', parameter declaration clause, ')', cv qualifier seq?, exception specification?
901 -> '[', constant expression?, ']'
902 -> '(', abstract declarator, ')'
903 -> direct abstract declarator, '(', parameter declaration clause?, ')', cv qualifier seq?, exception specification?
904 -> direct abstract declarator, '[', constant expression?, ']'
905
906 parameter declaration clause
907 -> parameter declaration list
908 -> ELLIPSIS
909 -> parameter declaration list, ELLIPSIS
910 -> parameter declaration list, ',', ELLIPSIS
911
912 parameter declaration list
913 -> parameter declaration
914 -> parameter declaration list, ',', parameter declaration
915
916 parameter declaration
917 -> decl specifier seq, declarator hack
918 -> decl specifier seq, declarator hack, '=', assignment expression
919 -> decl specifier seq, abstract declarator hack?
920 -> decl specifier seq, abstract declarator hack?, '=', assignment expression
921
922 parameter declaration clause hack 3
923 -> parameter declaration clause
924
925 declarator hack 3
926 -> retry, declarator
927 -> retry, error, ~eof, resynch =diagnose("declarator hack 3");
928
929 parameter declaration clause hack 4
930 -> parameter declaration clause?
931
932 expression list hack 4
933 -> retry, expression list
934 -> retry, error, ~(eof + skip + ')')?... =diagnose("expression list hack 4");
935
936 function definition 1
937 -> constructor identifier, ctor initializer?, function body ={if (templateFlag) restoreScope();}
938 -> constructor identifier, constructor try block ={if (templateFlag) restoreScope();}
939
940 constructor try block
941 -> TRY, ctor initializer?, function body, handler seq
942
943 constructor identifier
944 -> type qualifier?, declarator:s =nestScope(s);
945 -> template header, type qualifier?, declarator:s =nestScope(s);
946 -> EXPORT, template header, type qualifier?, declarator:s =nestScope(s);
947
948 function definition 2
949 -> function identifier, function body
950 -> function identifier, function try block
951
952 function identifier
953 -> decl specifier seq, declarator:s =nestScope(s);
954
955 function body
956 -> '{', statement seq?, '}' =restoreScope();
957
958 function try block
959 -> TRY, function body, handler seq
960
961
962 initializer
963 -> '=', initializer clause
964 -> '(', expression list hack 4, ')'
965
966 initializer clause
967 -> assignment expression
968 -> '{', initializer list, ','?, '}'
969 -> '{', '}'
970
971 initializer list
972 -> initializer clause
973 -> initializer list, ',', initializer clause
974
975 class specifier
976 -> class head, '{', member specification?, '}' =restoreScope();
977 -> class head, ':', base clause, '{', member specification?, '}' =restoreScope();
978
979 specifier lookahead
980 -> '{'+ ':' =CONTEXT.restore();
981
982 class head
983 -> class key, specifier lookahead ={
984 nestScope();
985 }
986 -> class key, IDENTIFIER:n, specifier lookahead ={
987 TypeDefinition d(n);
988 int flag = templateFlag;
989 d.scope = nestScope(d.handle);
990 if (flag) {
991 d.tokenType = TEMPLATE_NAME;
992 scopeStack[scopeStack.size() - 3]->addIdentifier(d);
993 }
994 else {
995 d.tokenType = CLASS_NAME;
996 scopeStack[scopeStack.size() - 2]->addIdentifier(d);
997 }
998 }
999 -> class key, class name:d, specifier lookahead ={
1000 d.tokenType = templateFlag? TEMPLATE_NAME : CLASS_NAME;
1001 int flag = templateFlag;
1002 d.scope = nestScope(d.handle);
1003 if (flag) scopeStack[scopeStack.size() - 3]->addIdentifier(d);
1004 else scopeStack[scopeStack.size() - 2]->addIdentifier(d);
1005 }
1006 -> class key, TEMPLATE_NAME:n, specifier lookahead ={
1007 TypeDefinition d(n);
1008 d.tokenType = templateFlag? TEMPLATE_NAME : CLASS_NAME;
1009 int flag = templateFlag;
1010 d.scope = nestScope(d.handle);
1011 if (flag) scopeStack[scopeStack.size() - 3]->addIdentifier(d);
1012 else scopeStack[scopeStack.size() - 2]->addIdentifier(d);
1013 }
1014 -> class key, name qualifier:s, IDENTIFIER:n, specifier lookahead ={
1015 if (s != 0) s->nest(new Scope(n.handle));
1016 }
1017 -> class key, name qualifier:s, TEMPLATE, template id:h, specifier lookahead ={
1018 if (s != 0) s->nest(new Scope(h.handle));
1019 }
1020
1021 class key
1022 -> CLASS
1023 -> STRUCT
1024 -> UNION
1025
1026 member specification
1027 -> member declaration hack
1028 -> member specification, member declaration hack
1029
1030
1031 member declaration hack
1032 -> member declaration
1033 -> member restore, type qualifier?, member declarator list, ';'
1034 -> member restore, function definition 1
1035 -> member restore, error, resynch =diagnose("member specification");
1036
1037 member restore
1038 -> retry =printf("try member declarator list\n");
1039
1040 member declaration
1041 -> ';'
1042 -> decl specifier seq, member declarator list?, ';'
1043 -> function definition 2
1044 -> using declaration
1045 // -> template declaration
1046 -> access specifier, ':'
1047
1048 member declarator list
1049 -> member declarator
1050 -> member declarator list, ',', member declarator
1051
1052 member declarator
1053 -> declarator
1054 -> declarator, pure specifier
1055 -> declarator, constant intializer
1056 -> ':', constant expression
1057 -> IDENTIFIER, ':', constant expression
1058
1059 pure specifier
1060 -> '=', '0'
1061
1062 constant intializer
1063 -> '=', constant expression
1064
1065 base clause
1066 -> base specifier list
1067
1068 base specifier list
1069 -> base specifier:s =scopeStack.top()->addBase(s);
1070 -> base specifier list, ',', base specifier:s =scopeStack.top()->addBase(s);
1071
1072 (Scope *) base specifier
1073 -> CLASS_NAME:n =getNestedScope(n.handle);
1074 -> TEMPLATE_NAME:n =getNestedScope(n.handle);
1075 -> qualified class name:s =s;
1076 -> VIRTUAL, access specifier?, qualified class name:s =s;
1077 -> access specifier, VIRTUAL?, qualified class name:s =s;
1078 -> access specifier, VIRTUAL?, CLASS_NAME:n =getNestedScope(n.handle);
1079 -> access specifier, VIRTUAL?, TEMPLATE_NAME:n =getNestedScope(n.handle);
1080
1081 (Scope *) qualified class name
1082 -> name qualifier:s, class name:n =(s == 0) ? 0 : s->getNestedScope(n.handle);
1083
1084 access specifier
1085 -> PRIVATE
1086 -> PROTECTED
1087 -> PUBLIC
1088
1089 conversion function id
1090 -> OPERATOR, conversion type id
1091
1092 conversion type id
1093 -> type specifier seq, conversion declarator?
1094
1095 conversion declarator
1096 -> ptr operator, conversion declarator?
1097
1098 ctor initializer
1099 -> ':', mem initializer list
1100
1101 mem initializer list
1102 -> mem initializer
1103 -> mem initializer list, ',', mem initializer
1104
1105 mem initializer
1106 -> mem initializer id, '(', expression list?, ')'
1107
1108 mem initializer id
1109 -> qualified class name
1110 -> class name
1111 -> IDENTIFIER
1112
1113 operator function id
1114 -> OPERATOR, operator
1115
1116 operator
1117 -> NEW
1118 -> DELETE
1119 -> NEW, '[', ']'
1120 -> DELETE, '[', ']'
1121 -> '+' // =PLUS_OP;
1122 -> '-' // =MINUS_OP;
1123 -> '*' // =MUL_OP;
1124 -> '/' // =DIV_OP;
1125 -> '%' // =MOD_OP;
1126 -> '^' // =XOR_OP;
1127 -> '&' // =AND_OP;
1128 -> '|' // =OR_OP;
1129 -> '~' // =COM_OP;
1130 -> '!' // =NOT_OP;
1131 -> '=' // =ASSIGN_OP;
1132 -> '<' // =LT_OP;
1133 -> '>' // =GT_OP;
1134 -> MULT_EQ
1135 -> DIV_EQ
1136 -> MOD_EQ
1137 -> ADD_EQ
1138 -> SUB_EQ
1139 -> LS_EQ
1140 -> RS_EQ
1141 -> AND_EQ
1142 -> OR_EQ
1143 -> XOR_EQ
1144 -> LS
1145 -> RS
1146 -> EQ
1147 -> NE
1148 -> LE
1149 -> GE
1150 -> AND
1151 -> OR
1152 -> INCR
1153 -> DECR
1154 -> ','
1155 -> ARROW_STAR
1156 -> ARROW
1157 -> '(', ')' // =PAREN_OP;
1158 -> '[', ']' // =BRACKET_OP;
1159
1160 /*
1161 template declaration
1162 -> template header, declaration =restoreScope();
1163 -> EXPORT, template header, declaration =restoreScope();
1164 */
1165
1166 template header
1167 -> TEMPLATE, '<', !nestScope();, template parameter list, '>' =templateFlag = 1;
1168
1169 template parameter list
1170 -> template parameter
1171 -> template parameter list, ',', template parameter
1172
1173 template parameter
1174 -> type parameter
1175 -> template parameter declaration
1176
1177 template parameter declaration
1178 -> restricted decl specifier seq, declarator hack
1179 -> restricted decl specifier seq, declarator hack, '=', restricted assignment expression
1180 -> restricted decl specifier seq, abstract declarator hack?
1181 -> restricted decl specifier seq, abstract declarator hack?, '=', restricted assignment expression
1182
1183 restricted assignment expression
1184 -> restricted conditional expression
1185 -> shift expression, assignment operator, restricted assignment expression
1186 -> restricted throw expression
1187
1188 restricted conditional expression
1189 -> shift expression
1190 -> shift expression, '?', shift expression, ':', restricted conditional expression
1191
1192 restricted throw expression
1193 -> THROW, restricted assignment expression?
1194
1195 type parameter
1196 -> CLASS
1197 -> CLASS, IDENTIFIER:n =scopeStack.top()->addIdentifier(TypeDefinition(n.handle, CLASS_NAME));
1198 -> CLASS, '=', type id
1199 -> CLASS, IDENTIFIER, '=', type id
1200 -> TYPENAME, IDENTIFIER?
1201 -> TYPENAME, IDENTIFIER?, '=', type id
1202 -> TEMPLATE, '<', template parameter list, '>', CLASS, IDENTIFIER?
1203 -> TEMPLATE, '<', template parameter list, '>', CLASS, IDENTIFIER?, '=', TEMPLATE_NAME
1204
1205 (InputToken) template id
1206 -> TEMPLATE_NAME:n, '<', template argument list, '>' =n;
1207
1208 template argument list
1209 -> template argument
1210 -> template argument list, ',', template argument
1211
1212 template argument
1213 -> restricted assignment expression hack 5
1214 -> TEMPLATE_NAME
1215 -> type id
1216
1217 explicit instantiation
1218 -> TEMPLATE, declaration
1219
1220 explicit specialization
1221 -> TEMPLATE, '<', '>', declaration
1222
1223 try block
1224 -> TRY, compound statement, handler seq
1225
1226 handler seq
1227 -> handler
1228 -> handler seq, handler
1229
1230 handler
1231 -> CATCH, '(', exception declaration, ')', compound statement
1232
1233 exception declaration
1234 -> type specifier seq, declarator hack
1235 -> type specifier seq, abstract declarator hack
1236 -> type specifier seq
1237 -> ELLIPSIS
1238
1239 throw expression
1240 -> THROW, assignment expression?
1241
1242 exception specification
1243 -> THROW, '(', type id list?, ')'
1244
1245 type id list
1246 -> type id
1247 -> type id list, ',', type id
1248
1249 {
1250
1251 #include "mpp.h"
1252 #include "token.h"
1253
1254 #define GET_CONTEXT CONTEXT = ParserState()
1255
1256 int typedefFlag = 0;
1257 AgStack<int> typedefStack;
1258 int templateFlag = 0;
1259 AgStack<int> templateStack;
1260
1261 AgStack<InputToken> tokenStack;
1262
1263 ParserState::ParserState()
1264 : pointer(pcbPointer ? pcbPointer->pointer : 0),
1265 line(pcbPointer ? pcbPointer->line : 0),
1266 scopeStackDepth(scopeStack.size()),
1267 typedefStackDepth(typedefStack.size()),
1268 nLocalVariables(scopeStackDepth ? scopeStack.top()->nLocals(): 0),
1269 templateStackDepth(templateStack.size()),
1270 typedefFlag(::typedefFlag),
1271 templateFlag(::templateFlag)
1272 { }
1273
1274 void ParserState::restore() {
1275 PCB.pointer = pointer;
1276 PCB.line = line;
1277 while (scopeStack.size() > scopeStackDepth) scopeStack.pop();
1278 while (typedefStack.size() > typedefStackDepth) typedefStack.pop();
1279 while (templateStack.size() > templateStackDepth) templateStack.pop();
1280 scopeStack.top()->restoreLocals(nLocalVariables);
1281 ::typedefFlag = typedefFlag;
1282 ::templateFlag = templateFlag;
1283 }
1284 void diagnose(char *msg) {
1285 printf("Error: %s\n", msg);
1286 PCB.exit_flag = AG_SEMANTIC_ERROR_CODE;
1287 }
1288
1289 c_parser::c_parser() {
1290 pcbPointer = &pcb;
1291 //init_parse(); // init parse
1292 }
1293
1294
1295 // Destructor
1296
1297 c_parser::~c_parser() {
1298 }
1299
1300
1301 // Reset Parser
1302
1303 c_parser &reset(c_parser &c) {
1304 return c;
1305 }
1306
1307
1308 // Transmit token to c_parser
1309
1310 /*
1311 The overloaded operator "<<" is used to transmit data to a parser.
1312 Newline tokens are filtered out, since they are passed along by the
1313 token scanner only in case text output of the preprocessor is
1314 required.
1315
1316 If the parser has encountered an error, there is no point in giving
1317 it any further input.
1318
1319 Otherwise, the input_code and input_value fields of the pcb are set
1320 up and cc() is called to deal with the token.
1321 */
1322
1323 token_sink &c_parser::operator << (InputToken c) {
1324 printf("%d\n", c.code);
1325 if (c.code == 10) return *this;
1326 tokenStack.push(c);
1327 if (c.code == END_OF_FILE) {
1328 int k = tokenStack.size();
1329 int n = k;
1330 InputToken *tokenArray = new InputToken[k];
1331 while (k--) {
1332 tokenStack.pop(tokenArray[k]);
1333 }
1334 PCB.pointer = tokenArray;
1335 trackPointer = tokenArray - 1;
1336 PCB.line = 1;
1337 scopeStack.push(new Scope); // global scope
1338 parse();
1339 }
1340 return *this;
1341 }
1342
1343 token_sink &c_parser::operator << (InputToken *s) {
1344 while (s->code != END_OF_FILE) {
1345 if ((int) s->code == 10) continue;
1346 tokenStack.push(*s++);
1347 }
1348 return *this;
1349 }
1350
1351 }