source: src/Parser/parser.yy @ 8e5724e

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

Merge branch 'master' of plg2:software/cfa/cfa-cc

Conflicts:

src/Parser/parser.cc

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