source: src/Parser/parser.yy @ 4b1afb6

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

lex/parse new tuples

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