source: src/Parser/parser.yy @ 9059213

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

small changes to bring me up to date

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