source: src/Parser/parser.yy @ 08ac489

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 08ac489 was 0da3e2c, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more refactoring of parser code

  • Property mode set to 100644
File size: 102.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// cfa.y --
8//
9// Author           : Peter A. Buhr
10// Created On       : Sat Sep  1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Aug 16 17:10:35 2016
13// Update Count     : 1898
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::Type linkage;
59extern TypedefTable typedefTable;
60
61std::stack< LinkageSpec::Type > 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".
313INTEGERconstant                                                                 { $$ = new ExpressionNode( build_constantInteger( *$1 ) ); }
314        | FLOATINGconstant                                                      { $$ = new ExpressionNode( build_constantFloat( *$1 ) ); }
315        | CHARACTERconstant                                                     { $$ = new ExpressionNode( build_constantChar( *$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( *$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 != NULL; 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( *$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( *$2, BranchStmt::Continue ) ); delete $2; }
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( *$2, BranchStmt::Break ) ); delete $2; }
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( *$1 ); }
982        | label_list ',' no_attr_identifier
983                { $$ = $1; $1->labels.push_back( *$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 != NULL; 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                {
1973                        if ( parseTree ) {
1974                                parseTree->appendList( $1 );
1975                        } else {
1976                                parseTree = $1;
1977                        }
1978                }
1979        ;
1980
1981external_definition_list:
1982        external_definition
1983        | external_definition_list push external_definition
1984                { $$ = ( $1 != NULL ) ? $1->appendList( $3 ) : $3; }
1985        ;
1986
1987external_definition_list_opt:
1988        // empty
1989                { $$ = 0; }
1990        | external_definition_list
1991        ;
1992
1993external_definition:
1994        declaration
1995        | external_function_definition
1996        | asm_statement                                                                         // GCC, global assembler statement
1997                {}
1998        | EXTERN STRINGliteral
1999                {
2000                        linkageStack.push( linkage );
2001                        linkage = LinkageSpec::fromString( *$2 );
2002                }
2003          '{' external_definition_list_opt '}'                          // C++-style linkage specifier
2004                {
2005                        linkage = linkageStack.top();
2006                        linkageStack.pop();
2007                        $$ = $5;
2008                }
2009        | EXTENSION external_definition
2010                {       // mark all fields in list
2011                        for ( DeclarationNode *iter = $2; iter != NULL; iter = (DeclarationNode *)iter->get_next() )
2012                                iter->set_extension( true );
2013                        $$ = $2;
2014                }
2015        ;
2016
2017external_function_definition:
2018        function_definition
2019                // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
2020                // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2021                // Parsing is possible because function_definition does not appear in the context of an expression (nested
2022                // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2023                // declaration must still have a type_specifier.  OBSOLESCENT (see 1)
2024        | function_declarator compound_statement
2025                {
2026                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2027                        typedefTable.leaveScope();
2028                        $$ = $1->addFunctionBody( $2 );
2029                }
2030        | old_function_declarator push old_declaration_list_opt compound_statement
2031                {
2032                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2033                        typedefTable.leaveScope();
2034                        $$ = $1->addOldDeclList( $3 )->addFunctionBody( $4 );
2035                }
2036        ;
2037
2038function_definition:
2039        new_function_declaration compound_statement                     // CFA
2040                {
2041                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2042                        typedefTable.leaveScope();
2043                        $$ = $1->addFunctionBody( $2 );
2044                }
2045        | declaration_specifier function_declarator compound_statement
2046                {
2047                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2048                        typedefTable.leaveScope();
2049                        $$ = $2->addFunctionBody( $3 )->addType( $1 );
2050                }
2051        | type_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 function_declarator compound_statement
2058                {
2059                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2060                        typedefTable.leaveScope();
2061                        $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
2062                }
2063        | declaration_qualifier_list type_qualifier_list function_declarator compound_statement
2064                {
2065                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2066                        typedefTable.leaveScope();
2067                        $$ = $3->addFunctionBody( $4 )->addQualifiers( $2 )->addQualifiers( $1 );
2068                }
2069
2070                // Old-style K&R function definition, OBSOLESCENT (see 4)
2071        | declaration_specifier 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 )->addType( $1 );
2076                }
2077        | type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2078                {
2079                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2080                        typedefTable.leaveScope();
2081                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2082                }
2083
2084                // Old-style K&R function definition with "implicit int" type_specifier, OBSOLESCENT (see 4)
2085        | declaration_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2086                {
2087                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2088                        typedefTable.leaveScope();
2089                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2090                }
2091        | declaration_qualifier_list type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2092                {
2093                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2094                        typedefTable.leaveScope();
2095                        $$ = $3->addOldDeclList( $5 )->addFunctionBody( $6 )->addQualifiers( $2 )->addQualifiers( $1 );
2096                }
2097        ;
2098
2099declarator:
2100        variable_declarator
2101        | variable_type_redeclarator
2102        | function_declarator
2103        ;
2104
2105subrange:
2106        constant_expression '~' constant_expression                     // CFA, integer subrange
2107                { $$ = new ExpressionNode( build_range( $1, $3 ) ); }
2108        ;
2109
2110asm_name_opt:                                                                                   // GCC
2111        // empty
2112        | ASM '(' string_literal_list ')' attribute_list_opt
2113        ;
2114
2115attribute_list_opt:                                                                             // GCC
2116        // empty
2117                { $$ = 0; }
2118        | attribute_list
2119        ;
2120
2121attribute_list:                                                                                 // GCC
2122        attribute
2123        | attribute_list attribute
2124                { $$ = $2->addQualifiers( $1 ); }
2125        ;
2126
2127attribute:                                                                                              // GCC
2128        ATTRIBUTE '(' '(' attribute_parameter_list ')' ')'
2129        //              { $$ = DeclarationNode::newQualifier( DeclarationNode::Attribute ); }
2130                { $$ = 0; }
2131        ;
2132
2133attribute_parameter_list:                                                               // GCC
2134        attrib
2135        | attribute_parameter_list ',' attrib
2136        ;
2137
2138attrib:                                                                                                 // GCC
2139        // empty
2140        | any_word
2141        | any_word '(' comma_expression_opt ')'
2142        ;
2143
2144any_word:                                                                                               // GCC
2145        identifier_or_type_name {}
2146        | storage_class {}
2147        | basic_type_name {}
2148        | type_qualifier {}
2149        ;
2150
2151// ============================================================================
2152// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2153// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2154// in:
2155//
2156//              int (*f())[10] { ... };
2157//              ... (*f())[3] += 1;             // definition mimics usage
2158//
2159// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2160// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
2161// ============================================================================
2162
2163// ----------------------------------------------------------------------------
2164// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2165// to define a variable or function prototype, e.g.:
2166//
2167//              valid declaration               invalid definition
2168//              -----------------               ------------------
2169//              int f;                                  int f {}
2170//              int *f;                                 int *f {}
2171//              int f[10];                              int f[10] {}
2172//              int (*f)(int);                  int (*f)(int) {}
2173//
2174// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2175// variable_declarator and function_declarator.
2176// ----------------------------------------------------------------------------
2177
2178// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2179// declaring an array of functions versus a pointer to an array of functions.
2180
2181variable_declarator:
2182        paren_identifier attribute_list_opt
2183                { $$ = $1->addQualifiers( $2 ); }
2184        | variable_ptr
2185        | variable_array attribute_list_opt
2186                { $$ = $1->addQualifiers( $2 ); }
2187        | variable_function attribute_list_opt
2188                { $$ = $1->addQualifiers( $2 ); }
2189        ;
2190
2191paren_identifier:
2192        identifier
2193                {
2194                        typedefTable.setNextIdentifier( *$1 );
2195                        $$ = DeclarationNode::newName( $1 );
2196                }
2197        | '(' paren_identifier ')'                                                      // redundant parenthesis
2198                { $$ = $2; }
2199        ;
2200
2201variable_ptr:
2202        ptrref_operator variable_declarator
2203                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2204        | ptrref_operator type_qualifier_list variable_declarator
2205                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2206        | '(' variable_ptr ')'
2207                { $$ = $2; }
2208        ;
2209
2210variable_array:
2211        paren_identifier array_dimension
2212                { $$ = $1->addArray( $2 ); }
2213        | '(' variable_ptr ')' array_dimension
2214                { $$ = $2->addArray( $4 ); }
2215        | '(' variable_array ')' multi_array_dimension          // redundant parenthesis
2216                { $$ = $2->addArray( $4 ); }
2217        | '(' variable_array ')'                                                        // redundant parenthesis
2218                { $$ = $2; }
2219        ;
2220
2221variable_function:
2222        '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2223                { $$ = $2->addParamList( $6 ); }
2224        | '(' variable_function ')'                                                     // redundant parenthesis
2225                { $$ = $2; }
2226        ;
2227
2228// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2229// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2230// is the same scope.  The pattern precludes returning arrays and functions versus pointers to arrays and functions.
2231
2232function_declarator:
2233        function_no_ptr attribute_list_opt
2234                { $$ = $1->addQualifiers( $2 ); }
2235        | function_ptr
2236        | function_array attribute_list_opt
2237                { $$ = $1->addQualifiers( $2 ); }
2238        ;
2239
2240function_no_ptr:
2241        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2242                { $$ = $1->addParamList( $4 ); }
2243        | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2244                { $$ = $2->addParamList( $6 ); }
2245        | '(' function_no_ptr ')'                                                       // redundant parenthesis
2246                { $$ = $2; }
2247        ;
2248
2249function_ptr:
2250        ptrref_operator function_declarator
2251                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2252        | ptrref_operator type_qualifier_list function_declarator
2253                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2254        | '(' function_ptr ')'
2255                { $$ = $2; }
2256        ;
2257
2258function_array:
2259        '(' function_ptr ')' array_dimension
2260                { $$ = $2->addArray( $4 ); }
2261        | '(' function_array ')' multi_array_dimension          // redundant parenthesis
2262                { $$ = $2->addArray( $4 ); }
2263        | '(' function_array ')'                                                        // redundant parenthesis
2264                { $$ = $2; }
2265        ;
2266
2267// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4) that is not redefining a typedef name
2268// (see function_declarator for additional comments). The pattern precludes returning arrays and functions versus
2269// pointers to arrays and functions.
2270
2271old_function_declarator:
2272        old_function_no_ptr
2273        | old_function_ptr
2274        | old_function_array
2275        ;
2276
2277old_function_no_ptr:
2278        paren_identifier '(' identifier_list ')'                        // function_declarator handles empty parameter
2279                { $$ = $1->addIdList( $3 ); }
2280        | '(' old_function_ptr ')' '(' identifier_list ')'
2281                { $$ = $2->addIdList( $5 ); }
2282        | '(' old_function_no_ptr ')'                                           // redundant parenthesis
2283                { $$ = $2; }
2284        ;
2285
2286old_function_ptr:
2287        ptrref_operator old_function_declarator
2288                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2289        | ptrref_operator type_qualifier_list old_function_declarator
2290                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2291        | '(' old_function_ptr ')'
2292                { $$ = $2; }
2293        ;
2294
2295old_function_array:
2296        '(' old_function_ptr ')' array_dimension
2297                { $$ = $2->addArray( $4 ); }
2298        | '(' old_function_array ')' multi_array_dimension      // redundant parenthesis
2299                { $$ = $2->addArray( $4 ); }
2300        | '(' old_function_array ')'                                            // redundant parenthesis
2301                { $$ = $2; }
2302        ;
2303
2304// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
2305//
2306//              typedef int foo;
2307//              {
2308//                 int foo; // redefine typedef name in new scope
2309//              }
2310//
2311// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2312// and functions versus pointers to arrays and functions.
2313
2314variable_type_redeclarator:
2315        paren_type attribute_list_opt
2316                { $$ = $1->addQualifiers( $2 ); }
2317        | type_ptr
2318        | type_array attribute_list_opt
2319                { $$ = $1->addQualifiers( $2 ); }
2320        | type_function attribute_list_opt
2321                { $$ = $1->addQualifiers( $2 ); }
2322        ;
2323
2324paren_type:
2325        typedef
2326        | '(' paren_type ')'
2327                { $$ = $2; }
2328        ;
2329
2330type_ptr:
2331        ptrref_operator variable_type_redeclarator
2332                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2333        | ptrref_operator type_qualifier_list variable_type_redeclarator
2334                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2335        | '(' type_ptr ')'
2336                { $$ = $2; }
2337        ;
2338
2339type_array:
2340        paren_type array_dimension
2341                { $$ = $1->addArray( $2 ); }
2342        | '(' type_ptr ')' array_dimension
2343                { $$ = $2->addArray( $4 ); }
2344        | '(' type_array ')' multi_array_dimension                      // redundant parenthesis
2345                { $$ = $2->addArray( $4 ); }
2346        | '(' type_array ')'                                                            // redundant parenthesis
2347                { $$ = $2; }
2348        ;
2349
2350type_function:
2351        paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2352                { $$ = $1->addParamList( $4 ); }
2353        | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2354                { $$ = $2->addParamList( $6 ); }
2355        | '(' type_function ')'                                                         // redundant parenthesis
2356                { $$ = $2; }
2357        ;
2358
2359// This pattern parses a declaration for a parameter variable or function prototype that is not redefining a typedef
2360// name and allows the C99 array options, which can only appear in a parameter list.  The pattern precludes declaring an
2361// array of functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to
2362// arrays and functions.
2363
2364identifier_parameter_declarator:
2365        paren_identifier attribute_list_opt
2366                { $$ = $1->addQualifiers( $2 ); }
2367        | identifier_parameter_ptr
2368        | identifier_parameter_array attribute_list_opt
2369                { $$ = $1->addQualifiers( $2 ); }
2370        | identifier_parameter_function attribute_list_opt
2371                { $$ = $1->addQualifiers( $2 ); }
2372        ;
2373
2374identifier_parameter_ptr:
2375        ptrref_operator identifier_parameter_declarator
2376                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2377        | ptrref_operator type_qualifier_list identifier_parameter_declarator
2378                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2379        | '(' identifier_parameter_ptr ')'
2380                { $$ = $2; }
2381        ;
2382
2383identifier_parameter_array:
2384        paren_identifier array_parameter_dimension
2385                { $$ = $1->addArray( $2 ); }
2386        | '(' identifier_parameter_ptr ')' array_dimension
2387                { $$ = $2->addArray( $4 ); }
2388        | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2389                { $$ = $2->addArray( $4 ); }
2390        | '(' identifier_parameter_array ')'                            // redundant parenthesis
2391                { $$ = $2; }
2392        ;
2393
2394identifier_parameter_function:
2395        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2396                { $$ = $1->addParamList( $4 ); }
2397        | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2398                { $$ = $2->addParamList( $6 ); }
2399        | '(' identifier_parameter_function ')'                         // redundant parenthesis
2400                { $$ = $2; }
2401        ;
2402
2403// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2404// e.g.:
2405//
2406//              typedef int foo;
2407//              int f( int foo ); // redefine typedef name in new scope
2408//
2409// and allows the C99 array options, which can only appear in a parameter list.  In addition, the pattern handles the
2410// special meaning of parenthesis around a typedef name:
2411//
2412//              ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2413//              parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2414//              not as redundant parentheses around the identifier."
2415//
2416// For example:
2417//
2418//              typedef float T;
2419//              int f( int ( T [5] ) );                                 // see abstract_parameter_declarator
2420//              int g( int ( T ( int ) ) );                             // see abstract_parameter_declarator
2421//              int f( int f1( T a[5] ) );                              // see identifier_parameter_declarator
2422//              int g( int g1( T g2( int p ) ) );               // see identifier_parameter_declarator
2423//
2424// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2425// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2426// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2427// functions.
2428
2429type_parameter_redeclarator:
2430        typedef attribute_list_opt
2431                { $$ = $1->addQualifiers( $2 ); }
2432        | type_parameter_ptr
2433        | type_parameter_array attribute_list_opt
2434                { $$ = $1->addQualifiers( $2 ); }
2435        | type_parameter_function attribute_list_opt
2436                { $$ = $1->addQualifiers( $2 ); }
2437        ;
2438
2439typedef:
2440        TYPEDEFname
2441                {
2442                        typedefTable.setNextIdentifier( *$1 );
2443                        $$ = DeclarationNode::newName( $1 );
2444                }
2445        | TYPEGENname
2446                {
2447                        typedefTable.setNextIdentifier( *$1 );
2448                        $$ = DeclarationNode::newName( $1 );
2449                }
2450        ;
2451
2452type_parameter_ptr:
2453        ptrref_operator type_parameter_redeclarator
2454                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2455        | ptrref_operator type_qualifier_list type_parameter_redeclarator
2456                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2457        | '(' type_parameter_ptr ')'
2458                { $$ = $2; }
2459        ;
2460
2461type_parameter_array:
2462        typedef array_parameter_dimension
2463                { $$ = $1->addArray( $2 ); }
2464        | '(' type_parameter_ptr ')' array_parameter_dimension
2465                { $$ = $2->addArray( $4 ); }
2466        ;
2467
2468type_parameter_function:
2469        typedef '(' push parameter_type_list_opt pop ')'        // empty parameter list OBSOLESCENT (see 3)
2470                { $$ = $1->addParamList( $4 ); }
2471        | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2472                { $$ = $2->addParamList( $6 ); }
2473        ;
2474
2475// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2476// which the type applies, e.g.:
2477//
2478//              sizeof( int );
2479//              sizeof( int [10] );
2480//
2481// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2482// and functions versus pointers to arrays and functions.
2483
2484abstract_declarator:
2485        abstract_ptr
2486        | abstract_array attribute_list_opt
2487                { $$ = $1->addQualifiers( $2 ); }
2488        | abstract_function attribute_list_opt
2489                { $$ = $1->addQualifiers( $2 ); }
2490        ;
2491
2492abstract_ptr:
2493        ptrref_operator
2494                { $$ = DeclarationNode::newPointer( 0 ); }
2495        | ptrref_operator type_qualifier_list
2496                { $$ = DeclarationNode::newPointer( $2 ); }
2497        | ptrref_operator abstract_declarator
2498                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2499        | ptrref_operator type_qualifier_list abstract_declarator
2500                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2501        | '(' abstract_ptr ')'
2502                { $$ = $2; }
2503        ;
2504
2505abstract_array:
2506        array_dimension
2507        | '(' abstract_ptr ')' array_dimension
2508                { $$ = $2->addArray( $4 ); }
2509        | '(' abstract_array ')' multi_array_dimension          // redundant parenthesis
2510                { $$ = $2->addArray( $4 ); }
2511        | '(' abstract_array ')'                                                        // redundant parenthesis
2512                { $$ = $2; }
2513        ;
2514
2515abstract_function:
2516        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2517                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2518        | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2519                { $$ = $2->addParamList( $6 ); }
2520        | '(' abstract_function ')'                                                     // redundant parenthesis
2521                { $$ = $2; }
2522        ;
2523
2524array_dimension:
2525                // Only the first dimension can be empty.
2526        '[' ']'
2527                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2528        | '[' ']' multi_array_dimension
2529                { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
2530        | multi_array_dimension
2531        ;
2532
2533multi_array_dimension:
2534        '[' push assignment_expression pop ']'
2535                { $$ = DeclarationNode::newArray( $3, 0, false ); }
2536        | '[' push '*' pop ']'                                                          // C99
2537                { $$ = DeclarationNode::newVarArray( 0 ); }
2538        | multi_array_dimension '[' push assignment_expression pop ']'
2539                { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2540        | multi_array_dimension '[' push '*' pop ']'            // C99
2541                { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2542        ;
2543
2544// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2545// identifier to which the type applies, e.g.:
2546//
2547//              int f( int );                   // abstract variable parameter; no parameter name specified
2548//              int f( int (int) );             // abstract function-prototype parameter; no parameter name specified
2549//
2550// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2551// and functions versus pointers to arrays and functions.
2552
2553abstract_parameter_declarator:
2554        abstract_parameter_ptr
2555        | abstract_parameter_array attribute_list_opt
2556                { $$ = $1->addQualifiers( $2 ); }
2557        | abstract_parameter_function attribute_list_opt
2558                { $$ = $1->addQualifiers( $2 ); }
2559        ;
2560
2561abstract_parameter_ptr:
2562        ptrref_operator
2563                { $$ = DeclarationNode::newPointer( 0 ); }
2564        | ptrref_operator type_qualifier_list
2565                { $$ = DeclarationNode::newPointer( $2 ); }
2566        | ptrref_operator abstract_parameter_declarator
2567                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2568        | ptrref_operator type_qualifier_list abstract_parameter_declarator
2569                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2570        | '(' abstract_parameter_ptr ')'
2571                { $$ = $2; }
2572        ;
2573
2574abstract_parameter_array:
2575        array_parameter_dimension
2576        | '(' abstract_parameter_ptr ')' array_parameter_dimension
2577                { $$ = $2->addArray( $4 ); }
2578        | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2579                { $$ = $2->addArray( $4 ); }
2580        | '(' abstract_parameter_array ')'                                      // redundant parenthesis
2581                { $$ = $2; }
2582        ;
2583
2584abstract_parameter_function:
2585        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2586                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2587        | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2588                { $$ = $2->addParamList( $6 ); }
2589        | '(' abstract_parameter_function ')'                           // redundant parenthesis
2590                { $$ = $2; }
2591        ;
2592
2593array_parameter_dimension:
2594                // Only the first dimension can be empty or have qualifiers.
2595        array_parameter_1st_dimension
2596        | array_parameter_1st_dimension multi_array_dimension
2597                { $$ = $1->addArray( $2 ); }
2598        | multi_array_dimension
2599        ;
2600
2601// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2602//
2603//              ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2604//              a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
2605
2606array_parameter_1st_dimension:
2607        '[' ']'
2608                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2609        // multi_array_dimension handles the '[' '*' ']' case
2610        | '[' push type_qualifier_list '*' pop ']'                      // remaining C99
2611                { $$ = DeclarationNode::newVarArray( $3 ); }
2612        | '[' push type_qualifier_list pop ']'
2613                { $$ = DeclarationNode::newArray( 0, $3, false ); }
2614        // multi_array_dimension handles the '[' assignment_expression ']' case
2615        | '[' push type_qualifier_list assignment_expression pop ']'
2616                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2617        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2618                { $$ = DeclarationNode::newArray( $5, $4, true ); }
2619        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2620                { $$ = DeclarationNode::newArray( $5, $3, true ); }
2621        ;
2622
2623// This pattern parses a declaration of an abstract variable, i.e., there is no identifier to which the type applies,
2624// e.g.:
2625//
2626//              sizeof( int ); // abstract variable; no identifier name specified
2627//
2628// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2629// and functions versus pointers to arrays and functions.
2630
2631variable_abstract_declarator:
2632        variable_abstract_ptr
2633        | variable_abstract_array attribute_list_opt
2634                { $$ = $1->addQualifiers( $2 ); }
2635        | variable_abstract_function attribute_list_opt
2636                { $$ = $1->addQualifiers( $2 ); }
2637        ;
2638
2639variable_abstract_ptr:
2640        ptrref_operator
2641                { $$ = DeclarationNode::newPointer( 0 ); }
2642        | ptrref_operator type_qualifier_list
2643                { $$ = DeclarationNode::newPointer( $2 ); }
2644        | ptrref_operator variable_abstract_declarator
2645                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2646        | ptrref_operator type_qualifier_list variable_abstract_declarator
2647                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2648        | '(' variable_abstract_ptr ')'
2649                { $$ = $2; }
2650        ;
2651
2652variable_abstract_array:
2653        array_dimension
2654        | '(' variable_abstract_ptr ')' array_dimension
2655                { $$ = $2->addArray( $4 ); }
2656        | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
2657                { $$ = $2->addArray( $4 ); }
2658        | '(' variable_abstract_array ')'                                       // redundant parenthesis
2659                { $$ = $2; }
2660        ;
2661
2662variable_abstract_function:
2663        '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2664                { $$ = $2->addParamList( $6 ); }
2665        | '(' variable_abstract_function ')'                            // redundant parenthesis
2666                { $$ = $2; }
2667        ;
2668
2669// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
2670// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
2671
2672new_identifier_parameter_declarator_tuple:                              // CFA
2673        new_identifier_parameter_declarator_no_tuple
2674        | new_abstract_tuple
2675        | type_qualifier_list new_abstract_tuple
2676                { $$ = $2->addQualifiers( $1 ); }
2677        ;
2678
2679new_identifier_parameter_declarator_no_tuple:                   // CFA
2680        new_identifier_parameter_ptr
2681        | new_identifier_parameter_array
2682        ;
2683
2684new_identifier_parameter_ptr:                                                   // CFA
2685        ptrref_operator type_specifier
2686                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2687        | type_qualifier_list ptrref_operator type_specifier
2688                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2689        | ptrref_operator new_abstract_function
2690                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2691        | type_qualifier_list ptrref_operator new_abstract_function
2692                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2693        | ptrref_operator new_identifier_parameter_declarator_tuple
2694                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2695        | type_qualifier_list ptrref_operator new_identifier_parameter_declarator_tuple
2696                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2697        ;
2698
2699new_identifier_parameter_array:                                                 // CFA
2700                // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
2701                // shift/reduce conflict with new-style empty (void) function return type.
2702        '[' ']' type_specifier
2703                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2704        | new_array_parameter_1st_dimension type_specifier
2705                { $$ = $2->addNewArray( $1 ); }
2706        | '[' ']' multi_array_dimension type_specifier
2707                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2708        | new_array_parameter_1st_dimension multi_array_dimension type_specifier
2709                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2710        | multi_array_dimension type_specifier
2711                { $$ = $2->addNewArray( $1 ); }
2712        | '[' ']' new_identifier_parameter_ptr
2713                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2714        | new_array_parameter_1st_dimension new_identifier_parameter_ptr
2715                { $$ = $2->addNewArray( $1 ); }
2716        | '[' ']' multi_array_dimension new_identifier_parameter_ptr
2717                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2718        | new_array_parameter_1st_dimension multi_array_dimension new_identifier_parameter_ptr
2719                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2720        | multi_array_dimension new_identifier_parameter_ptr
2721                { $$ = $2->addNewArray( $1 ); }
2722        ;
2723
2724new_array_parameter_1st_dimension:
2725        '[' push type_qualifier_list '*' pop ']'                        // remaining C99
2726                { $$ = DeclarationNode::newVarArray( $3 ); }
2727        | '[' push type_qualifier_list assignment_expression pop ']'
2728                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2729        | '[' push declaration_qualifier_list assignment_expression pop ']'
2730                // declaration_qualifier_list must be used because of shift/reduce conflict with
2731                // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
2732                // appear in this context.
2733                { $$ = DeclarationNode::newArray( $4, $3, true ); }
2734        | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
2735                { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
2736        ;
2737
2738// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
2739// identifier to which the type applies, e.g.:
2740//
2741//              [int] f( int );                         // abstract variable parameter; no parameter name specified
2742//              [int] f( [int] (int) );         // abstract function-prototype parameter; no parameter name specified
2743//
2744// These rules need LR(3):
2745//
2746//              new_abstract_tuple identifier_or_type_name
2747//              '[' new_parameter_list ']' identifier_or_type_name '(' new_parameter_type_list_opt ')'
2748//
2749// since a function return type can be syntactically identical to a tuple type:
2750//
2751//              [int, int] t;
2752//              [int, int] f( int );
2753//
2754// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
2755// new_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
2756// lookahead. To accomplish this, new_abstract_declarator has an entry point without tuple, and tuple declarations are
2757// duplicated when appearing with new_function_specifier.
2758
2759new_abstract_declarator_tuple:                                                  // CFA
2760        new_abstract_tuple
2761        | type_qualifier_list new_abstract_tuple
2762                { $$ = $2->addQualifiers( $1 ); }
2763        | new_abstract_declarator_no_tuple
2764        ;
2765
2766new_abstract_declarator_no_tuple:                                               // CFA
2767        new_abstract_ptr
2768        | new_abstract_array
2769        ;
2770
2771new_abstract_ptr:                                                                               // CFA
2772        ptrref_operator type_specifier
2773                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2774        | type_qualifier_list ptrref_operator type_specifier
2775                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2776        | ptrref_operator new_abstract_function
2777                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2778        | type_qualifier_list ptrref_operator new_abstract_function
2779                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2780        | ptrref_operator new_abstract_declarator_tuple
2781                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2782        | type_qualifier_list ptrref_operator new_abstract_declarator_tuple
2783                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2784        ;
2785
2786new_abstract_array:                                                                             // CFA
2787                // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
2788                // empty (void) function return type.
2789        '[' ']' type_specifier
2790                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2791        | '[' ']' multi_array_dimension type_specifier
2792                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2793        | multi_array_dimension type_specifier
2794                { $$ = $2->addNewArray( $1 ); }
2795        | '[' ']' new_abstract_ptr
2796                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2797        | '[' ']' multi_array_dimension new_abstract_ptr
2798                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2799        | multi_array_dimension new_abstract_ptr
2800                { $$ = $2->addNewArray( $1 ); }
2801        ;
2802
2803new_abstract_tuple:                                                                             // CFA
2804        '[' push new_abstract_parameter_list pop ']'
2805                { $$ = DeclarationNode::newTuple( $3 ); }
2806        ;
2807
2808new_abstract_function:                                                                  // CFA
2809        '[' ']' '(' new_parameter_type_list_opt ')'
2810                { $$ = DeclarationNode::newFunction( 0, DeclarationNode::newTuple( 0 ), $4, 0 ); }
2811        | new_abstract_tuple '(' push new_parameter_type_list_opt pop ')'
2812                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2813        | new_function_return '(' push new_parameter_type_list_opt pop ')'
2814                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2815        ;
2816
2817// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
2818//    each declaration, and in the specifier-qualifier list in each structure declaration and type name."
2819//
2820// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
2821//    the declaration specifiers in a declaration is an obsolescent feature."
2822//
2823// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
2824//    prototype-format parameter type declarators) is an obsolescent feature."
2825//
2826// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
2827//    declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
2828
2829//************************* MISCELLANEOUS ********************************
2830
2831comma_opt:                                                                                              // redundant comma
2832        // empty
2833        | ','
2834        ;
2835
2836assignment_opt:
2837        // empty
2838                { $$ = 0; }
2839        | '=' assignment_expression
2840                { $$ = $2; }
2841        ;
2842
2843%%
2844// ----end of grammar----
2845
2846extern char *yytext;
2847
2848void yyerror( const char * ) {
2849        std::cout << "Error ";
2850        if ( yyfilename ) {
2851                std::cout << "in file " << yyfilename << " ";
2852        } // if
2853        std::cout << "at line " << yylineno << " reading token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << "\"" << std::endl;
2854}
2855
2856// Local Variables: //
2857// mode: c++ //
2858// tab-width: 4 //
2859// compile-command: "make install" //
2860// End: //
Note: See TracBrowser for help on using the repository browser.