source: src/Parser/parser.yy @ 68a8ba2a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 68a8ba2a was 68a8ba2a, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

fix destructor statement parsing rule to pass its argument unchanged (no address-of)

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