source: src/Parser/parser.yy @ b512454

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

fix conflicts

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