source: src/Parser/parser.yy @ 2037f82

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 2037f82 was 2037f82, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

added many missing calls to delete, still a long way to go...

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