source: src/Parser/parser.yy @ d1fbaa7

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 d1fbaa7 was faddbd8, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more refactoring of parser code, new tuple syntax

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