source: src/Parser/parser.yy @ c1c1112

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since c1c1112 was c1c1112, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

fix segment fault when printing syntax error, more refactoring of parser code

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