source: src/Parser/parser.yy @ 064e3ff

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 064e3ff was 064e3ff, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add new type for ranges and refactor parser code

  • Property mode set to 100644
File size: 104.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// cfa.y --
8//
9// Author           : Peter A. Buhr
10// Created On       : Sat Sep  1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Aug  4 16:56:28 2016
13// Update Count     : 1694
14//
15
16// This grammar is based on the ANSI99/11 C grammar, specifically parts of EXPRESSION and STATEMENTS, and on the C
17// grammar by James A. Roskind, specifically parts of DECLARATIONS and EXTERNAL DEFINITIONS.  While parts have been
18// copied, important changes have been made in all sections; these changes are sufficient to constitute a new grammar.
19// In particular, this grammar attempts to be more syntactically precise, i.e., it parses less incorrect language syntax
20// that must be subsequently rejected by semantic checks.  Nevertheless, there are still several semantic checks
21// required and many are noted in the grammar. Finally, the grammar is extended with GCC and CFA language extensions.
22
23// Acknowledgments to Richard Bilson, Glen Ditchfield, and Rodolfo Gabriel Esteves who all helped when I got stuck with
24// the grammar.
25
26// The root language for this grammar is ANSI99/11 C. All of ANSI99/11 is parsed, except for:
27//
28// 1. designation with '=' (use ':' instead)
29//
30// Most of the syntactic extensions from ANSI90 to ANSI11 C are marked with the comment "C99/C11". This grammar also has
31// two levels of extensions. The first extensions cover most of the GCC C extensions, except for:
32//
33// 1. designation with and without '=' (use ':' instead)
34// 2. attributes not allowed in parenthesis of declarator
35//
36// All of the syntactic extensions for GCC C are marked with the comment "GCC". The second extensions are for Cforall
37// (CFA), which fixes several of C's outstanding problems and extends C with many modern language concepts. All of the
38// syntactic extensions for CFA C are marked with the comment "CFA". As noted above, there is one unreconcileable
39// parsing problem between C99 and CFA with respect to designators; this is discussed in detail before the "designation"
40// grammar rule.
41
42%{
43#define YYDEBUG_LEXER_TEXT (yylval)                                             // lexer loads this up each time
44#define YYDEBUG 1                                                                               // get the pretty debugging code to compile
45extern char *yytext;
46
47#undef __GNUC_MINOR__
48
49#include <cstdio>
50#include <stack>
51#include "lex.h"
52#include "parser.h"
53#include "ParseNode.h"
54#include "TypedefTable.h"
55#include "TypeData.h"
56#include "LinkageSpec.h"
57
58DeclarationNode *theTree = 0;                                                   // the resulting parse tree
59LinkageSpec::Type linkage = LinkageSpec::Cforall;
60std::stack< LinkageSpec::Type > linkageStack;
61TypedefTable typedefTable;
62%}
63
64//************************* TERMINAL TOKENS ********************************
65
66// keywords
67%token TYPEDEF
68%token AUTO EXTERN REGISTER STATIC
69%token INLINE                                                                                   // C99
70%token FORTRAN                                                                                  // C99, extension ISO/IEC 9899:1999 Section J.5.9(1)
71%token CONST VOLATILE
72%token RESTRICT                                                                                 // C99
73%token FORALL LVALUE                                                                    // CFA
74%token VOID CHAR SHORT INT LONG FLOAT DOUBLE SIGNED UNSIGNED
75%token VALIST                                                                                   // GCC
76%token BOOL COMPLEX IMAGINARY                                                   // C99
77%token TYPEOF LABEL                                                                             // GCC
78%token ENUM STRUCT UNION
79%token OTYPE FTYPE DTYPE TRAIT                                                  // CFA
80%token SIZEOF OFFSETOF
81%token ATTRIBUTE EXTENSION                                                              // GCC
82%token IF ELSE SWITCH CASE DEFAULT DO WHILE FOR BREAK CONTINUE GOTO RETURN
83%token CHOOSE DISABLE ENABLE FALLTHRU TRY CATCH CATCHRESUME FINALLY THROW THROWRESUME AT        // CFA
84%token ASM                                                                                              // C99, extension ISO/IEC 9899:1999 Section J.5.10(1)
85%token ALIGNAS ALIGNOF ATOMIC GENERIC NORETURN STATICASSERT THREADLOCAL // C11
86
87// names and constants: lexer differentiates between identifier and typedef names
88%token<tok> IDENTIFIER                  QUOTED_IDENTIFIER               TYPEDEFname                             TYPEGENname
89%token<tok> ATTR_IDENTIFIER             ATTR_TYPEDEFname                ATTR_TYPEGENname
90%token<tok> INTEGERconstant             FLOATINGconstant                CHARACTERconstant               STRINGliteral
91%token<tok> ZERO                                ONE                                             // CFA
92
93// multi-character operators
94%token ARROW                                                                                    // ->
95%token ICR DECR                                                                                 // ++   --
96%token LS RS                                                                                    // <<   >>
97%token LE GE EQ NE                                                                              // <=   >=      ==      !=
98%token ANDAND OROR                                                                              // &&   ||
99%token ELLIPSIS                                                                                 // ...
100
101%token MULTassign       DIVassign       MODassign                               // *=   /=      %=/
102%token PLUSassign       MINUSassign                                                     // +=   -=
103%token LSassign         RSassign                                                        // <<=  >>=
104%token ANDassign        ERassign        ORassign                                // &=   ^=      |=
105
106%token ATassign                                                                                 // @=
107
108// Types declaration
109%union
110{
111        Token tok;
112        ParseNode *pn;
113        ExpressionNode *en;
114        DeclarationNode *decl;
115        DeclarationNode::Aggregate aggKey;
116        DeclarationNode::TypeClass tclass;
117        StatementNode *sn;
118        ConstantNode *constant;
119        LabelNode *label;
120        InitializerNode *in;
121        OperatorNode::Type op;
122        bool flag;
123}
124
125%type<tok> identifier  no_01_identifier  no_attr_identifier zero_one
126%type<tok> identifier_or_type_name  no_attr_identifier_or_type_name  no_01_identifier_or_type_name
127%type<constant> string_literal_list
128
129// expressions
130%type<constant> constant
131%type<en> tuple                                                 tuple_expression_list
132%type<op> ptrref_operator
133%type<en> unary_operator                                assignment_operator
134%type<en> primary_expression                    postfix_expression                      unary_expression
135%type<en> cast_expression                               multiplicative_expression       additive_expression                     shift_expression
136%type<en> relational_expression                 equality_expression                     AND_expression                          exclusive_OR_expression
137%type<en> inclusive_OR_expression               logical_AND_expression          logical_OR_expression           conditional_expression
138%type<en> constant_expression                   assignment_expression           assignment_expression_opt
139%type<en> comma_expression                              comma_expression_opt
140%type<en> argument_expression_list              argument_expression                     for_control_expression          assignment_opt
141%type<en> subrange
142%type<en> asm_operands_opt asm_operands_list asm_operand
143%type<label> label_list
144%type<constant> asm_clobbers_list_opt
145%type<flag> asm_volatile_opt
146
147// statements
148%type<sn> labeled_statement                             compound_statement                      expression_statement            selection_statement
149%type<sn> iteration_statement                   jump_statement                          exception_statement                     asm_statement
150%type<sn> fall_through_opt                              fall_through
151%type<sn> statement                                             statement_list
152%type<sn> block_item_list                               block_item
153%type<sn> case_clause
154%type<en> case_value
155%type<sn> case_value_list                               case_label                                      case_label_list
156%type<sn> switch_clause_list_opt                switch_clause_list                      choose_clause_list_opt          choose_clause_list
157%type<pn> handler_list                                  handler_clause                          finally_clause
158
159// declarations
160%type<decl> abstract_array abstract_declarator abstract_function abstract_parameter_array
161%type<decl> abstract_parameter_declaration abstract_parameter_declarator abstract_parameter_function
162%type<decl> abstract_parameter_ptr abstract_ptr
163
164%type<aggKey> aggregate_key
165%type<decl>  aggregate_name
166
167%type<decl> array_dimension array_parameter_1st_dimension array_parameter_dimension multi_array_dimension
168
169%type<decl> assertion assertion_list_opt
170
171%type<en>   bit_subrange_size_opt bit_subrange_size
172
173%type<decl> basic_declaration_specifier basic_type_name basic_type_specifier direct_type_name indirect_type_name
174
175%type<decl> trait_declaration trait_declaration_list trait_declaring_list trait_specifier
176
177%type<decl> declaration declaration_list declaration_list_opt declaration_qualifier_list
178%type<decl> declaration_specifier declarator declaring_list
179
180%type<decl> elaborated_type_name
181
182%type<decl> enumerator_list enum_name
183%type<en> enumerator_value_opt
184
185%type<decl> exception_declaration external_definition external_definition_list external_definition_list_opt
186
187%type<decl> field_declaration field_declaration_list field_declarator field_declaring_list
188%type<en> field field_list
189
190%type<decl> external_function_definition function_definition function_array function_declarator function_no_ptr function_ptr
191
192%type<decl> identifier_parameter_array identifier_parameter_declarator identifier_parameter_function
193%type<decl> identifier_parameter_ptr identifier_list
194
195%type<decl> new_abstract_array new_abstract_declarator_no_tuple new_abstract_declarator_tuple
196%type<decl> new_abstract_function new_abstract_parameter_declaration new_abstract_parameter_list
197%type<decl> new_abstract_ptr new_abstract_tuple
198
199%type<decl> new_array_parameter_1st_dimension
200
201%type<decl> new_trait_declaring_list new_declaration new_field_declaring_list
202%type<decl> new_function_declaration new_function_return new_function_specifier
203
204%type<decl> new_identifier_parameter_array new_identifier_parameter_declarator_no_tuple
205%type<decl> new_identifier_parameter_declarator_tuple new_identifier_parameter_ptr
206
207%type<decl> new_parameter_declaration new_parameter_list new_parameter_type_list new_parameter_type_list_opt
208
209%type<decl> new_typedef_declaration new_variable_declaration new_variable_specifier
210
211%type<decl> old_declaration old_declaration_list old_declaration_list_opt old_function_array
212%type<decl> old_function_declarator old_function_no_ptr old_function_ptr
213
214%type<decl> parameter_declaration parameter_list parameter_type_list
215%type<decl> parameter_type_list_opt
216
217%type<decl> paren_identifier paren_type
218
219%type<decl> storage_class storage_class_name storage_class_list
220
221%type<decl> sue_declaration_specifier sue_type_specifier
222
223%type<tclass> type_class
224%type<decl> type_declarator type_declarator_name type_declaring_list
225
226%type<decl> typedef type_array typedef_declaration typedef_declaration_specifier typedef_expression
227%type<decl> type_function type_parameter_array type_parameter_function type_parameter_ptr
228%type<decl> type_parameter_redeclarator type_ptr variable_type_redeclarator typedef_type_specifier
229%type<decl> typegen_declaration_specifier typegen_type_specifier typegen_name
230
231%type<decl> type_name type_name_no_function
232%type<decl> type_parameter type_parameter_list
233
234%type<en> type_name_list
235
236%type<decl> type_qualifier type_qualifier_name type_qualifier_list type_qualifier_list_opt type_specifier
237
238%type<decl> variable_abstract_array variable_abstract_declarator variable_abstract_function
239%type<decl> variable_abstract_ptr variable_array variable_declarator variable_function variable_ptr
240
241%type<decl> attribute_list_opt attribute_list attribute
242
243// initializers
244%type<in>  initializer initializer_list initializer_opt
245
246// designators
247%type<en>  designator designator_list designation
248
249
250// Handle single shift/reduce conflict for dangling else by shifting the ELSE token. For example, this string
251// is ambiguous:
252// .---------.                          matches IF '(' comma_expression ')' statement
253// if ( C ) S1 else S2
254// `-----------------'          matches IF '(' comma_expression ')' statement ELSE statement */
255
256%nonassoc THEN  // rule precedence for IF '(' comma_expression ')' statement
257%nonassoc ELSE  // token precedence for start of else clause in IF statement
258
259%start translation_unit                                                                 // parse-tree root
260
261%%
262//************************* Namespace Management ********************************
263
264// The grammar in the ANSI C standard is not strictly context-free, since it relies upon the distinct terminal symbols
265// "identifier" and "TYPEDEFname" that are lexically identical.  While it is possible to write a purely context-free
266// grammar, such a grammar would obscure the relationship between syntactic and semantic constructs.  Hence, this
267// grammar uses the ANSI style.
268//
269// Cforall compounds this problem by introducing type names local to the scope of a declaration (for instance, those
270// introduced through "forall" qualifiers), and by introducing "type generators" -- parametrized types.  This latter
271// type name creates a third class of identifiers that must be distinguished by the scanner.
272//
273// Since the scanner cannot distinguish among the different classes of identifiers without some context information, it
274// accesses a data structure (the TypedefTable) to allow classification of an identifier that it has just read.
275// Semantic actions during the parser update this data structure when the class of identifiers change.
276//
277// Because the Cforall language is block-scoped, there is the possibility that an identifier can change its class in a
278// local scope; it must revert to its original class at the end of the block.  Since type names can be local to a
279// particular declaration, each declaration is itself a scope.  This requires distinguishing between type names that are
280// local to the current declaration scope and those that persist past the end of the declaration (i.e., names defined in
281// "typedef" or "type" declarations).
282//
283// The non-terminals "push" and "pop" derive the empty string; their only use is to denote the opening and closing of
284// scopes.  Every push must have a matching pop, although it is regrettable the matching pairs do not always occur
285// within the same rule.  These non-terminals may appear in more contexts than strictly necessary from a semantic point
286// of view.  Unfortunately, these extra rules are necessary to prevent parsing conflicts -- the parser may not have
287// enough context and look-ahead information to decide whether a new scope is necessary, so the effect of these extra
288// rules is to open a new scope unconditionally.  As the grammar evolves, it may be neccesary to add or move around
289// "push" and "pop" nonterminals to resolve conflicts of this sort.
290
291push:
292                {
293                        typedefTable.enterScope();
294                }
295        ;
296
297pop:
298                {
299                        typedefTable.leaveScope();
300                }
301        ;
302
303//************************* CONSTANTS ********************************
304
305constant:
306                // ENUMERATIONconstant is not included here; it is treated as a variable with type "enumeration constant".
307INTEGERconstant                                                                 { $$ = makeConstantInteger( *$1 ); }
308        | FLOATINGconstant                                                      { $$ = makeConstantFloat( *$1 ); }
309        | CHARACTERconstant                                                     { $$ = makeConstantChar( *$1 ); }
310        ;
311
312identifier:
313        IDENTIFIER
314        | ATTR_IDENTIFIER                                                                       // CFA
315        | zero_one                                                                                      // CFA
316        ;
317
318no_01_identifier:
319        IDENTIFIER
320        | ATTR_IDENTIFIER                                                                       // CFA
321        ;
322
323no_attr_identifier:
324        IDENTIFIER
325        | zero_one                                                                                      // CFA
326        ;
327
328zero_one:                                                                                               // CFA
329        ZERO
330        | ONE
331        ;
332
333string_literal_list:                                                                    // juxtaposed strings are concatenated
334        STRINGliteral                                                           { $$ = makeConstantStr( *$1 ); }
335        | string_literal_list STRINGliteral                     { $$ = $1->appendstr( $2 ); }
336        ;
337
338//************************* EXPRESSIONS ********************************
339
340primary_expression:
341        IDENTIFIER                                                                                      // typedef name cannot be used as a variable name
342                { $$ = new VarRefNode( $1 ); }
343        | zero_one
344                { $$ = new VarRefNode( $1 ); }
345        | '(' comma_expression ')'
346                { $$ = $2; }
347        | '(' compound_statement ')'                                            // GCC, lambda expression
348                { $$ = new ValofExprNode( $2 ); }
349        ;
350
351postfix_expression:
352        primary_expression
353        | postfix_expression '[' push assignment_expression pop ']'
354                // CFA, comma_expression disallowed in this context because it results in a common user error: subscripting a
355                // matrix with x[i,j] instead of x[i][j]. While this change is not backwards compatible, there seems to be
356                // little advantage to this feature and many disadvantages. It is possible to write x[(i,j)] in CFA, which is
357                // equivalent to the old x[i,j].
358                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Index ), $1, $4 ); }
359        | postfix_expression '(' argument_expression_list ')'
360                { $$ = new CompositeExprNode( $1, $3 ); }
361        // ambiguity with .0 so space required after field-selection, e.g.
362                //   struct S { int 0, 1; } s; s. 0 = 0; s. 1 = 1;
363        | postfix_expression '.' no_attr_identifier
364                { $$ = new CompositeExprNode2( build_fieldSel( $1, new VarRefNode( $3 ) ) ); }
365        | postfix_expression '.' '[' push field_list pop ']' // CFA, tuple field selector
366        | postfix_expression ARROW no_attr_identifier
367                { $$ = new CompositeExprNode2( build_pfieldSel( $1, new VarRefNode( $3 ) ) ); }
368        | postfix_expression ARROW '[' push field_list pop ']' // CFA, tuple field selector
369        | postfix_expression ICR
370                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::IncrPost ), $1 ); }
371        | postfix_expression DECR
372                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::DecrPost ), $1 ); }
373        | '(' type_name_no_function ')' '{' initializer_list comma_opt '}' // C99
374                { $$ = new CompoundLiteralNode( $2, new InitializerNode( $5, true ) ); }
375        | postfix_expression '{' argument_expression_list '}' // CFA
376                {
377                        Token fn; fn.str = new std::string( "?{}" ); // location undefined
378                        $$ = new CompositeExprNode( new VarRefNode( fn ), (ExpressionNode *)( $1 )->set_link( $3 ) );
379                }
380        ;
381
382argument_expression_list:
383        argument_expression
384        | argument_expression_list ',' argument_expression
385                { $$ = (ExpressionNode *)( $1->set_link( $3 )); }
386        ;
387
388argument_expression:
389        // empty
390                { $$ = 0; }                                                                             // use default argument
391        | assignment_expression
392        | no_attr_identifier ':' assignment_expression
393                { $$ = $3->set_argName( $1 ); }
394                // Only a list of no_attr_identifier_or_type_name is allowed in this context. However, there is insufficient
395                // look ahead to distinguish between this list of parameter names and a tuple, so the tuple form must be used
396                // with an appropriate semantic check.
397        | '[' push assignment_expression pop ']' ':' assignment_expression
398                { $$ = $7->set_argName( $3 ); }
399        | '[' push assignment_expression ',' tuple_expression_list pop ']' ':' assignment_expression
400                { $$ = $9->set_argName( new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ), (ExpressionNode *)$3->set_link( flattenCommas( $5 )))); }
401        ;
402
403field_list:                                                                                             // CFA, tuple field selector
404        field
405        | field_list ',' field                                          { $$ = (ExpressionNode *)$1->set_link( $3 ); }
406        ;
407
408field:                                                                                                  // CFA, tuple field selector
409        no_attr_identifier
410                { $$ = new VarRefNode( $1 ); }
411        // ambiguity with .0 so space required after field-selection, e.g.
412                //   struct S { int 0, 1; } s; s. 0 = 0; s. 1 = 1;
413        | no_attr_identifier '.' field
414                { $$ = new CompositeExprNode2( build_fieldSel( $3, new VarRefNode( $1 ) ) ); }
415        | no_attr_identifier '.' '[' push field_list pop ']'
416                { $$ = new CompositeExprNode2( build_fieldSel( $5, new VarRefNode( $1 ) ) ); }
417        | no_attr_identifier ARROW field
418                { $$ = new CompositeExprNode2( build_pfieldSel( $3, new VarRefNode( $1 ) ) ); }
419        | no_attr_identifier ARROW '[' push field_list pop ']'
420                { $$ = new CompositeExprNode2( build_pfieldSel( $5, new VarRefNode( $1 ) ) ); }
421        ;
422
423unary_expression:
424        postfix_expression
425                // first location where constant/string can have operator applied: sizeof 3/sizeof "abc" still requires
426                // semantics checks, e.g., ++3, 3--, *3, &&3
427        | constant
428                { $$ = $1; }
429        | string_literal_list
430                { $$ = $1; }
431        | EXTENSION cast_expression                                                     // GCC
432                { $$ = $2->set_extension( true ); }
433        | ptrref_operator cast_expression                                       // CFA
434        { $$ = $1 == OperatorNode::AddressOf ? (ExpressionNode*) new CompositeExprNode2( build_addressOf( $2 ) ) : (ExpressionNode*)new CompositeExprNode( new OperatorNode ( $1 ), $2 ); }
435                // '*' ('&') is separated from unary_operator because of shift/reduce conflict in:
436                //              { * X; }         // dereference X
437                //              { * int X; } // CFA declaration of pointer to int
438        | unary_operator cast_expression
439                { $$ = new CompositeExprNode( $1, $2 ); }
440        | ICR unary_expression
441                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Incr ), $2 ); }
442        | DECR unary_expression
443                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Decr ), $2 ); }
444        | SIZEOF unary_expression
445                { $$ = new CompositeExprNode2( build_sizeOf( $2 ) ); }
446        | SIZEOF '(' type_name_no_function ')'
447                { $$ = new CompositeExprNode2( build_sizeOf( new TypeValueNode( $3 ) ) ); }
448        | OFFSETOF '(' type_name_no_function ',' no_attr_identifier ')'
449                { $$ = new CompositeExprNode2( build_offsetOf( new TypeValueNode( $3 ), new VarRefNode( $5 ) ) ); }
450        | ATTR_IDENTIFIER
451                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Attr ), new VarRefNode( $1 )); }
452        | ATTR_IDENTIFIER '(' type_name ')'
453                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Attr ), new VarRefNode( $1 ), new TypeValueNode( $3 )); }
454        | ATTR_IDENTIFIER '(' argument_expression ')'
455                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Attr ), new VarRefNode( $1 ), $3 ); }
456        | ALIGNOF unary_expression                                                      // GCC, variable alignment
457                { $$ = new CompositeExprNode2( build_alignOf( $2 ) ); }
458        | ALIGNOF '(' type_name_no_function ')'                         // GCC, type alignment
459                { $$ = new CompositeExprNode2( build_alignOf( new TypeValueNode( $3 ) ) ); }
460//      | ANDAND IDENTIFIER                                                                     // GCC, address of label
461//              { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::LabelAddress ), new VarRefNode( $2, true ) ); }
462        ;
463
464ptrref_operator:
465        '*'                                                                                     { $$ = OperatorNode::PointTo; }
466        | '&'                                                                           { $$ = OperatorNode::AddressOf; }
467                // GCC, address of label must be handled by semantic check for ref,ref,label
468        | ANDAND                                                                        { $$ = OperatorNode::And; }
469        ;
470
471unary_operator:
472        '+'                                                                                     { $$ = new OperatorNode( OperatorNode::UnPlus ); }
473        | '-'                                                                           { $$ = new OperatorNode( OperatorNode::UnMinus ); }
474        | '!'                                                                           { $$ = new OperatorNode( OperatorNode::Neg ); }
475        | '~'                                                                           { $$ = new OperatorNode( OperatorNode::BitNeg ); }
476        ;
477
478cast_expression:
479        unary_expression
480        | '(' type_name_no_function ')' cast_expression
481                { $$ = new CompositeExprNode2( build_cast( new TypeValueNode( $2 ), $4 ) ); }
482        | '(' type_name_no_function ')' tuple
483                { $$ = new CompositeExprNode2( build_cast( new TypeValueNode( $2 ), $4 ) ); }
484        ;
485
486multiplicative_expression:
487        cast_expression
488        | multiplicative_expression '*' cast_expression
489                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Mul ), $1, $3 ); }
490        | multiplicative_expression '/' cast_expression
491                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Div ), $1, $3 ); }
492        | multiplicative_expression '%' cast_expression
493                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Mod ), $1, $3 ); }
494        ;
495
496additive_expression:
497        multiplicative_expression
498        | additive_expression '+' multiplicative_expression
499                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Plus ), $1, $3 ); }
500        | additive_expression '-' multiplicative_expression
501                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Minus ), $1, $3 ); }
502        ;
503
504shift_expression:
505        additive_expression
506        | shift_expression LS additive_expression
507                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::LShift ), $1, $3 ); }
508        | shift_expression RS additive_expression
509                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::RShift ), $1, $3 ); }
510        ;
511
512relational_expression:
513        shift_expression
514        | relational_expression '<' shift_expression
515                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::LThan ), $1, $3 ); }
516        | relational_expression '>' shift_expression
517                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::GThan ), $1, $3 ); }
518        | relational_expression LE shift_expression
519                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::LEThan ), $1, $3 ); }
520        | relational_expression GE shift_expression
521                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::GEThan ), $1, $3 ); }
522        ;
523
524equality_expression:
525        relational_expression
526        | equality_expression EQ relational_expression
527                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Eq ), $1, $3 ); }
528        | equality_expression NE relational_expression
529                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Neq ), $1, $3 ); }
530        ;
531
532AND_expression:
533        equality_expression
534        | AND_expression '&' equality_expression
535                { $$ =new CompositeExprNode( new OperatorNode( OperatorNode::BitAnd ), $1, $3 ); }
536        ;
537
538exclusive_OR_expression:
539        AND_expression
540        | exclusive_OR_expression '^' AND_expression
541                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Xor ), $1, $3 ); }
542        ;
543
544inclusive_OR_expression:
545        exclusive_OR_expression
546        | inclusive_OR_expression '|' exclusive_OR_expression
547                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::BitOr ), $1, $3 ); }
548        ;
549
550logical_AND_expression:
551        inclusive_OR_expression
552        | logical_AND_expression ANDAND inclusive_OR_expression
553                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::And ), $1, $3 ); }
554        ;
555
556logical_OR_expression:
557        logical_AND_expression
558        | logical_OR_expression OROR logical_AND_expression
559                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Or ), $1, $3 ); }
560        ;
561
562conditional_expression:
563        logical_OR_expression
564        | logical_OR_expression '?' comma_expression ':' conditional_expression
565                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Cond ), (ExpressionNode *)mkList( (*$1, *$3, *$5 ) ) ); }
566        | logical_OR_expression '?' /* empty */ ':' conditional_expression // GCC, omitted first operand
567                { $$=new CompositeExprNode( new OperatorNode( OperatorNode::NCond ), $1, $4 ); }
568        | logical_OR_expression '?' comma_expression ':' tuple // CFA, tuple expression
569                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Cond ), (ExpressionNode *)mkList( (*$1, *$3, *$5 ) ) ); }
570        ;
571
572constant_expression:
573        conditional_expression
574        ;
575
576assignment_expression:
577                // CFA, assignment is separated from assignment_operator to ensure no assignment operations for tuples
578        conditional_expression
579        | unary_expression '=' assignment_expression
580                { $$ =new CompositeExprNode( new OperatorNode( OperatorNode::Assign ), $1, $3 ); }
581        | unary_expression assignment_operator assignment_expression
582                { $$ =new CompositeExprNode( $2, $1, $3 ); }
583        | tuple assignment_opt                                                          // CFA, tuple expression
584                { $$ = ( $2 == 0 ) ? $1 : new CompositeExprNode( new OperatorNode( OperatorNode::Assign ), $1, $2 ); }
585        ;
586
587assignment_expression_opt:
588        // empty
589                { $$ = new NullExprNode; }
590        | assignment_expression
591        ;
592
593tuple:                                                                                                  // CFA, tuple
594                // CFA, one assignment_expression is factored out of comma_expression to eliminate a shift/reduce conflict with
595                // comma_expression in new_identifier_parameter_array and new_abstract_array
596        '[' ']'
597                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ) ); }
598        | '[' push assignment_expression pop ']'
599                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ), $3 ); }
600        | '[' push ',' tuple_expression_list pop ']'
601                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ), (ExpressionNode *)(new NullExprNode)->set_link( $4 ) ); }
602        | '[' push assignment_expression ',' tuple_expression_list pop ']'
603                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::TupleC ), (ExpressionNode *)$3->set_link( flattenCommas( $5 ) ) ); }
604        ;
605
606tuple_expression_list:
607        assignment_expression_opt
608        | tuple_expression_list ',' assignment_expression_opt
609                { $$ = (ExpressionNode *)$1->set_link( $3 ); }
610        ;
611
612assignment_operator:
613        MULTassign                                                                      { $$ = new OperatorNode( OperatorNode::MulAssn ); }
614        | DIVassign                                                                     { $$ = new OperatorNode( OperatorNode::DivAssn ); }
615        | MODassign                                                                     { $$ = new OperatorNode( OperatorNode::ModAssn ); }
616        | PLUSassign                                                            { $$ = new OperatorNode( OperatorNode::PlusAssn ); }
617        | MINUSassign                                                           { $$ = new OperatorNode( OperatorNode::MinusAssn ); }
618        | LSassign                                                                      { $$ = new OperatorNode( OperatorNode::LSAssn ); }
619        | RSassign                                                                      { $$ = new OperatorNode( OperatorNode::RSAssn ); }
620        | ANDassign                                                                     { $$ = new OperatorNode( OperatorNode::AndAssn ); }
621        | ERassign                                                                      { $$ = new OperatorNode( OperatorNode::ERAssn ); }
622        | ORassign                                                                      { $$ = new OperatorNode( OperatorNode::OrAssn ); }
623        ;
624
625comma_expression:
626        assignment_expression
627        | comma_expression ',' assignment_expression    // { $$ = (ExpressionNode *)$1->add_to_list( $3 ); }
628                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Comma ), $1, $3 ); }
629        ;
630
631comma_expression_opt:
632        // empty
633                { $$ = 0; }
634        | comma_expression
635        ;
636
637//*************************** STATEMENTS *******************************
638
639statement:
640        labeled_statement
641        | compound_statement
642        | expression_statement                                          { $$ = $1; }
643        | selection_statement
644        | iteration_statement
645        | jump_statement
646        | exception_statement
647        | asm_statement
648        | '^' postfix_expression '{' argument_expression_list '}' ';' // CFA
649                {
650                        Token fn; fn.str = new std::string( "^?{}" ); // location undefined
651                        $$ = new StatementNode( StatementNode::Exp, new CompositeExprNode( new VarRefNode( fn ),
652                                (ExpressionNode *)( $2 )->set_link( $4 ) ), 0 );
653                }
654        ;
655
656labeled_statement:
657                // labels cannot be identifiers 0 or 1
658        IDENTIFIER ':' attribute_list_opt statement
659                {
660                        $$ = $4->add_label( $1 );
661                }
662        ;
663
664compound_statement:
665        '{' '}'
666                { $$ = new CompoundStmtNode( (StatementNode *)0 ); }
667        | '{'
668                // Two scopes are necessary because the block itself has a scope, but every declaration within the block also
669                // requires its own scope
670          push push
671          local_label_declaration_opt                                           // GCC, local labels
672          block_item_list pop '}'                                                       // C99, intermix declarations and statements
673                { $$ = new CompoundStmtNode( $5 ); }
674        ;
675
676block_item_list:                                                                                // C99
677        block_item
678        | block_item_list push block_item
679                { if ( $1 != 0 ) { $1->set_link( $3 ); $$ = $1; } }
680        ;
681
682block_item:
683        declaration                                                                                     // CFA, new & old style declarations
684                { $$ = new StatementNode( $1 ); }
685        | EXTENSION declaration                                                         // GCC
686                {       // mark all fields in list
687                        for ( DeclarationNode *iter = $2; iter != NULL; iter = (DeclarationNode *)iter->get_link() )
688                                iter->set_extension( true );
689                        $$ = new StatementNode( $2 );
690                }
691        | function_definition
692                { $$ = new StatementNode( $1 ); }
693        | statement pop
694        ;
695
696statement_list:
697        statement
698        | statement_list statement
699                { if ( $1 != 0 ) { $1->set_link( $2 ); $$ = $1; } }
700        ;
701
702expression_statement:
703        comma_expression_opt ';'
704                { $$ = new StatementNode( StatementNode::Exp, $1, 0 ); }
705        ;
706
707selection_statement:
708        IF '(' comma_expression ')' statement                           %prec THEN
709                // explicitly deal with the shift/reduce conflict on if/else
710                { $$ = new StatementNode( StatementNode::If, $3, $5 ); }
711        | IF '(' comma_expression ')' statement ELSE statement
712                { $$ = new StatementNode( StatementNode::If, $3, (StatementNode *)mkList((*$5, *$7 )) ); }
713        | SWITCH '(' comma_expression ')' case_clause           // CFA
714                { $$ = new StatementNode( StatementNode::Switch, $3, $5 ); }
715        | SWITCH '(' comma_expression ')' '{' push declaration_list_opt switch_clause_list_opt '}' // CFA
716                {
717                        StatementNode *sw = new StatementNode( StatementNode::Switch, $3, $8 );
718                        // The semantics of the declaration list is changed to include associated initialization, which is performed
719                        // *before* the transfer to the appropriate case clause by hoisting the declarations into a compound
720                        // statement around the switch.  Statements after the initial declaration list can never be executed, and
721                        // therefore, are removed from the grammar even though C allows it. The change also applies to choose
722                        // statement.
723                        $$ = $7 != 0 ? new CompoundStmtNode( (StatementNode *)((new StatementNode( $7 ))->set_link( sw )) ) : sw;
724                }
725        | CHOOSE '(' comma_expression ')' case_clause           // CFA
726                { $$ = new StatementNode( StatementNode::Switch, $3, $5 ); }
727        | CHOOSE '(' comma_expression ')' '{' push declaration_list_opt choose_clause_list_opt '}' // CFA
728                {
729                        StatementNode *sw = new StatementNode( StatementNode::Switch, $3, $8 );
730                        $$ = $7 != 0 ? new CompoundStmtNode( (StatementNode *)((new StatementNode( $7 ))->set_link( sw )) ) : sw;
731                }
732        ;
733
734// CASE and DEFAULT clauses are only allowed in the SWITCH statement, precluding Duff's device. In addition, a case
735// clause allows a list of values and subranges.
736
737case_value:                                                                                             // CFA
738        constant_expression                                                     { $$ = $1; }
739        | constant_expression ELLIPSIS constant_expression      // GCC, subrange
740                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Range ), $1, $3 ); }
741        | subrange                                                                                      // CFA, subrange
742        ;
743
744case_value_list:                                                                                // CFA
745        case_value                                                                      { $$ = new StatementNode( StatementNode::Case, $1, 0 ); }
746                // convert case list, e.g., "case 1, 3, 5:" into "case 1: case 3: case 5"
747        | case_value_list ',' case_value                        { $$ = (StatementNode *)($1->set_link( new StatementNode( StatementNode::Case, $3, 0 ) ) ); }
748        ;
749
750case_label:                                                                                             // CFA
751        CASE case_value_list ':'                                        { $$ = $2; }
752        | DEFAULT ':'                                                           { $$ = new StatementNode( StatementNode::Default ); }
753                // A semantic check is required to ensure only one default clause per switch/choose statement.
754        ;
755
756case_label_list:                                                                                // CFA
757        case_label
758        | case_label_list case_label                            { $$ = (StatementNode *)( $1->set_link( $2 )); }
759        ;
760
761case_clause:                                                                                    // CFA
762        case_label_list statement                                       { $$ = $1->append_last_case( new CompoundStmtNode( $2 ) ); }
763        ;
764
765switch_clause_list_opt:                                                                 // CFA
766        // empty
767                { $$ = 0; }
768        | switch_clause_list
769        ;
770
771switch_clause_list:                                                                             // CFA
772        case_label_list statement_list
773                { $$ = $1->append_last_case( new CompoundStmtNode( $2 ) ); }
774        | switch_clause_list case_label_list statement_list
775                { $$ = (StatementNode *)( $1->set_link( $2->append_last_case( new CompoundStmtNode( $3 ) ) ) ); }
776        ;
777
778choose_clause_list_opt:                                                                 // CFA
779        // empty
780                { $$ = 0; }
781        | choose_clause_list
782        ;
783
784choose_clause_list:                                                                             // CFA
785        case_label_list fall_through
786                { $$ = $1->append_last_case( $2 ); }
787        | case_label_list statement_list fall_through_opt
788                { $$ = $1->append_last_case( new CompoundStmtNode( (StatementNode *)mkList( (*$2, *$3 ) ) ) ); }
789        | choose_clause_list case_label_list fall_through
790                { $$ = (StatementNode *)( $1->set_link( $2->append_last_case( $3 ))); }
791        | choose_clause_list case_label_list statement_list fall_through_opt
792                { $$ = (StatementNode *)( $1->set_link( $2->append_last_case( new CompoundStmtNode( (StatementNode *)mkList( (*$3, *$4 ) ) ) ) ) ); }
793        ;
794
795fall_through_opt:                                                                               // CFA
796        // empty
797                { $$ = new StatementNode( StatementNode::Break ); }     // insert implicit break
798        | fall_through
799        ;
800
801fall_through:                                                                                   // CFA
802        FALLTHRU
803                { $$ = 0; }
804        | FALLTHRU ';'
805                { $$ = 0; }
806        ;
807
808iteration_statement:
809        WHILE '(' comma_expression ')' statement
810                { $$ = new StatementNode( StatementNode::While, $3, $5 ); }
811        | DO statement WHILE '(' comma_expression ')' ';'
812                { $$ = new StatementNode( StatementNode::Do, $5, $2 ); }
813        | FOR '(' push for_control_expression ')' statement
814                { $$ = new StatementNode( StatementNode::For, $4, $6 ); }
815        ;
816
817for_control_expression:
818        comma_expression_opt pop ';' comma_expression_opt ';' comma_expression_opt
819                { $$ = new ForCtlExprNode( $1, $4, $6 ); }
820        | declaration comma_expression_opt ';' comma_expression_opt // C99
821                { $$ = new ForCtlExprNode( $1, $2, $4 ); }
822        ;
823
824jump_statement:
825        GOTO IDENTIFIER ';'
826                { $$ = new StatementNode( StatementNode::Goto, $2 ); }
827        | GOTO '*' comma_expression ';'                                         // GCC, computed goto
828                // The syntax for the GCC computed goto violates normal expression precedence, e.g., goto *i+3; => goto *(i+3);
829                // whereas normal operator precedence yields goto (*i)+3;
830                { $$ = new StatementNode( StatementNode::Goto, $3 ); }
831        | CONTINUE ';'
832                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
833                { $$ = new StatementNode( StatementNode::Continue ); }
834        | CONTINUE IDENTIFIER ';'                                                       // CFA, multi-level continue
835                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
836                // the target of the transfer appears only at the start of an iteration statement.
837                { $$ = new StatementNode( StatementNode::Continue, $2 ); }
838        | BREAK ';'
839                // A semantic check is required to ensure this statement appears only in the body of an iteration statement.
840                { $$ = new StatementNode( StatementNode::Break ); }
841        | BREAK IDENTIFIER ';'                                                          // CFA, multi-level exit
842                // A semantic check is required to ensure this statement appears only in the body of an iteration statement, and
843                // the target of the transfer appears only at the start of an iteration statement.
844                { $$ = new StatementNode( StatementNode::Break, $2 ); }
845        | RETURN comma_expression_opt ';'
846                { $$ = new StatementNode( StatementNode::Return, $2, 0 ); }
847        | THROW assignment_expression_opt ';'
848                { $$ = new StatementNode( StatementNode::Throw, $2, 0 ); }
849//      | THROW ';'
850//              { $$ = new StatementNode( StatementNode::Throw ); }
851        | THROWRESUME assignment_expression_opt ';'
852                { $$ = new StatementNode( StatementNode::Throw, $2, 0 ); }
853        | THROWRESUME assignment_expression_opt AT assignment_expression ';'
854                { $$ = new StatementNode( StatementNode::Throw, $2, 0 ); }
855//      | THROWRESUME ';'
856//              { $$ = new StatementNode( StatementNode::Throw ); }
857        ;
858
859exception_statement:
860        TRY compound_statement handler_list
861                { $$ = new StatementNode( StatementNode::Try, 0,(StatementNode *)(mkList((*$2,*$3 )))); }
862        | TRY compound_statement finally_clause
863                { $$ = new StatementNode( StatementNode::Try, 0,(StatementNode *)(mkList((*$2,*$3 )))); }
864        | TRY compound_statement handler_list finally_clause
865                {
866                        $3->set_link( $4 );
867                        $$ = new StatementNode( StatementNode::Try, 0,(StatementNode *)(mkList((*$2,*$3 ))));
868                }
869        ;
870
871handler_list:
872                // There must be at least one catch clause
873        handler_clause
874                // ISO/IEC 9899:1999 Section 15.3(6 ) If present, a "..." handler shall be the last handler for its try block.
875        | CATCH '(' ELLIPSIS ')' compound_statement
876                { $$ = StatementNode::newCatchStmt( 0, $5, true ); }
877        | handler_clause CATCH '(' ELLIPSIS ')' compound_statement
878                { $$ = $1->set_link( StatementNode::newCatchStmt( 0, $6, true ) ); }
879        | CATCHRESUME '(' ELLIPSIS ')' compound_statement
880                { $$ = StatementNode::newCatchStmt( 0, $5, true ); }
881        | handler_clause CATCHRESUME '(' ELLIPSIS ')' compound_statement
882                { $$ = $1->set_link( StatementNode::newCatchStmt( 0, $6, true ) ); }
883        ;
884
885handler_clause:
886        CATCH '(' push push exception_declaration pop ')' compound_statement pop
887                { $$ = StatementNode::newCatchStmt( $5, $8 ); }
888        | handler_clause CATCH '(' push push exception_declaration pop ')' compound_statement pop
889                { $$ = $1->set_link( StatementNode::newCatchStmt( $6, $9 ) ); }
890        | CATCHRESUME '(' push push exception_declaration pop ')' compound_statement pop
891                { $$ = StatementNode::newCatchStmt( $5, $8 ); }
892        | handler_clause CATCHRESUME '(' push push exception_declaration pop ')' compound_statement pop
893                { $$ = $1->set_link( StatementNode::newCatchStmt( $6, $9 ) ); }
894        ;
895
896finally_clause:
897        FINALLY compound_statement
898                {
899                        $$ = new StatementNode( StatementNode::Finally, 0, $2 );
900                        std::cout << "Just created a finally node" << std::endl;
901                }
902        ;
903
904exception_declaration:
905                // A semantic check is required to ensure type_specifier does not create a new type, e.g.:
906                //
907                //              catch ( struct { int i; } x ) ...
908                //
909                // This new type cannot catch any thrown type because of name equivalence among types.
910        type_specifier
911        | type_specifier declarator
912                {
913                        typedefTable.addToEnclosingScope( TypedefTable::ID );
914                        $$ = $2->addType( $1 );
915                }
916        | type_specifier variable_abstract_declarator
917                { $$ = $2->addType( $1 ); }
918        | new_abstract_declarator_tuple no_attr_identifier      // CFA
919                {
920                        typedefTable.addToEnclosingScope( TypedefTable::ID );
921                        $$ = $1->addName( $2 );
922                }
923        | new_abstract_declarator_tuple                                         // CFA
924        ;
925
926asm_statement:
927        ASM asm_volatile_opt '(' string_literal_list ')' ';'
928                { $$ = new AsmStmtNode( StatementNode::Asm, $2, $4, 0 ); }
929        | ASM asm_volatile_opt '(' string_literal_list ':' asm_operands_opt ')' ';' // remaining GCC
930                { $$ = new AsmStmtNode( StatementNode::Asm, $2, $4, $6 ); }
931        | ASM asm_volatile_opt '(' string_literal_list ':' asm_operands_opt ':' asm_operands_opt ')' ';'
932                { $$ = new AsmStmtNode( StatementNode::Asm, $2, $4, $6, $8 ); }
933        | ASM asm_volatile_opt '(' string_literal_list ':' asm_operands_opt ':' asm_operands_opt ':' asm_clobbers_list_opt ')' ';'
934                { $$ = new AsmStmtNode( StatementNode::Asm, $2, $4, $6, $8, $10 ); }
935        | ASM asm_volatile_opt GOTO '(' string_literal_list ':' ':' asm_operands_opt ':' asm_clobbers_list_opt ':' label_list ')' ';'
936        { $$ = new AsmStmtNode( StatementNode::Asm, $2, $5, 0, $8, $10, $12 ); }
937        ;
938
939asm_volatile_opt:                                                                               // GCC
940        // empty
941                { $$ = false; }
942        | VOLATILE
943                { $$ = true; }
944        ;
945
946asm_operands_opt:                                                                               // GCC
947        // empty
948                { $$ = 0; }                                                                             // use default argument
949        | asm_operands_list
950        ;
951
952asm_operands_list:                                                                              // GCC
953        asm_operand
954        | asm_operands_list ',' asm_operand
955                { $$ = (ExpressionNode *)$1->set_link( $3 ); }
956        ;
957
958asm_operand:                                                                                    // GCC
959        string_literal_list '(' constant_expression ')'
960                { $$ = new AsmExprNode( 0, $1, $3 ); }
961        | '[' constant_expression ']' string_literal_list '(' constant_expression ')'
962                { $$ = new AsmExprNode( $2, $4, $6 ); }
963        ;
964
965asm_clobbers_list_opt:                                                                  // GCC
966        // empty
967                { $$ = 0; }                                                                             // use default argument
968        | string_literal_list
969                { $$ = $1; }
970        | asm_clobbers_list_opt ',' string_literal_list
971                { $$ = (ConstantNode *)$1->set_link( $3 ); }
972        ;
973
974label_list:
975        no_attr_identifier
976                { $$ = new LabelNode(); $$->append_label( $1 ); }
977        | label_list ',' no_attr_identifier
978                { $$ = $1; $1->append_label( $3 ); }
979        ;
980
981//******************************* DECLARATIONS *********************************
982
983declaration_list_opt:                                                                   // used at beginning of switch statement
984        pop
985                { $$ = 0; }
986        | declaration_list
987        ;
988
989declaration_list:
990        declaration
991        | declaration_list push declaration
992                { $$ = $1->appendList( $3 ); }
993        ;
994
995old_declaration_list_opt:                                                               // used to declare parameter types in K&R style functions
996        pop
997                { $$ = 0; }
998        | old_declaration_list
999        ;
1000
1001old_declaration_list:
1002        old_declaration
1003        | old_declaration_list push old_declaration
1004                { $$ = $1->appendList( $3 ); }
1005        ;
1006
1007local_label_declaration_opt:                                                    // GCC, local label
1008        // empty
1009        | local_label_declaration_list
1010        ;
1011
1012local_label_declaration_list:                                                   // GCC, local label
1013        LABEL local_label_list ';'
1014        | local_label_declaration_list LABEL local_label_list ';'
1015        ;
1016
1017local_label_list:                                                                               // GCC, local label
1018        no_attr_identifier_or_type_name                         {}
1019        | local_label_list ',' no_attr_identifier_or_type_name {}
1020        ;
1021
1022declaration:                                                                                    // CFA, new & old style declarations
1023        new_declaration
1024        | old_declaration
1025        ;
1026
1027// C declaration syntax is notoriously confusing and error prone. Cforall provides its own type, variable and function
1028// declarations. CFA declarations use the same declaration tokens as in C; however, CFA places declaration modifiers to
1029// the left of the base type, while C declarations place modifiers to the right of the base type. CFA declaration
1030// modifiers are interpreted from left to right and the entire type specification is distributed across all variables in
1031// the declaration list (as in Pascal).  ANSI C and the new CFA declarations may appear together in the same program
1032// block, but cannot be mixed within a specific declaration.
1033//
1034//                      CFA                                     C
1035//              [10] int x;                     int x[10];              // array of 10 integers
1036//              [10] * char y;          char *y[10];    // array of 10 pointers to char
1037
1038new_declaration:                                                                                // CFA
1039        new_variable_declaration pop ';'
1040        | new_typedef_declaration pop ';'
1041        | new_function_declaration pop ';'
1042        | type_declaring_list pop ';'
1043        | trait_specifier pop ';'
1044        ;
1045
1046new_variable_declaration:                                                               // CFA
1047        new_variable_specifier initializer_opt
1048                {
1049                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1050                        $$ = $1->addInitializer( $2 );
1051                }
1052        | declaration_qualifier_list new_variable_specifier initializer_opt
1053                // declaration_qualifier_list also includes type_qualifier_list, so a semantic check is necessary to preclude
1054                // them as a type_qualifier cannot appear in that context.
1055                {
1056                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1057                        $$ = $2->addQualifiers( $1 )->addInitializer( $3 );;
1058                }
1059        | new_variable_declaration pop ',' push identifier_or_type_name initializer_opt
1060                {
1061                        typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
1062                        $$ = $1->appendList( $1->cloneType( $5 )->addInitializer( $6 ) );
1063                }
1064        ;
1065
1066new_variable_specifier:                                                                 // CFA
1067                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1068                // storage-class
1069        new_abstract_declarator_no_tuple identifier_or_type_name asm_name_opt
1070                {
1071                        typedefTable.setNextIdentifier( *$2 );
1072                        $$ = $1->addName( $2 );
1073                }
1074        | new_abstract_tuple identifier_or_type_name asm_name_opt
1075                {
1076                        typedefTable.setNextIdentifier( *$2 );
1077                        $$ = $1->addName( $2 );
1078                }
1079        | type_qualifier_list new_abstract_tuple identifier_or_type_name asm_name_opt
1080                {
1081                        typedefTable.setNextIdentifier( *$3 );
1082                        $$ = $2->addQualifiers( $1 )->addName( $3 );
1083                }
1084        ;
1085
1086new_function_declaration:                                                               // CFA
1087        new_function_specifier
1088                {
1089                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1090                        $$ = $1;
1091                }
1092        | type_qualifier_list new_function_specifier
1093                {
1094                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1095                        $$ = $2->addQualifiers( $1 );
1096                }
1097        | declaration_qualifier_list new_function_specifier
1098                {
1099                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1100                        $$ = $2->addQualifiers( $1 );
1101                }
1102        | declaration_qualifier_list type_qualifier_list new_function_specifier
1103                {
1104                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1105                        $$ = $3->addQualifiers( $1 )->addQualifiers( $2 );
1106                }
1107        | new_function_declaration pop ',' push identifier_or_type_name
1108                {
1109                        typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
1110                        $$ = $1->appendList( $1->cloneType( $5 ) );
1111                }
1112        ;
1113
1114new_function_specifier:                                                                 // CFA
1115        '[' ']' identifier_or_type_name '(' push new_parameter_type_list_opt pop ')' // S/R conflict
1116                {
1117                        $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
1118                }
1119//      '[' ']' identifier '(' push new_parameter_type_list_opt pop ')'
1120//              {
1121//                      typedefTable.setNextIdentifier( *$5 );
1122//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1123//              }
1124//      | '[' ']' TYPEDEFname '(' push new_parameter_type_list_opt pop ')'
1125//              {
1126//                      typedefTable.setNextIdentifier( *$5 );
1127//                      $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1128//              }
1129//      | '[' ']' typegen_name
1130                // identifier_or_type_name must be broken apart because of the sequence:
1131                //
1132                //   '[' ']' identifier_or_type_name '(' new_parameter_type_list_opt ')'
1133                //   '[' ']' type_specifier
1134                //
1135                // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1136                // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
1137        | new_abstract_tuple identifier_or_type_name '(' push new_parameter_type_list_opt pop ')'
1138                // To obtain LR(1 ), this rule must be factored out from function return type (see new_abstract_declarator).
1139                {
1140                        $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1141                }
1142        | new_function_return identifier_or_type_name '(' push new_parameter_type_list_opt pop ')'
1143                {
1144                        $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1145                }
1146        ;
1147
1148new_function_return:                                                                    // CFA
1149        '[' push new_parameter_list pop ']'
1150                { $$ = DeclarationNode::newTuple( $3 ); }
1151        | '[' push new_parameter_list pop ',' push new_abstract_parameter_list pop ']'
1152                // To obtain LR(1 ), the last new_abstract_parameter_list is added into this flattened rule to lookahead to the
1153                // ']'.
1154                { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1155        ;
1156
1157new_typedef_declaration:                                                                // CFA
1158        TYPEDEF new_variable_specifier
1159                {
1160                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1161                        $$ = $2->addTypedef();
1162                }
1163        | TYPEDEF new_function_specifier
1164                {
1165                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1166                        $$ = $2->addTypedef();
1167                }
1168        | new_typedef_declaration pop ',' push no_attr_identifier
1169                {
1170                        typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
1171                        $$ = $1->appendList( $1->cloneType( $5 ) );
1172                }
1173        ;
1174
1175// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1176// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
1177
1178typedef_declaration:
1179        TYPEDEF type_specifier declarator
1180                {
1181                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1182                        $$ = $3->addType( $2 )->addTypedef();
1183                }
1184        | typedef_declaration pop ',' push declarator
1185                {
1186                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1187                        $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1188                }
1189        | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
1190                {
1191                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1192                        $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1193                }
1194        | type_specifier TYPEDEF declarator
1195                {
1196                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1197                        $$ = $3->addType( $1 )->addTypedef();
1198                }
1199        | type_specifier TYPEDEF type_qualifier_list declarator
1200                {
1201                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1202                        $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
1203                }
1204        ;
1205
1206typedef_expression:
1207                // GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
1208        TYPEDEF no_attr_identifier '=' assignment_expression
1209                {
1210                        typedefTable.addToEnclosingScope( *$2, TypedefTable::TD );
1211                        $$ = DeclarationNode::newName( 0 ); // XXX
1212                }
1213        | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
1214                {
1215                        typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
1216                        $$ = DeclarationNode::newName( 0 ); // XXX
1217                }
1218        ;
1219
1220old_declaration:
1221        declaring_list pop ';'
1222        | typedef_declaration pop ';'
1223        | typedef_expression pop ';'                                            // GCC, naming expression type
1224        | sue_declaration_specifier pop ';'
1225        ;
1226
1227declaring_list:
1228                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1229                // storage-class
1230        declaration_specifier declarator asm_name_opt initializer_opt
1231                {
1232                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1233                        $$ = ( $2->addType( $1 ))->addInitializer( $4 );
1234                }
1235        | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1236                {
1237                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1238                        $$ = $1->appendList( $1->cloneBaseType( $4->addInitializer( $6 ) ) );
1239                }
1240        ;
1241
1242declaration_specifier:                                                                  // type specifier + storage class
1243        basic_declaration_specifier
1244        | sue_declaration_specifier
1245        | typedef_declaration_specifier
1246        | typegen_declaration_specifier
1247        ;
1248
1249type_specifier:                                                                                 // declaration specifier - storage class
1250        basic_type_specifier
1251        | sue_type_specifier
1252        | typedef_type_specifier
1253        | typegen_type_specifier
1254        ;
1255
1256type_qualifier_list_opt:                                                                // GCC, used in asm_statement
1257        // empty
1258                { $$ = 0; }
1259        | type_qualifier_list
1260        ;
1261
1262type_qualifier_list:
1263                // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
1264                //
1265                // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1266                // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1267                // appeared only once.
1268        type_qualifier
1269        | type_qualifier_list type_qualifier
1270                { $$ = $1->addQualifiers( $2 ); }
1271        ;
1272
1273type_qualifier:
1274        type_qualifier_name
1275        | attribute
1276        //{ $$ = DeclarationNode::newQualifier( DeclarationNode::Attribute ); }
1277        ;
1278
1279type_qualifier_name:
1280        CONST
1281                { $$ = DeclarationNode::newQualifier( DeclarationNode::Const ); }
1282        | RESTRICT
1283                { $$ = DeclarationNode::newQualifier( DeclarationNode::Restrict ); }
1284        | VOLATILE
1285                { $$ = DeclarationNode::newQualifier( DeclarationNode::Volatile ); }
1286        | LVALUE                                                                                        // CFA
1287                { $$ = DeclarationNode::newQualifier( DeclarationNode::Lvalue ); }
1288        | ATOMIC
1289                { $$ = DeclarationNode::newQualifier( DeclarationNode::Atomic ); }
1290        | FORALL '('
1291                {
1292                        typedefTable.enterScope();
1293                }
1294          type_parameter_list ')'                                                       // CFA
1295                {
1296                        typedefTable.leaveScope();
1297                        $$ = DeclarationNode::newForall( $4 );
1298                }
1299        ;
1300
1301declaration_qualifier_list:
1302        storage_class_list
1303        | type_qualifier_list storage_class_list                        // remaining OBSOLESCENT (see 2 )
1304                { $$ = $1->addQualifiers( $2 ); }
1305        | declaration_qualifier_list type_qualifier_list storage_class_list
1306                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1307        ;
1308
1309storage_class_list:
1310                // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1311                // only one of each is specified, except for inline, which can appear with the others.
1312                //
1313                // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1314                // specifiers in a declaration.
1315        storage_class
1316        | storage_class_list storage_class
1317                { $$ = $1->addQualifiers( $2 ); }
1318        ;
1319
1320storage_class:
1321        storage_class_name
1322        ;
1323
1324storage_class_name:
1325        EXTERN
1326                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Extern ); }
1327        | STATIC
1328                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Static ); }
1329        | AUTO
1330                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Auto ); }
1331        | REGISTER
1332                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Register ); }
1333        | INLINE                                                                                        // C99
1334                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Inline ); }
1335        | FORTRAN                                                                                       // C99
1336                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Fortran ); }
1337        | NORETURN                                                                                      // C11
1338                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Noreturn ); }
1339        | THREADLOCAL                                                                           // C11
1340                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Threadlocal ); }
1341        ;
1342
1343basic_type_name:
1344        CHAR
1345                { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
1346        | DOUBLE
1347                { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
1348        | FLOAT
1349                { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
1350        | INT
1351                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
1352        | LONG
1353                { $$ = DeclarationNode::newModifier( DeclarationNode::Long ); }
1354        | SHORT
1355                { $$ = DeclarationNode::newModifier( DeclarationNode::Short ); }
1356        | SIGNED
1357                { $$ = DeclarationNode::newModifier( DeclarationNode::Signed ); }
1358        | UNSIGNED
1359                { $$ = DeclarationNode::newModifier( DeclarationNode::Unsigned ); }
1360        | VOID
1361                { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1362        | BOOL                                                                                          // C99
1363                { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
1364        | COMPLEX                                                                                       // C99
1365                { $$ = DeclarationNode::newBasicType( DeclarationNode::Complex ); }
1366        | IMAGINARY                                                                                     // C99
1367                { $$ = DeclarationNode::newBasicType( DeclarationNode::Imaginary ); }
1368        | VALIST                                                                                        // GCC, __builtin_va_list
1369                { $$ = DeclarationNode::newBuiltinType( DeclarationNode::Valist ); }
1370        ;
1371
1372basic_declaration_specifier:
1373                // A semantic check is necessary for conflicting storage classes.
1374        basic_type_specifier
1375        | declaration_qualifier_list basic_type_specifier
1376                { $$ = $2->addQualifiers( $1 ); }
1377        | basic_declaration_specifier storage_class                     // remaining OBSOLESCENT (see 2)
1378                { $$ = $1->addQualifiers( $2 ); }
1379        | basic_declaration_specifier storage_class type_qualifier_list
1380                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1381        | basic_declaration_specifier storage_class basic_type_specifier
1382                { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
1383        ;
1384
1385basic_type_specifier:
1386        direct_type_name
1387        | type_qualifier_list_opt indirect_type_name type_qualifier_list_opt
1388                { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
1389        ;
1390
1391direct_type_name:
1392                // A semantic check is necessary for conflicting type qualifiers.
1393        basic_type_name
1394        | type_qualifier_list basic_type_name
1395                { $$ = $2->addQualifiers( $1 ); }
1396        | direct_type_name type_qualifier
1397                { $$ = $1->addQualifiers( $2 ); }
1398        | direct_type_name basic_type_name
1399                { $$ = $1->addType( $2 ); }
1400        ;
1401
1402indirect_type_name:
1403        TYPEOF '(' type_name ')'                                                        // GCC: typeof(x) y;
1404                { $$ = $3; }
1405        | TYPEOF '(' comma_expression ')'                                       // GCC: typeof(a+b) y;
1406                { $$ = DeclarationNode::newTypeof( $3 ); }
1407        | ATTR_TYPEGENname '(' type_name ')'                            // CFA: e.g., @type(x) y;
1408                { $$ = DeclarationNode::newAttr( $1, $3 ); }
1409        | ATTR_TYPEGENname '(' comma_expression ')'                     // CFA: e.g., @type(a+b) y;
1410                { $$ = DeclarationNode::newAttr( $1, $3 ); }
1411        ;
1412
1413sue_declaration_specifier:
1414        sue_type_specifier
1415        | declaration_qualifier_list sue_type_specifier
1416                { $$ = $2->addQualifiers( $1 ); }
1417        | sue_declaration_specifier storage_class                       // remaining OBSOLESCENT (see 2)
1418                { $$ = $1->addQualifiers( $2 ); }
1419        | sue_declaration_specifier storage_class type_qualifier_list
1420                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1421        ;
1422
1423sue_type_specifier:
1424        elaborated_type_name                                                            // struct, union, enum
1425        | type_qualifier_list elaborated_type_name
1426                { $$ = $2->addQualifiers( $1 ); }
1427        | sue_type_specifier type_qualifier
1428                { $$ = $1->addQualifiers( $2 ); }
1429        ;
1430
1431typedef_declaration_specifier:
1432        typedef_type_specifier
1433        | declaration_qualifier_list typedef_type_specifier
1434                { $$ = $2->addQualifiers( $1 ); }
1435        | typedef_declaration_specifier storage_class           // remaining OBSOLESCENT (see 2)
1436                { $$ = $1->addQualifiers( $2 ); }
1437        | typedef_declaration_specifier storage_class type_qualifier_list
1438                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1439        ;
1440
1441typedef_type_specifier:                                                                 // typedef types
1442        TYPEDEFname
1443                { $$ = DeclarationNode::newFromTypedef( $1 ); }
1444        | type_qualifier_list TYPEDEFname
1445                { $$ = DeclarationNode::newFromTypedef( $2 )->addQualifiers( $1 ); }
1446        | typedef_type_specifier type_qualifier
1447                { $$ = $1->addQualifiers( $2 ); }
1448        ;
1449
1450elaborated_type_name:
1451        aggregate_name
1452        | enum_name
1453        ;
1454
1455aggregate_name:
1456        aggregate_key '{' field_declaration_list '}'
1457                { $$ = DeclarationNode::newAggregate( $1, 0, 0, $3, true ); }
1458        | aggregate_key no_attr_identifier_or_type_name
1459                {
1460                        typedefTable.makeTypedef( *$2 );
1461                        $$ = DeclarationNode::newAggregate( $1, $2, 0, 0, false );
1462                }
1463        | aggregate_key no_attr_identifier_or_type_name
1464                { typedefTable.makeTypedef( *$2 ); }
1465                '{' field_declaration_list '}'
1466                { $$ = DeclarationNode::newAggregate( $1, $2, 0, $5, true ); }
1467        | aggregate_key '(' type_name_list ')' '{' field_declaration_list '}' // CFA
1468                { $$ = DeclarationNode::newAggregate( $1, 0, $3, $6, false ); }
1469        | aggregate_key typegen_name                                            // CFA, S/R conflict
1470                { $$ = $2; }
1471        ;
1472
1473aggregate_key:
1474        STRUCT attribute_list_opt
1475                { $$ = DeclarationNode::Struct; }
1476        | UNION attribute_list_opt
1477                { $$ = DeclarationNode::Union; }
1478        ;
1479
1480field_declaration_list:
1481        // empty
1482                { $$ = 0; }
1483        | field_declaration_list field_declaration
1484                { $$ = $1 != 0 ? $1->appendList( $2 ) : $2; }
1485        ;
1486
1487field_declaration:
1488        new_field_declaring_list ';'                                            // CFA, new style field declaration
1489        | EXTENSION new_field_declaring_list ';'                        // GCC
1490                { $$ = $2->set_extension( true ); }
1491        | field_declaring_list ';'
1492        | EXTENSION field_declaring_list ';'                            // GCC
1493                {       // mark all fields in list
1494                        for ( DeclarationNode *iter = $2; iter != NULL; iter = (DeclarationNode *)iter->get_link() )
1495                                iter->set_extension( true );
1496                        $$ = $2;
1497                }
1498        ;
1499
1500new_field_declaring_list:                                                               // CFA, new style field declaration
1501        new_abstract_declarator_tuple                                           // CFA, no field name
1502        | new_abstract_declarator_tuple no_attr_identifier_or_type_name
1503                { $$ = $1->addName( $2 ); }
1504        | new_field_declaring_list ',' no_attr_identifier_or_type_name
1505                { $$ = $1->appendList( $1->cloneType( $3 ) ); }
1506        | new_field_declaring_list ','                                          // CFA, no field name
1507                { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1508        ;
1509
1510field_declaring_list:
1511        type_specifier field_declarator
1512                { $$ = $2->addType( $1 ); }
1513        | field_declaring_list ',' attribute_list_opt field_declarator
1514                { $$ = $1->appendList( $1->cloneBaseType( $4 ) ); }
1515        ;
1516
1517field_declarator:
1518        // empty
1519                { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
1520        | bit_subrange_size                                                                     // no field name
1521                { $$ = DeclarationNode::newBitfield( $1 ); }
1522        | variable_declarator bit_subrange_size_opt
1523                // A semantic check is required to ensure bit_subrange only appears on base type int.
1524                { $$ = $1->addBitfield( $2 ); }
1525        | variable_type_redeclarator bit_subrange_size_opt
1526                // A semantic check is required to ensure bit_subrange only appears on base type int.
1527                { $$ = $1->addBitfield( $2 ); }
1528        | variable_abstract_declarator                                          // CFA, no field name
1529        ;
1530
1531bit_subrange_size_opt:
1532        // empty
1533                { $$ = 0; }
1534        | bit_subrange_size
1535                { $$ = $1; }
1536        ;
1537
1538bit_subrange_size:
1539        ':' constant_expression
1540                { $$ = $2; }
1541        ;
1542
1543enum_key:
1544        ENUM attribute_list_opt
1545        ;
1546
1547enum_name:
1548        enum_key '{' enumerator_list comma_opt '}'
1549                { $$ = DeclarationNode::newEnum( 0, $3 ); }
1550        | enum_key no_attr_identifier_or_type_name
1551                {
1552                        typedefTable.makeTypedef( *$2 );
1553                        $$ = DeclarationNode::newEnum( $2, 0 );
1554                }
1555        | enum_key no_attr_identifier_or_type_name
1556                { typedefTable.makeTypedef( *$2 ); }
1557                '{' enumerator_list comma_opt '}'
1558                { $$ = DeclarationNode::newEnum( $2, $5 ); }
1559        ;
1560
1561enumerator_list:
1562        no_attr_identifier_or_type_name enumerator_value_opt
1563                { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
1564        | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
1565                { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1566        ;
1567
1568enumerator_value_opt:
1569        // empty
1570                { $$ = 0; }
1571        | '=' constant_expression
1572                { $$ = $2; }
1573        ;
1574
1575// Minimum of one parameter after which ellipsis is allowed only at the end.
1576
1577new_parameter_type_list_opt:                                                    // CFA
1578        // empty
1579                { $$ = 0; }
1580        | new_parameter_type_list
1581        ;
1582
1583new_parameter_type_list:                                                                // CFA, abstract + real
1584        new_abstract_parameter_list
1585        | new_parameter_list
1586        | new_parameter_list pop ',' push new_abstract_parameter_list
1587                { $$ = $1->appendList( $5 ); }
1588        | new_abstract_parameter_list pop ',' push ELLIPSIS
1589                { $$ = $1->addVarArgs(); }
1590        | new_parameter_list pop ',' push ELLIPSIS
1591                { $$ = $1->addVarArgs(); }
1592        ;
1593
1594new_parameter_list:                                                                             // CFA
1595                // To obtain LR(1) between new_parameter_list and new_abstract_tuple, the last new_abstract_parameter_list is
1596                // factored out from new_parameter_list, flattening the rules to get lookahead to the ']'.
1597        new_parameter_declaration
1598        | new_abstract_parameter_list pop ',' push new_parameter_declaration
1599                { $$ = $1->appendList( $5 ); }
1600        | new_parameter_list pop ',' push new_parameter_declaration
1601                { $$ = $1->appendList( $5 ); }
1602        | new_parameter_list pop ',' push new_abstract_parameter_list pop ',' push new_parameter_declaration
1603                { $$ = $1->appendList( $5 )->appendList( $9 ); }
1604        ;
1605
1606new_abstract_parameter_list:                                                    // CFA, new & old style abstract
1607        new_abstract_parameter_declaration
1608        | new_abstract_parameter_list pop ',' push new_abstract_parameter_declaration
1609                { $$ = $1->appendList( $5 ); }
1610        ;
1611
1612parameter_type_list_opt:
1613        // empty
1614                { $$ = 0; }
1615        | parameter_type_list
1616        ;
1617
1618parameter_type_list:
1619        parameter_list
1620        | parameter_list pop ',' push ELLIPSIS
1621                { $$ = $1->addVarArgs(); }
1622        ;
1623
1624parameter_list:                                                                                 // abstract + real
1625        abstract_parameter_declaration
1626        | parameter_declaration
1627        | parameter_list pop ',' push abstract_parameter_declaration
1628                { $$ = $1->appendList( $5 ); }
1629        | parameter_list pop ',' push parameter_declaration
1630                { $$ = $1->appendList( $5 ); }
1631        ;
1632
1633// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
1634// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
1635
1636new_parameter_declaration:                                                              // CFA, new & old style parameter declaration
1637        parameter_declaration
1638        | new_identifier_parameter_declarator_no_tuple identifier_or_type_name assignment_opt
1639                { $$ = $1->addName( $2 ); }
1640        | new_abstract_tuple identifier_or_type_name assignment_opt
1641                // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1642                { $$ = $1->addName( $2 ); }
1643        | type_qualifier_list new_abstract_tuple identifier_or_type_name assignment_opt
1644                { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
1645        | new_function_specifier
1646        ;
1647
1648new_abstract_parameter_declaration:                                             // CFA, new & old style parameter declaration
1649        abstract_parameter_declaration
1650        | new_identifier_parameter_declarator_no_tuple
1651        | new_abstract_tuple
1652                // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1653        | type_qualifier_list new_abstract_tuple
1654                { $$ = $2->addQualifiers( $1 ); }
1655        | new_abstract_function
1656        ;
1657
1658parameter_declaration:
1659        declaration_specifier identifier_parameter_declarator assignment_opt
1660                {
1661                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1662                        $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
1663                }
1664        | declaration_specifier type_parameter_redeclarator assignment_opt
1665                {
1666                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1667                        $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
1668                }
1669        ;
1670
1671abstract_parameter_declaration:
1672        declaration_specifier
1673        | declaration_specifier abstract_parameter_declarator
1674                { $$ = $2->addType( $1 ); }
1675        ;
1676
1677// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
1678// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
1679// identifiers.  The ANSI-style parameter-list can redefine a typedef name.
1680
1681identifier_list:                                                                                // K&R-style parameter list => no types
1682        no_attr_identifier
1683                { $$ = DeclarationNode::newName( $1 ); }
1684        | identifier_list ',' no_attr_identifier
1685                { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
1686        ;
1687
1688identifier_or_type_name:
1689        identifier
1690        | TYPEDEFname
1691        | TYPEGENname
1692        ;
1693
1694no_01_identifier_or_type_name:
1695        no_01_identifier
1696        | TYPEDEFname
1697        | TYPEGENname
1698        ;
1699
1700no_attr_identifier_or_type_name:
1701        no_attr_identifier
1702        | TYPEDEFname
1703        | TYPEGENname
1704        ;
1705
1706type_name_no_function:                                                                  // sizeof, alignof, cast (constructor)
1707        new_abstract_declarator_tuple                                           // CFA
1708        | type_specifier
1709        | type_specifier variable_abstract_declarator
1710                { $$ = $2->addType( $1 ); }
1711        ;
1712
1713type_name:                                                                                              // typeof, assertion
1714        new_abstract_declarator_tuple                                           // CFA
1715        | new_abstract_function                                                         // CFA
1716        | type_specifier
1717        | type_specifier abstract_declarator
1718                { $$ = $2->addType( $1 ); }
1719        ;
1720
1721initializer_opt:
1722        // empty
1723                { $$ = 0; }
1724        | '=' initializer
1725                { $$ = $2; }
1726        | ATassign initializer
1727                { $$ = $2->set_maybeConstructed( false ); }
1728        ;
1729
1730initializer:
1731        assignment_expression                                           { $$ = new InitializerNode( $1 ); }
1732        | '{' initializer_list comma_opt '}'            { $$ = new InitializerNode( $2, true ); }
1733        ;
1734
1735initializer_list:
1736        // empty
1737                { $$ = 0; }
1738        | initializer
1739        | designation initializer                                       { $$ = $2->set_designators( $1 ); }
1740        | initializer_list ',' initializer                      { $$ = (InitializerNode *)( $1->set_link( $3 ) ); }
1741        | initializer_list ',' designation initializer
1742                { $$ = (InitializerNode *)( $1->set_link( $4->set_designators( $3 ) ) ); }
1743        ;
1744
1745// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
1746// '=' to separator the designator from the initializer value, as in:
1747//
1748//              int x[10] = { [1] = 3 };
1749//
1750// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment.  To disambiguate this case, CFA
1751// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
1752// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
1753// shift/reduce conflicts
1754
1755designation:
1756        designator_list ':'                                                                     // C99, CFA uses ":" instead of "="
1757        | no_attr_identifier_or_type_name ':'                           // GCC, field name
1758                { $$ = new VarRefNode( $1 ); }
1759        ;
1760
1761designator_list:                                                                                // C99
1762        designator
1763        | designator_list designator
1764                { $$ = (ExpressionNode *)( $1->set_link( $2 )); }
1765        //| designator_list designator                                          { $$ = new CompositeExprNode( $1, $2 ); }
1766        ;
1767
1768designator:
1769                // lexer ambiguity: designator ".0" is floating-point constant or designator for name 0 only ".0" and ".1"
1770                // allowed => semantic check
1771        FLOATINGconstant
1772                { $$ = new DesignatorNode( new VarRefNode( $1 ) ); }
1773        | '.' no_attr_identifier_or_type_name                           // C99, field name
1774                { $$ = new DesignatorNode( new VarRefNode( $2 ) ); }
1775        | '[' push assignment_expression pop ']'                        // C99, single array element
1776                // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
1777                { $$ = new DesignatorNode( $3, true ); }
1778        | '[' push subrange pop ']'                                                     // CFA, multiple array elements
1779                { $$ = new DesignatorNode( $3, true ); }
1780        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
1781                { $$ = new DesignatorNode( new CompositeExprNode( new OperatorNode( OperatorNode::Range ), $3, $5 ), true ); }
1782        | '.' '[' push field_list pop ']'                                       // CFA, tuple field selector
1783                { $$ = new DesignatorNode( $4 ); }
1784        ;
1785
1786// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
1787// rather than an object-oriented type system. This required four groups of extensions:
1788//
1789// Overloading: function, data, and operator identifiers may be overloaded.
1790//
1791// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
1792//     and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
1793//     definitions of new types. Type declarations with storage class "extern" provide opaque types.
1794//
1795// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
1796//     site. A polymorphic function is not a template; it is a function, with an address and a type.
1797//
1798// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
1799//     types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
1800//     hierarchies. Unlike classes, they can define relationships between types.  Assertions declare that a type or
1801//     types provide the operations declared by a specification.  Assertions are normally used to declare requirements
1802//     on type arguments of polymorphic functions.
1803
1804typegen_declaration_specifier:                                                  // CFA
1805        typegen_type_specifier
1806        | declaration_qualifier_list typegen_type_specifier
1807                { $$ = $2->addQualifiers( $1 ); }
1808        | typegen_declaration_specifier storage_class           // remaining OBSOLESCENT (see 2)
1809                { $$ = $1->addQualifiers( $2 ); }
1810        | typegen_declaration_specifier storage_class type_qualifier_list
1811                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1812        ;
1813
1814typegen_type_specifier:                                                                 // CFA
1815        typegen_name
1816        | type_qualifier_list typegen_name
1817                { $$ = $2->addQualifiers( $1 ); }
1818        | typegen_type_specifier type_qualifier
1819                { $$ = $1->addQualifiers( $2 ); }
1820        ;
1821
1822typegen_name:                                                                                   // CFA
1823        TYPEGENname '(' type_name_list ')'
1824                { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
1825        ;
1826
1827type_parameter_list:                                                                    // CFA
1828        type_parameter assignment_opt
1829        | type_parameter_list ',' type_parameter assignment_opt
1830                { $$ = $1->appendList( $3 ); }
1831        ;
1832
1833type_parameter:                                                                                 // CFA
1834        type_class no_attr_identifier_or_type_name
1835                { typedefTable.addToEnclosingScope( *$2, TypedefTable::TD ); }
1836          assertion_list_opt
1837                { $$ = DeclarationNode::newTypeParam( $1, $2 )->addAssertions( $4 ); }
1838        | type_specifier identifier_parameter_declarator
1839        ;
1840
1841type_class:                                                                                             // CFA
1842        OTYPE
1843                { $$ = DeclarationNode::Type; }
1844        | DTYPE
1845                { $$ = DeclarationNode::Ftype; }
1846        | FTYPE
1847                { $$ = DeclarationNode::Dtype; }
1848        ;
1849
1850assertion_list_opt:                                                                             // CFA
1851        // empty
1852                { $$ = 0; }
1853        | assertion_list_opt assertion
1854                { $$ = $1 != 0 ? $1->appendList( $2 ) : $2; }
1855        ;
1856
1857assertion:                                                                                              // CFA
1858        '|' no_attr_identifier_or_type_name '(' type_name_list ')'
1859                {
1860                        typedefTable.openTrait( *$2 );
1861                        $$ = DeclarationNode::newTraitUse( $2, $4 );
1862                }
1863        | '|' '{' push trait_declaration_list '}'
1864                { $$ = $4; }
1865        | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list '}' '(' type_name_list ')'
1866                { $$ = 0; }
1867        ;
1868
1869type_name_list:                                                                                 // CFA
1870        type_name
1871                { $$ = new TypeValueNode( $1 ); }
1872        | assignment_expression
1873        | type_name_list ',' type_name
1874                { $$ = (ExpressionNode *)( $1->set_link( new TypeValueNode( $3 ))); }
1875        | type_name_list ',' assignment_expression
1876                { $$ = (ExpressionNode *)( $1->set_link( $3 )); }
1877        ;
1878
1879type_declaring_list:                                                                    // CFA
1880        OTYPE type_declarator
1881                { $$ = $2; }
1882        | storage_class_list OTYPE type_declarator
1883                { $$ = $3->addQualifiers( $1 ); }
1884        | type_declaring_list ',' type_declarator
1885                { $$ = $1->appendList( $3->copyStorageClasses( $1 ) ); }
1886        ;
1887
1888type_declarator:                                                                                // CFA
1889        type_declarator_name assertion_list_opt
1890                { $$ = $1->addAssertions( $2 ); }
1891        | type_declarator_name assertion_list_opt '=' type_name
1892                { $$ = $1->addAssertions( $2 )->addType( $4 ); }
1893        ;
1894
1895type_declarator_name:                                                                   // CFA
1896        no_attr_identifier_or_type_name
1897                {
1898                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
1899                        $$ = DeclarationNode::newTypeDecl( $1, 0 );
1900                }
1901        | no_01_identifier_or_type_name '(' push type_parameter_list pop ')'
1902                {
1903                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
1904                        $$ = DeclarationNode::newTypeDecl( $1, $4 );
1905                }
1906        ;
1907
1908trait_specifier:                                                                                // CFA
1909        TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{' '}'
1910                {
1911                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
1912                        $$ = DeclarationNode::newTrait( $2, $5, 0 );
1913                }
1914        | TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{'
1915                {
1916                        typedefTable.enterTrait( *$2 );
1917                        typedefTable.enterScope();
1918                }
1919          trait_declaration_list '}'
1920                {
1921                        typedefTable.leaveTrait();
1922                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
1923                        $$ = DeclarationNode::newTrait( $2, $5, $10 );
1924                }
1925        ;
1926
1927trait_declaration_list:                                                         // CFA
1928        trait_declaration
1929        | trait_declaration_list push trait_declaration
1930                { $$ = $1->appendList( $3 ); }
1931        ;
1932
1933trait_declaration:                                                                      // CFA
1934        new_trait_declaring_list pop ';'
1935        | trait_declaring_list pop ';'
1936        ;
1937
1938new_trait_declaring_list:                                                               // CFA
1939        new_variable_specifier
1940                {
1941                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1942                        $$ = $1;
1943                }
1944        | new_function_specifier
1945                {
1946                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1947                        $$ = $1;
1948                }
1949        | new_trait_declaring_list pop ',' push identifier_or_type_name
1950                {
1951                        typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
1952                        $$ = $1->appendList( $1->cloneType( $5 ) );
1953                }
1954        ;
1955
1956trait_declaring_list:                                                                   // CFA
1957        type_specifier declarator
1958                {
1959                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1960                        $$ = $2->addType( $1 );
1961                }
1962        | trait_declaring_list pop ',' push declarator
1963                {
1964                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1965                        $$ = $1->appendList( $1->cloneBaseType( $5 ) );
1966                }
1967        ;
1968
1969//***************************** EXTERNAL DEFINITIONS *****************************
1970
1971translation_unit:
1972        // empty
1973                {}                                                                                              // empty input file
1974        | external_definition_list
1975                {
1976                        if ( theTree ) {
1977                                theTree->appendList( $1 );
1978                        } else {
1979                                theTree = $1;
1980                        }
1981                }
1982        ;
1983
1984external_definition_list:
1985        external_definition
1986        | external_definition_list push external_definition
1987                { $$ = ( $1 != NULL ) ? $1->appendList( $3 ) : $3; }
1988        ;
1989
1990external_definition_list_opt:
1991        // empty
1992                { $$ = 0; }
1993        | external_definition_list
1994        ;
1995
1996external_definition:
1997        declaration
1998        | external_function_definition
1999        | asm_statement                                                                         // GCC, global assembler statement
2000                {}
2001        | EXTERN STRINGliteral
2002                {
2003                        linkageStack.push( linkage );
2004                        linkage = LinkageSpec::fromString( *$2 );
2005                }
2006          '{' external_definition_list_opt '}'                          // C++-style linkage specifier
2007                {
2008                        linkage = linkageStack.top();
2009                        linkageStack.pop();
2010                        $$ = $5;
2011                }
2012        | EXTENSION external_definition
2013                {       // mark all fields in list
2014                        for ( DeclarationNode *iter = $2; iter != NULL; iter = (DeclarationNode *)iter->get_link() )
2015                                iter->set_extension( true );
2016                        $$ = $2;
2017                }
2018        ;
2019
2020external_function_definition:
2021        function_definition
2022                // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
2023                // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2024                // Parsing is possible because function_definition does not appear in the context of an expression (nested
2025                // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2026                // declaration must still have a type_specifier.  OBSOLESCENT (see 1)
2027        | function_declarator compound_statement
2028                {
2029                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2030                        typedefTable.leaveScope();
2031                        $$ = $1->addFunctionBody( $2 );
2032                }
2033        | old_function_declarator push old_declaration_list_opt compound_statement
2034                {
2035                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2036                        typedefTable.leaveScope();
2037                        $$ = $1->addOldDeclList( $3 )->addFunctionBody( $4 );
2038                }
2039        ;
2040
2041function_definition:
2042        new_function_declaration compound_statement                     // CFA
2043                {
2044                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2045                        typedefTable.leaveScope();
2046                        $$ = $1->addFunctionBody( $2 );
2047                }
2048        | declaration_specifier function_declarator compound_statement
2049                {
2050                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2051                        typedefTable.leaveScope();
2052                        $$ = $2->addFunctionBody( $3 )->addType( $1 );
2053                }
2054        | type_qualifier_list function_declarator compound_statement
2055                {
2056                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2057                        typedefTable.leaveScope();
2058                        $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
2059                }
2060        | declaration_qualifier_list function_declarator compound_statement
2061                {
2062                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2063                        typedefTable.leaveScope();
2064                        $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
2065                }
2066        | declaration_qualifier_list type_qualifier_list function_declarator compound_statement
2067                {
2068                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2069                        typedefTable.leaveScope();
2070                        $$ = $3->addFunctionBody( $4 )->addQualifiers( $2 )->addQualifiers( $1 );
2071                }
2072
2073                // Old-style K&R function definition, OBSOLESCENT (see 4)
2074        | declaration_specifier old_function_declarator push old_declaration_list_opt compound_statement
2075                {
2076                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2077                        typedefTable.leaveScope();
2078                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addType( $1 );
2079                }
2080        | type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2081                {
2082                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2083                        typedefTable.leaveScope();
2084                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2085                }
2086
2087                // Old-style K&R function definition with "implicit int" type_specifier, OBSOLESCENT (see 4)
2088        | declaration_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2089                {
2090                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2091                        typedefTable.leaveScope();
2092                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2093                }
2094        | declaration_qualifier_list type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2095                {
2096                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2097                        typedefTable.leaveScope();
2098                        $$ = $3->addOldDeclList( $5 )->addFunctionBody( $6 )->addQualifiers( $2 )->addQualifiers( $1 );
2099                }
2100        ;
2101
2102declarator:
2103        variable_declarator
2104        | variable_type_redeclarator
2105        | function_declarator
2106        ;
2107
2108subrange:
2109        constant_expression '~' constant_expression                     // CFA, integer subrange
2110                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Range ), $1, $3 ); }
2111        ;
2112
2113asm_name_opt:                                                                                   // GCC
2114        // empty
2115        | ASM '(' string_literal_list ')' attribute_list_opt
2116        ;
2117
2118attribute_list_opt:                                                                             // GCC
2119        // empty
2120                { $$ = 0; }
2121        | attribute_list
2122        ;
2123
2124attribute_list:                                                                                 // GCC
2125        attribute
2126        | attribute_list attribute
2127                { $$ = $2->addQualifiers( $1 ); }
2128        ;
2129
2130attribute:                                                                                              // GCC
2131        ATTRIBUTE '(' '(' attribute_parameter_list ')' ')'
2132        //              { $$ = DeclarationNode::newQualifier( DeclarationNode::Attribute ); }
2133                { $$ = 0; }
2134        ;
2135
2136attribute_parameter_list:                                                               // GCC
2137        attrib
2138        | attribute_parameter_list ',' attrib
2139        ;
2140
2141attrib:                                                                                                 // GCC
2142        // empty
2143        | any_word
2144        | any_word '(' comma_expression_opt ')'
2145        ;
2146
2147any_word:                                                                                               // GCC
2148        identifier_or_type_name {}
2149        | storage_class_name {}
2150        | basic_type_name {}
2151        | type_qualifier {}
2152        ;
2153
2154// ============================================================================
2155// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2156// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2157// in:
2158//
2159//              int (*f())[10] { ... };
2160//              ... (*f())[3] += 1;             // definition mimics usage
2161//
2162// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2163// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
2164// ============================================================================
2165
2166// ----------------------------------------------------------------------------
2167// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2168// to define a variable or function prototype, e.g.:
2169//
2170//              valid declaration               invalid definition
2171//              -----------------               ------------------
2172//              int f;                                  int f {}
2173//              int *f;                                 int *f {}
2174//              int f[10];                              int f[10] {}
2175//              int (*f)(int);                  int (*f)(int) {}
2176//
2177// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2178// variable_declarator and function_declarator.
2179// ----------------------------------------------------------------------------
2180
2181// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2182// declaring an array of functions versus a pointer to an array of functions.
2183
2184variable_declarator:
2185        paren_identifier attribute_list_opt
2186                { $$ = $1->addQualifiers( $2 ); }
2187        | variable_ptr
2188        | variable_array attribute_list_opt
2189                { $$ = $1->addQualifiers( $2 ); }
2190        | variable_function attribute_list_opt
2191                { $$ = $1->addQualifiers( $2 ); }
2192        ;
2193
2194paren_identifier:
2195        identifier
2196                {
2197                        typedefTable.setNextIdentifier( *$1 );
2198                        $$ = DeclarationNode::newName( $1 );
2199                }
2200        | '(' paren_identifier ')'                                                      // redundant parenthesis
2201                { $$ = $2; }
2202        ;
2203
2204variable_ptr:
2205        ptrref_operator variable_declarator
2206                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2207        | ptrref_operator type_qualifier_list variable_declarator
2208                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2209        | '(' variable_ptr ')'
2210                { $$ = $2; }
2211        ;
2212
2213variable_array:
2214        paren_identifier array_dimension
2215                { $$ = $1->addArray( $2 ); }
2216        | '(' variable_ptr ')' array_dimension
2217                { $$ = $2->addArray( $4 ); }
2218        | '(' variable_array ')' multi_array_dimension          // redundant parenthesis
2219                { $$ = $2->addArray( $4 ); }
2220        | '(' variable_array ')'                                                        // redundant parenthesis
2221                { $$ = $2; }
2222        ;
2223
2224variable_function:
2225        '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2226                { $$ = $2->addParamList( $6 ); }
2227        | '(' variable_function ')'                                                     // redundant parenthesis
2228                { $$ = $2; }
2229        ;
2230
2231// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2232// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2233// is the same scope.  The pattern precludes returning arrays and functions versus pointers to arrays and functions.
2234
2235function_declarator:
2236        function_no_ptr attribute_list_opt
2237                { $$ = $1->addQualifiers( $2 ); }
2238        | function_ptr
2239        | function_array attribute_list_opt
2240                { $$ = $1->addQualifiers( $2 ); }
2241        ;
2242
2243function_no_ptr:
2244        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2245                { $$ = $1->addParamList( $4 ); }
2246        | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2247                { $$ = $2->addParamList( $6 ); }
2248        | '(' function_no_ptr ')'                                                       // redundant parenthesis
2249                { $$ = $2; }
2250        ;
2251
2252function_ptr:
2253        ptrref_operator function_declarator
2254                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2255        | ptrref_operator type_qualifier_list function_declarator
2256                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2257        | '(' function_ptr ')'
2258                { $$ = $2; }
2259        ;
2260
2261function_array:
2262        '(' function_ptr ')' array_dimension
2263                { $$ = $2->addArray( $4 ); }
2264        | '(' function_array ')' multi_array_dimension          // redundant parenthesis
2265                { $$ = $2->addArray( $4 ); }
2266        | '(' function_array ')'                                                        // redundant parenthesis
2267                { $$ = $2; }
2268        ;
2269
2270// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4) that is not redefining a typedef name
2271// (see function_declarator for additional comments). The pattern precludes returning arrays and functions versus
2272// pointers to arrays and functions.
2273
2274old_function_declarator:
2275        old_function_no_ptr
2276        | old_function_ptr
2277        | old_function_array
2278        ;
2279
2280old_function_no_ptr:
2281        paren_identifier '(' identifier_list ')'                        // function_declarator handles empty parameter
2282                { $$ = $1->addIdList( $3 ); }
2283        | '(' old_function_ptr ')' '(' identifier_list ')'
2284                { $$ = $2->addIdList( $5 ); }
2285        | '(' old_function_no_ptr ')'                                           // redundant parenthesis
2286                { $$ = $2; }
2287        ;
2288
2289old_function_ptr:
2290        ptrref_operator old_function_declarator
2291                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2292        | ptrref_operator type_qualifier_list old_function_declarator
2293                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2294        | '(' old_function_ptr ')'
2295                { $$ = $2; }
2296        ;
2297
2298old_function_array:
2299        '(' old_function_ptr ')' array_dimension
2300                { $$ = $2->addArray( $4 ); }
2301        | '(' old_function_array ')' multi_array_dimension      // redundant parenthesis
2302                { $$ = $2->addArray( $4 ); }
2303        | '(' old_function_array ')'                                            // redundant parenthesis
2304                { $$ = $2; }
2305        ;
2306
2307// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
2308//
2309//              typedef int foo;
2310//              {
2311//                 int foo; // redefine typedef name in new scope
2312//              }
2313//
2314// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2315// and functions versus pointers to arrays and functions.
2316
2317variable_type_redeclarator:
2318        paren_type attribute_list_opt
2319                { $$ = $1->addQualifiers( $2 ); }
2320        | type_ptr
2321        | type_array attribute_list_opt
2322                { $$ = $1->addQualifiers( $2 ); }
2323        | type_function attribute_list_opt
2324                { $$ = $1->addQualifiers( $2 ); }
2325        ;
2326
2327paren_type:
2328        typedef
2329        | '(' paren_type ')'
2330                { $$ = $2; }
2331        ;
2332
2333type_ptr:
2334        ptrref_operator variable_type_redeclarator
2335                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2336        | ptrref_operator type_qualifier_list variable_type_redeclarator
2337                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2338        | '(' type_ptr ')'
2339                { $$ = $2; }
2340        ;
2341
2342type_array:
2343        paren_type array_dimension
2344                { $$ = $1->addArray( $2 ); }
2345        | '(' type_ptr ')' array_dimension
2346                { $$ = $2->addArray( $4 ); }
2347        | '(' type_array ')' multi_array_dimension                      // redundant parenthesis
2348                { $$ = $2->addArray( $4 ); }
2349        | '(' type_array ')'                                                            // redundant parenthesis
2350                { $$ = $2; }
2351        ;
2352
2353type_function:
2354        paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2355                { $$ = $1->addParamList( $4 ); }
2356        | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2357                { $$ = $2->addParamList( $6 ); }
2358        | '(' type_function ')'                                                         // redundant parenthesis
2359                { $$ = $2; }
2360        ;
2361
2362// This pattern parses a declaration for a parameter variable or function prototype that is not redefining a typedef
2363// name and allows the C99 array options, which can only appear in a parameter list.  The pattern precludes declaring an
2364// array of functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to
2365// arrays and functions.
2366
2367identifier_parameter_declarator:
2368        paren_identifier attribute_list_opt
2369                { $$ = $1->addQualifiers( $2 ); }
2370        | identifier_parameter_ptr
2371        | identifier_parameter_array attribute_list_opt
2372                { $$ = $1->addQualifiers( $2 ); }
2373        | identifier_parameter_function attribute_list_opt
2374                { $$ = $1->addQualifiers( $2 ); }
2375        ;
2376
2377identifier_parameter_ptr:
2378        ptrref_operator identifier_parameter_declarator
2379                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2380        | ptrref_operator type_qualifier_list identifier_parameter_declarator
2381                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2382        | '(' identifier_parameter_ptr ')'
2383                { $$ = $2; }
2384        ;
2385
2386identifier_parameter_array:
2387        paren_identifier array_parameter_dimension
2388                { $$ = $1->addArray( $2 ); }
2389        | '(' identifier_parameter_ptr ')' array_dimension
2390                { $$ = $2->addArray( $4 ); }
2391        | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2392                { $$ = $2->addArray( $4 ); }
2393        | '(' identifier_parameter_array ')'                            // redundant parenthesis
2394                { $$ = $2; }
2395        ;
2396
2397identifier_parameter_function:
2398        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2399                { $$ = $1->addParamList( $4 ); }
2400        | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2401                { $$ = $2->addParamList( $6 ); }
2402        | '(' identifier_parameter_function ')'                         // redundant parenthesis
2403                { $$ = $2; }
2404        ;
2405
2406// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2407// e.g.:
2408//
2409//              typedef int foo;
2410//              int f( int foo ); // redefine typedef name in new scope
2411//
2412// and allows the C99 array options, which can only appear in a parameter list.  In addition, the pattern handles the
2413// special meaning of parenthesis around a typedef name:
2414//
2415//              ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2416//              parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2417//              not as redundant parentheses around the identifier."
2418//
2419// For example:
2420//
2421//              typedef float T;
2422//              int f( int ( T [5] ) );                                 // see abstract_parameter_declarator
2423//              int g( int ( T ( int ) ) );                             // see abstract_parameter_declarator
2424//              int f( int f1( T a[5] ) );                              // see identifier_parameter_declarator
2425//              int g( int g1( T g2( int p ) ) );               // see identifier_parameter_declarator
2426//
2427// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2428// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2429// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2430// functions.
2431
2432type_parameter_redeclarator:
2433        typedef attribute_list_opt
2434                { $$ = $1->addQualifiers( $2 ); }
2435        | type_parameter_ptr
2436        | type_parameter_array attribute_list_opt
2437                { $$ = $1->addQualifiers( $2 ); }
2438        | type_parameter_function attribute_list_opt
2439                { $$ = $1->addQualifiers( $2 ); }
2440        ;
2441
2442typedef:
2443        TYPEDEFname
2444                {
2445                        typedefTable.setNextIdentifier( *$1 );
2446                        $$ = DeclarationNode::newName( $1 );
2447                }
2448        | TYPEGENname
2449                {
2450                        typedefTable.setNextIdentifier( *$1 );
2451                        $$ = DeclarationNode::newName( $1 );
2452                }
2453        ;
2454
2455type_parameter_ptr:
2456        ptrref_operator type_parameter_redeclarator
2457                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2458        | ptrref_operator type_qualifier_list type_parameter_redeclarator
2459                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2460        | '(' type_parameter_ptr ')'
2461                { $$ = $2; }
2462        ;
2463
2464type_parameter_array:
2465        typedef array_parameter_dimension
2466                { $$ = $1->addArray( $2 ); }
2467        | '(' type_parameter_ptr ')' array_parameter_dimension
2468                { $$ = $2->addArray( $4 ); }
2469        ;
2470
2471type_parameter_function:
2472        typedef '(' push parameter_type_list_opt pop ')'        // empty parameter list OBSOLESCENT (see 3)
2473                { $$ = $1->addParamList( $4 ); }
2474        | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2475                { $$ = $2->addParamList( $6 ); }
2476        ;
2477
2478// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2479// which the type applies, e.g.:
2480//
2481//              sizeof( int );
2482//              sizeof( int [10] );
2483//
2484// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2485// and functions versus pointers to arrays and functions.
2486
2487abstract_declarator:
2488        abstract_ptr
2489        | abstract_array attribute_list_opt
2490                { $$ = $1->addQualifiers( $2 ); }
2491        | abstract_function attribute_list_opt
2492                { $$ = $1->addQualifiers( $2 ); }
2493        ;
2494
2495abstract_ptr:
2496        ptrref_operator
2497                { $$ = DeclarationNode::newPointer( 0 ); }
2498        | ptrref_operator type_qualifier_list
2499                { $$ = DeclarationNode::newPointer( $2 ); }
2500        | ptrref_operator abstract_declarator
2501                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2502        | ptrref_operator type_qualifier_list abstract_declarator
2503                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2504        | '(' abstract_ptr ')'
2505                { $$ = $2; }
2506        ;
2507
2508abstract_array:
2509        array_dimension
2510        | '(' abstract_ptr ')' array_dimension
2511                { $$ = $2->addArray( $4 ); }
2512        | '(' abstract_array ')' multi_array_dimension          // redundant parenthesis
2513                { $$ = $2->addArray( $4 ); }
2514        | '(' abstract_array ')'                                                        // redundant parenthesis
2515                { $$ = $2; }
2516        ;
2517
2518abstract_function:
2519        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2520                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2521        | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2522                { $$ = $2->addParamList( $6 ); }
2523        | '(' abstract_function ')'                                                     // redundant parenthesis
2524                { $$ = $2; }
2525        ;
2526
2527array_dimension:
2528                // Only the first dimension can be empty.
2529        '[' ']'
2530                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2531        | '[' ']' multi_array_dimension
2532                { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
2533        | multi_array_dimension
2534        ;
2535
2536multi_array_dimension:
2537        '[' push assignment_expression pop ']'
2538                { $$ = DeclarationNode::newArray( $3, 0, false ); }
2539        | '[' push '*' pop ']'                                                          // C99
2540                { $$ = DeclarationNode::newVarArray( 0 ); }
2541        | multi_array_dimension '[' push assignment_expression pop ']'
2542                { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2543        | multi_array_dimension '[' push '*' pop ']'            // C99
2544                { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2545        ;
2546
2547// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2548// identifier to which the type applies, e.g.:
2549//
2550//              int f( int );                   // abstract variable parameter; no parameter name specified
2551//              int f( int (int) );             // abstract function-prototype parameter; no parameter name specified
2552//
2553// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2554// and functions versus pointers to arrays and functions.
2555
2556abstract_parameter_declarator:
2557        abstract_parameter_ptr
2558        | abstract_parameter_array attribute_list_opt
2559                { $$ = $1->addQualifiers( $2 ); }
2560        | abstract_parameter_function attribute_list_opt
2561                { $$ = $1->addQualifiers( $2 ); }
2562        ;
2563
2564abstract_parameter_ptr:
2565        ptrref_operator
2566                { $$ = DeclarationNode::newPointer( 0 ); }
2567        | ptrref_operator type_qualifier_list
2568                { $$ = DeclarationNode::newPointer( $2 ); }
2569        | ptrref_operator abstract_parameter_declarator
2570                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2571        | ptrref_operator type_qualifier_list abstract_parameter_declarator
2572                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2573        | '(' abstract_parameter_ptr ')'
2574                { $$ = $2; }
2575        ;
2576
2577abstract_parameter_array:
2578        array_parameter_dimension
2579        | '(' abstract_parameter_ptr ')' array_parameter_dimension
2580                { $$ = $2->addArray( $4 ); }
2581        | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2582                { $$ = $2->addArray( $4 ); }
2583        | '(' abstract_parameter_array ')'                                      // redundant parenthesis
2584                { $$ = $2; }
2585        ;
2586
2587abstract_parameter_function:
2588        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2589                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2590        | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2591                { $$ = $2->addParamList( $6 ); }
2592        | '(' abstract_parameter_function ')'                           // redundant parenthesis
2593                { $$ = $2; }
2594        ;
2595
2596array_parameter_dimension:
2597                // Only the first dimension can be empty or have qualifiers.
2598        array_parameter_1st_dimension
2599        | array_parameter_1st_dimension multi_array_dimension
2600                { $$ = $1->addArray( $2 ); }
2601        | multi_array_dimension
2602        ;
2603
2604// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2605//
2606//              ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2607//              a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
2608
2609array_parameter_1st_dimension:
2610        '[' ']'
2611                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2612        // multi_array_dimension handles the '[' '*' ']' case
2613        | '[' push type_qualifier_list '*' pop ']'                      // remaining C99
2614                { $$ = DeclarationNode::newVarArray( $3 ); }
2615        | '[' push type_qualifier_list pop ']'
2616                { $$ = DeclarationNode::newArray( 0, $3, false ); }
2617        // multi_array_dimension handles the '[' assignment_expression ']' case
2618        | '[' push type_qualifier_list assignment_expression pop ']'
2619                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2620        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2621                { $$ = DeclarationNode::newArray( $5, $4, true ); }
2622        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2623                { $$ = DeclarationNode::newArray( $5, $3, true ); }
2624        ;
2625
2626// This pattern parses a declaration of an abstract variable, i.e., there is no identifier to which the type applies,
2627// e.g.:
2628//
2629//              sizeof( int ); // abstract variable; no identifier name specified
2630//
2631// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2632// and functions versus pointers to arrays and functions.
2633
2634variable_abstract_declarator:
2635        variable_abstract_ptr
2636        | variable_abstract_array attribute_list_opt
2637                { $$ = $1->addQualifiers( $2 ); }
2638        | variable_abstract_function attribute_list_opt
2639                { $$ = $1->addQualifiers( $2 ); }
2640        ;
2641
2642variable_abstract_ptr:
2643        ptrref_operator
2644                { $$ = DeclarationNode::newPointer( 0 ); }
2645        | ptrref_operator type_qualifier_list
2646                { $$ = DeclarationNode::newPointer( $2 ); }
2647        | ptrref_operator variable_abstract_declarator
2648                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2649        | ptrref_operator type_qualifier_list variable_abstract_declarator
2650                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2651        | '(' variable_abstract_ptr ')'
2652                { $$ = $2; }
2653        ;
2654
2655variable_abstract_array:
2656        array_dimension
2657        | '(' variable_abstract_ptr ')' array_dimension
2658                { $$ = $2->addArray( $4 ); }
2659        | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
2660                { $$ = $2->addArray( $4 ); }
2661        | '(' variable_abstract_array ')'                                       // redundant parenthesis
2662                { $$ = $2; }
2663        ;
2664
2665variable_abstract_function:
2666        '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2667                { $$ = $2->addParamList( $6 ); }
2668        | '(' variable_abstract_function ')'                            // redundant parenthesis
2669                { $$ = $2; }
2670        ;
2671
2672// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
2673// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
2674
2675new_identifier_parameter_declarator_tuple:                              // CFA
2676        new_identifier_parameter_declarator_no_tuple
2677        | new_abstract_tuple
2678        | type_qualifier_list new_abstract_tuple
2679                { $$ = $2->addQualifiers( $1 ); }
2680        ;
2681
2682new_identifier_parameter_declarator_no_tuple:                   // CFA
2683        new_identifier_parameter_ptr
2684        | new_identifier_parameter_array
2685        ;
2686
2687new_identifier_parameter_ptr:                                                   // CFA
2688        ptrref_operator type_specifier
2689                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2690        | type_qualifier_list ptrref_operator type_specifier
2691                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2692        | ptrref_operator new_abstract_function
2693                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2694        | type_qualifier_list ptrref_operator new_abstract_function
2695                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2696        | ptrref_operator new_identifier_parameter_declarator_tuple
2697                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2698        | type_qualifier_list ptrref_operator new_identifier_parameter_declarator_tuple
2699                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2700        ;
2701
2702new_identifier_parameter_array:                                                 // CFA
2703                // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
2704                // shift/reduce conflict with new-style empty (void) function return type.
2705        '[' ']' type_specifier
2706                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2707        | new_array_parameter_1st_dimension type_specifier
2708                { $$ = $2->addNewArray( $1 ); }
2709        | '[' ']' multi_array_dimension type_specifier
2710                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2711        | new_array_parameter_1st_dimension multi_array_dimension type_specifier
2712                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2713        | multi_array_dimension type_specifier
2714                { $$ = $2->addNewArray( $1 ); }
2715        | '[' ']' new_identifier_parameter_ptr
2716                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2717        | new_array_parameter_1st_dimension new_identifier_parameter_ptr
2718                { $$ = $2->addNewArray( $1 ); }
2719        | '[' ']' multi_array_dimension new_identifier_parameter_ptr
2720                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2721        | new_array_parameter_1st_dimension multi_array_dimension new_identifier_parameter_ptr
2722                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2723        | multi_array_dimension new_identifier_parameter_ptr
2724                { $$ = $2->addNewArray( $1 ); }
2725        ;
2726
2727new_array_parameter_1st_dimension:
2728        '[' push type_qualifier_list '*' pop ']'                        // remaining C99
2729                { $$ = DeclarationNode::newVarArray( $3 ); }
2730        | '[' push type_qualifier_list assignment_expression pop ']'
2731                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2732        | '[' push declaration_qualifier_list assignment_expression pop ']'
2733                // declaration_qualifier_list must be used because of shift/reduce conflict with
2734                // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
2735                // appear in this context.
2736                { $$ = DeclarationNode::newArray( $4, $3, true ); }
2737        | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
2738                { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
2739        ;
2740
2741// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
2742// identifier to which the type applies, e.g.:
2743//
2744//              [int] f( int );                         // abstract variable parameter; no parameter name specified
2745//              [int] f( [int] (int) );         // abstract function-prototype parameter; no parameter name specified
2746//
2747// These rules need LR(3):
2748//
2749//              new_abstract_tuple identifier_or_type_name
2750//              '[' new_parameter_list ']' identifier_or_type_name '(' new_parameter_type_list_opt ')'
2751//
2752// since a function return type can be syntactically identical to a tuple type:
2753//
2754//              [int, int] t;
2755//              [int, int] f( int );
2756//
2757// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
2758// new_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
2759// lookahead. To accomplish this, new_abstract_declarator has an entry point without tuple, and tuple declarations are
2760// duplicated when appearing with new_function_specifier.
2761
2762new_abstract_declarator_tuple:                                                  // CFA
2763        new_abstract_tuple
2764        | type_qualifier_list new_abstract_tuple
2765                { $$ = $2->addQualifiers( $1 ); }
2766        | new_abstract_declarator_no_tuple
2767        ;
2768
2769new_abstract_declarator_no_tuple:                                               // CFA
2770        new_abstract_ptr
2771        | new_abstract_array
2772        ;
2773
2774new_abstract_ptr:                                                                               // CFA
2775        ptrref_operator type_specifier
2776                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2777        | type_qualifier_list ptrref_operator type_specifier
2778                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2779        | ptrref_operator new_abstract_function
2780                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2781        | type_qualifier_list ptrref_operator new_abstract_function
2782                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2783        | ptrref_operator new_abstract_declarator_tuple
2784                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2785        | type_qualifier_list ptrref_operator new_abstract_declarator_tuple
2786                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2787        ;
2788
2789new_abstract_array:                                                                             // CFA
2790                // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
2791                // empty (void) function return type.
2792        '[' ']' type_specifier
2793                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2794        | '[' ']' multi_array_dimension type_specifier
2795                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2796        | multi_array_dimension type_specifier
2797                { $$ = $2->addNewArray( $1 ); }
2798        | '[' ']' new_abstract_ptr
2799                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2800        | '[' ']' multi_array_dimension new_abstract_ptr
2801                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2802        | multi_array_dimension new_abstract_ptr
2803                { $$ = $2->addNewArray( $1 ); }
2804        ;
2805
2806new_abstract_tuple:                                                                             // CFA
2807        '[' push new_abstract_parameter_list pop ']'
2808                { $$ = DeclarationNode::newTuple( $3 ); }
2809        ;
2810
2811new_abstract_function:                                                                  // CFA
2812        '[' ']' '(' new_parameter_type_list_opt ')'
2813                { $$ = DeclarationNode::newFunction( 0, DeclarationNode::newTuple( 0 ), $4, 0 ); }
2814        | new_abstract_tuple '(' push new_parameter_type_list_opt pop ')'
2815                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2816        | new_function_return '(' push new_parameter_type_list_opt pop ')'
2817                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2818        ;
2819
2820// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
2821//    each declaration, and in the specifier-qualifier list in each structure declaration and type name."
2822//
2823// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
2824//    the declaration specifiers in a declaration is an obsolescent feature."
2825//
2826// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
2827//    prototype-format parameter type declarators) is an obsolescent feature."
2828//
2829// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
2830//    declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
2831
2832//************************* MISCELLANEOUS ********************************
2833
2834comma_opt:                                                                                              // redundant comma
2835        // empty
2836        | ','
2837        ;
2838
2839assignment_opt:
2840        // empty
2841                { $$ = 0; }
2842        | '=' assignment_expression
2843                { $$ = $2; }
2844        ;
2845
2846%%
2847// ----end of grammar----
2848
2849void yyerror( const char * ) {
2850        std::cout << "Error ";
2851        if ( yyfilename ) {
2852                std::cout << "in file " << yyfilename << " ";
2853        } // if
2854        std::cout << "at line " << yylineno << " reading token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << "\"" << std::endl;
2855}
2856
2857// Local Variables: //
2858// mode: c++ //
2859// tab-width: 4 //
2860// compile-command: "make install" //
2861// End: //
Note: See TracBrowser for help on using the repository browser.