source: src/Parser/parser.yy @ 24f4671

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 24f4671 was 4cb935e, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

0 and 1 now properly parse and resolve to zero_t and one_t respectively

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