source: src/Parser/parser.yy @ a378ca7

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 a378ca7 was a378ca7, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Build WithStmt? node in parser

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