source: src/Parser/parser.yy @ 66f8528

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

fix empty assignment_opt allocation

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