source: src/Parser/parser.yy @ 984dce6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 984dce6 was 984dce6, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

only implicitly generate typedef for structures if name not in use and overwrite typedef name if explicit name appears, upate parser symbol table

  • Property mode set to 100644
File size: 102.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// cfa.y --
8//
9// Author           : Peter A. Buhr
10// Created On       : Sat Sep  1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Mar 21 18:18:34 2016
13// Update Count     : 1497
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                { $$ = 0; }
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 *)(new CompositeExprNode( new OperatorNode( OperatorNode::AddressOf ), $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;
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 );
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 ) );
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                { $$ = DeclarationNode::newAggregate( $1, $2, 0, 0 ); }
1441        | aggregate_key no_attr_identifier_or_type_name '{' field_declaration_list '}'
1442                { $$ = DeclarationNode::newAggregate( $1, $2, 0, $4 ); }
1443        | aggregate_key '(' type_name_list ')' '{' field_declaration_list '}' // CFA
1444                { $$ = DeclarationNode::newAggregate( $1, 0, $3, $6 ); }
1445        | aggregate_key typegen_name                                            // CFA, S/R conflict
1446                { $$ = $2; }
1447        ;
1448
1449aggregate_key:
1450        STRUCT attribute_list_opt
1451                { $$ = DeclarationNode::Struct; }
1452        | UNION attribute_list_opt
1453                { $$ = DeclarationNode::Union; }
1454        ;
1455
1456field_declaration_list:
1457        field_declaration
1458                { $$ = $1; }
1459        | field_declaration_list field_declaration
1460                { $$ = $1->appendList( $2 ); }
1461        ;
1462
1463field_declaration:
1464        new_field_declaring_list ';'                                            // CFA, new style field declaration
1465        | EXTENSION new_field_declaring_list ';'                        // GCC
1466                { $$ = $2; }
1467        | field_declaring_list ';'
1468        | EXTENSION field_declaring_list ';'                            // GCC
1469                { $$ = $2; }
1470        ;
1471
1472new_field_declaring_list:                                                               // CFA, new style field declaration
1473        new_abstract_declarator_tuple                                           // CFA, no field name
1474        | new_abstract_declarator_tuple no_attr_identifier_or_type_name
1475                { $$ = $1->addName( $2 ); }
1476        | new_field_declaring_list ',' no_attr_identifier_or_type_name
1477                { $$ = $1->appendList( $1->cloneType( $3 ) ); }
1478        | new_field_declaring_list ','                                          // CFA, no field name
1479                { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1480        ;
1481
1482field_declaring_list:
1483        type_specifier field_declarator
1484                { $$ = $2->addType( $1 ); }
1485        | field_declaring_list ',' attribute_list_opt field_declarator
1486                { $$ = $1->appendList( $1->cloneBaseType( $4 ) ); }
1487        ;
1488
1489field_declarator:
1490        // empty
1491                { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
1492        | bit_subrange_size                                                                     // no field name
1493                { $$ = DeclarationNode::newBitfield( $1 ); }
1494        | variable_declarator bit_subrange_size_opt
1495                // A semantic check is required to ensure bit_subrange only appears on base type int.
1496                { $$ = $1->addBitfield( $2 ); }
1497        | type_redeclarator bit_subrange_size_opt
1498                // A semantic check is required to ensure bit_subrange only appears on base type int.
1499                { $$ = $1->addBitfield( $2 ); }
1500        | variable_abstract_declarator                                          // CFA, no field name
1501        ;
1502
1503bit_subrange_size_opt:
1504        // empty
1505                { $$ = 0; }
1506        | bit_subrange_size
1507                { $$ = $1; }
1508        ;
1509
1510bit_subrange_size:
1511        ':' constant_expression
1512                { $$ = $2; }
1513        ;
1514
1515enum_key:
1516        ENUM attribute_list_opt
1517        ;
1518
1519enum_name:
1520        enum_key '{' enumerator_list comma_opt '}'
1521                { $$ = DeclarationNode::newEnum( 0, $3 ); }
1522        | enum_key no_attr_identifier_or_type_name '{' enumerator_list comma_opt '}'
1523                { $$ = DeclarationNode::newEnum( $2, $4 ); }
1524        | enum_key no_attr_identifier_or_type_name
1525                { $$ = DeclarationNode::newEnum( $2, 0 ); }
1526        ;
1527
1528enumerator_list:
1529        no_attr_identifier_or_type_name enumerator_value_opt
1530                { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
1531        | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
1532                { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1533        ;
1534
1535enumerator_value_opt:
1536        // empty
1537                { $$ = 0; }
1538        | '=' constant_expression
1539                { $$ = $2; }
1540        ;
1541
1542// Minimum of one parameter after which ellipsis is allowed only at the end.
1543
1544new_parameter_type_list_opt:                                                    // CFA
1545        // empty
1546                { $$ = 0; }
1547        | new_parameter_type_list
1548        ;
1549
1550new_parameter_type_list:                                                                // CFA, abstract + real
1551        new_abstract_parameter_list
1552        | new_parameter_list
1553        | new_parameter_list pop ',' push new_abstract_parameter_list
1554                { $$ = $1->appendList( $5 ); }
1555        | new_abstract_parameter_list pop ',' push ELLIPSIS
1556                { $$ = $1->addVarArgs(); }
1557        | new_parameter_list pop ',' push ELLIPSIS
1558                { $$ = $1->addVarArgs(); }
1559        ;
1560
1561new_parameter_list:                                                                             // CFA
1562                // To obtain LR(1) between new_parameter_list and new_abstract_tuple, the last new_abstract_parameter_list is
1563                // factored out from new_parameter_list, flattening the rules to get lookahead to the ']'.
1564        new_parameter_declaration
1565        | new_abstract_parameter_list pop ',' push new_parameter_declaration
1566                { $$ = $1->appendList( $5 ); }
1567        | new_parameter_list pop ',' push new_parameter_declaration
1568                { $$ = $1->appendList( $5 ); }
1569        | new_parameter_list pop ',' push new_abstract_parameter_list pop ',' push new_parameter_declaration
1570                { $$ = $1->appendList( $5 )->appendList( $9 ); }
1571        ;
1572
1573new_abstract_parameter_list:                                                    // CFA, new & old style abstract
1574        new_abstract_parameter_declaration
1575        | new_abstract_parameter_list pop ',' push new_abstract_parameter_declaration
1576                { $$ = $1->appendList( $5 ); }
1577        ;
1578
1579parameter_type_list_opt:
1580        // empty
1581                { $$ = 0; }
1582        | parameter_type_list
1583        ;
1584
1585parameter_type_list:
1586        parameter_list
1587        | parameter_list pop ',' push ELLIPSIS
1588                { $$ = $1->addVarArgs(); }
1589        ;
1590
1591parameter_list:                                                                                 // abstract + real
1592        abstract_parameter_declaration
1593        | parameter_declaration
1594        | parameter_list pop ',' push abstract_parameter_declaration
1595                { $$ = $1->appendList( $5 ); }
1596        | parameter_list pop ',' push parameter_declaration
1597                { $$ = $1->appendList( $5 ); }
1598        ;
1599
1600// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
1601// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
1602
1603new_parameter_declaration:                                                              // CFA, new & old style parameter declaration
1604        parameter_declaration
1605        | new_identifier_parameter_declarator_no_tuple identifier_or_type_name assignment_opt
1606                { $$ = $1->addName( $2 ); }
1607        | new_abstract_tuple identifier_or_type_name assignment_opt
1608                // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1609                { $$ = $1->addName( $2 ); }
1610        | type_qualifier_list new_abstract_tuple identifier_or_type_name assignment_opt
1611                { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
1612        | new_function_specifier
1613        ;
1614
1615new_abstract_parameter_declaration:                                             // CFA, new & old style parameter declaration
1616        abstract_parameter_declaration
1617        | new_identifier_parameter_declarator_no_tuple
1618        | new_abstract_tuple
1619                // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1620        | type_qualifier_list new_abstract_tuple
1621                { $$ = $2->addQualifiers( $1 ); }
1622        | new_abstract_function
1623        ;
1624
1625parameter_declaration:
1626        declaration_specifier identifier_parameter_declarator assignment_opt
1627                {
1628                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1629                        $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
1630                }
1631        | declaration_specifier type_parameter_redeclarator assignment_opt
1632                {
1633                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1634                        $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
1635                }
1636        ;
1637
1638abstract_parameter_declaration:
1639        declaration_specifier
1640        | declaration_specifier abstract_parameter_declarator
1641                { $$ = $2->addType( $1 ); }
1642        ;
1643
1644// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
1645// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
1646// identifiers.  The ANSI-style parameter-list can redefine a typedef name.
1647
1648identifier_list:                                                                                // K&R-style parameter list => no types
1649        no_attr_identifier
1650                { $$ = DeclarationNode::newName( $1 ); }
1651        | identifier_list ',' no_attr_identifier
1652                { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
1653        ;
1654
1655identifier_or_type_name:
1656        identifier
1657        | TYPEDEFname
1658        | TYPEGENname
1659        ;
1660
1661no_01_identifier_or_type_name:
1662        no_01_identifier
1663        | TYPEDEFname
1664        | TYPEGENname
1665        ;
1666
1667no_attr_identifier_or_type_name:
1668        no_attr_identifier
1669        | TYPEDEFname
1670        | TYPEGENname
1671        ;
1672
1673type_name_no_function:                                                                  // sizeof, alignof, cast (constructor)
1674        new_abstract_declarator_tuple                                           // CFA
1675        | type_specifier
1676        | type_specifier variable_abstract_declarator
1677                { $$ = $2->addType( $1 ); }
1678        ;
1679
1680type_name:                                                                                              // typeof, assertion
1681        new_abstract_declarator_tuple                                           // CFA
1682        | new_abstract_function                                                         // CFA
1683        | type_specifier
1684        | type_specifier abstract_declarator
1685                { $$ = $2->addType( $1 ); }
1686        ;
1687
1688initializer_opt:
1689        // empty
1690                { $$ = 0; }
1691        | '=' initializer
1692                { $$ = $2; }
1693        | ATassign initializer
1694                { $$ = $2; }
1695        ;
1696
1697initializer:
1698        assignment_expression                                           { $$ = new InitializerNode( $1 ); }
1699        | '{' initializer_list comma_opt '}'            { $$ = new InitializerNode( $2, true ); }
1700        ;
1701
1702initializer_list:
1703        // empty
1704                { $$ = 0; }
1705        | initializer
1706        | designation initializer                                       { $$ = $2->set_designators( $1 ); }
1707        | initializer_list ',' initializer                      { $$ = (InitializerNode *)( $1->set_link( $3 ) ); }
1708        | initializer_list ',' designation initializer
1709                { $$ = (InitializerNode *)( $1->set_link( $4->set_designators( $3 ) ) ); }
1710        ;
1711
1712// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
1713// '=' to separator the designator from the initializer value, as in:
1714//
1715//              int x[10] = { [1] = 3 };
1716//
1717// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment.  To disambiguate this case, CFA
1718// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
1719// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
1720// shift/reduce conflicts
1721
1722designation:
1723        designator_list ':'                                                                     // C99, CFA uses ":" instead of "="
1724        | no_attr_identifier_or_type_name ':'                           // GCC, field name
1725                { $$ = new VarRefNode( $1 ); }
1726        ;
1727
1728designator_list:                                                                                // C99
1729        designator
1730        | designator_list designator
1731                { $$ = (ExpressionNode *)( $1->set_link( $2 )); }
1732        //| designator_list designator                                          { $$ = new CompositeExprNode( $1, $2 ); }
1733        ;
1734
1735designator:
1736        // lexer ambiguity: designator ".0" is floating-point constant or designator for name 0
1737        // only ".0" and ".1" allowed => semantic check
1738        FLOATINGconstant
1739                { $$ = new DesignatorNode( new VarRefNode( $1 ) ); }
1740        | '.' no_attr_identifier_or_type_name                           // C99, field name
1741                { $$ = new DesignatorNode( new VarRefNode( $2 ) ); }
1742        | '[' push assignment_expression pop ']'                        // C99, single array element
1743                // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
1744                { $$ = new DesignatorNode( $3, true ); }
1745        | '[' push subrange pop ']'                                                     // CFA, multiple array elements
1746                { $$ = new DesignatorNode( $3, true ); }
1747        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
1748                { $$ = new DesignatorNode( new CompositeExprNode( new OperatorNode( OperatorNode::Range ), $3, $5 ), true ); }
1749        | '.' '[' push field_list pop ']'                                       // CFA, tuple field selector
1750                { $$ = new DesignatorNode( $4 ); }
1751        ;
1752
1753// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
1754// rather than an object-oriented type system. This required four groups of extensions:
1755//
1756// Overloading: function, data, and operator identifiers may be overloaded.
1757//
1758// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
1759//     and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
1760//     definitions of new types. Type declarations with storage class "extern" provide opaque types.
1761//
1762// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
1763//     site. A polymorphic function is not a template; it is a function, with an address and a type.
1764//
1765// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
1766//     types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
1767//     hierarchies. Unlike classes, they can define relationships between types.  Assertions declare that a type or
1768//     types provide the operations declared by a specification.  Assertions are normally used to declare requirements
1769//     on type arguments of polymorphic functions.
1770
1771typegen_declaration_specifier:                                                  // CFA
1772        typegen_type_specifier
1773        | declaration_qualifier_list typegen_type_specifier
1774                { $$ = $2->addQualifiers( $1 ); }
1775        | typegen_declaration_specifier storage_class           // remaining OBSOLESCENT (see 2)
1776                { $$ = $1->addQualifiers( $2 ); }
1777        | typegen_declaration_specifier storage_class type_qualifier_list
1778                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1779        ;
1780
1781typegen_type_specifier:                                                                 // CFA
1782        typegen_name
1783        | type_qualifier_list typegen_name
1784                { $$ = $2->addQualifiers( $1 ); }
1785        | typegen_type_specifier type_qualifier
1786                { $$ = $1->addQualifiers( $2 ); }
1787        ;
1788
1789typegen_name:                                                                                   // CFA
1790        TYPEGENname '(' type_name_list ')'
1791                { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
1792        ;
1793
1794type_parameter_list:                                                                    // CFA
1795        type_parameter assignment_opt
1796        | type_parameter_list ',' type_parameter assignment_opt
1797                { $$ = $1->appendList( $3 ); }
1798        ;
1799
1800type_parameter:                                                                                 // CFA
1801        type_class no_attr_identifier_or_type_name
1802                { typedefTable.addToEnclosingScope( *$2, TypedefTable::TD ); }
1803          assertion_list_opt
1804                { $$ = DeclarationNode::newTypeParam( $1, $2 )->addAssertions( $4 ); }
1805        | type_specifier identifier_parameter_declarator
1806        ;
1807
1808type_class:                                                                                             // CFA
1809        OTYPE
1810                { $$ = DeclarationNode::Type; }
1811        | DTYPE
1812                { $$ = DeclarationNode::Ftype; }
1813        | FTYPE
1814                { $$ = DeclarationNode::Dtype; }
1815        ;
1816
1817assertion_list_opt:                                                                             // CFA
1818        // empty
1819                { $$ = 0; }
1820        | assertion_list_opt assertion
1821                { $$ = $1 == 0 ? $2 : $1->appendList( $2 ); }
1822        ;
1823
1824assertion:                                                                                              // CFA
1825        '|' no_attr_identifier_or_type_name '(' type_name_list ')'
1826                {
1827                        typedefTable.openTrait( *$2 );
1828                        $$ = DeclarationNode::newTraitUse( $2, $4 );
1829                }
1830        | '|' '{' push trait_declaration_list '}'
1831                { $$ = $4; }
1832        | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list '}' '(' type_name_list ')'
1833                { $$ = 0; }
1834        ;
1835
1836type_name_list:                                                                                 // CFA
1837        type_name
1838                { $$ = new TypeValueNode( $1 ); }
1839        | assignment_expression
1840        | type_name_list ',' type_name
1841                { $$ = (ExpressionNode *)( $1->set_link( new TypeValueNode( $3 ))); }
1842        | type_name_list ',' assignment_expression
1843                { $$ = (ExpressionNode *)( $1->set_link( $3 )); }
1844        ;
1845
1846type_declaring_list:                                                                    // CFA
1847        OTYPE type_declarator
1848                { $$ = $2; }
1849        | storage_class_list OTYPE type_declarator
1850                { $$ = $3->addQualifiers( $1 ); }
1851        | type_declaring_list ',' type_declarator
1852                { $$ = $1->appendList( $3->copyStorageClasses( $1 ) ); }
1853        ;
1854
1855type_declarator:                                                                                // CFA
1856        type_declarator_name assertion_list_opt
1857                { $$ = $1->addAssertions( $2 ); }
1858        | type_declarator_name assertion_list_opt '=' type_name
1859                { $$ = $1->addAssertions( $2 )->addType( $4 ); }
1860        ;
1861
1862type_declarator_name:                                                                   // CFA
1863        no_attr_identifier_or_type_name
1864                {
1865                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
1866                        $$ = DeclarationNode::newTypeDecl( $1, 0 );
1867                }
1868        | no_01_identifier_or_type_name '(' push type_parameter_list pop ')'
1869                {
1870                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
1871                        $$ = DeclarationNode::newTypeDecl( $1, $4 );
1872                }
1873        ;
1874
1875trait_specifier:                                                                                // CFA
1876        TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{' '}'
1877                {
1878                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
1879                        $$ = DeclarationNode::newTrait( $2, $5, 0 );
1880                }
1881        | TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{'
1882                {
1883                        typedefTable.enterTrait( *$2 );
1884                        typedefTable.enterScope();
1885                }
1886          trait_declaration_list '}'
1887                {
1888                        typedefTable.leaveTrait();
1889                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
1890                        $$ = DeclarationNode::newTrait( $2, $5, $10 );
1891                }
1892        ;
1893
1894trait_declaration_list:                                                         // CFA
1895        trait_declaration
1896        | trait_declaration_list push trait_declaration
1897                { $$ = $1->appendList( $3 ); }
1898        ;
1899
1900trait_declaration:                                                                      // CFA
1901        new_trait_declaring_list pop ';'
1902        | trait_declaring_list pop ';'
1903        ;
1904
1905new_trait_declaring_list:                                                               // CFA
1906        new_variable_specifier
1907                {
1908                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1909                        $$ = $1;
1910                }
1911        | new_function_specifier
1912                {
1913                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1914                        $$ = $1;
1915                }
1916        | new_trait_declaring_list pop ',' push identifier_or_type_name
1917                {
1918                        typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
1919                        $$ = $1->appendList( $1->cloneType( $5 ) );
1920                }
1921        ;
1922
1923trait_declaring_list:                                                                   // CFA
1924        type_specifier declarator
1925                {
1926                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1927                        $$ = $2->addType( $1 );
1928                }
1929        | trait_declaring_list pop ',' push declarator
1930                {
1931                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1932                        $$ = $1->appendList( $1->cloneBaseType( $5 ) );
1933                }
1934        ;
1935
1936//***************************** EXTERNAL DEFINITIONS *****************************
1937
1938translation_unit:
1939        // empty
1940                {}                                                                                              // empty input file
1941        | external_definition_list
1942                {
1943                        if ( theTree ) {
1944                                theTree->appendList( $1 );
1945                        } else {
1946                                theTree = $1;
1947                        }
1948                }
1949        ;
1950
1951external_definition_list:
1952        external_definition
1953        | external_definition_list push external_definition
1954                { $$ = ( $1 != NULL ) ? $1->appendList( $3 ) : $3; }
1955        ;
1956
1957external_definition_list_opt:
1958        // empty
1959                { $$ = 0; }
1960        | external_definition_list
1961        ;
1962
1963external_definition:
1964        declaration
1965        | external_function_definition
1966        | asm_statement                                                                         // GCC, global assembler statement
1967                {}
1968        | EXTERN STRINGliteral
1969                {
1970                        linkageStack.push( linkage );
1971                        linkage = LinkageSpec::fromString( *$2 );
1972                }
1973          '{' external_definition_list_opt '}'                          // C++-style linkage specifier
1974                {
1975                        linkage = linkageStack.top();
1976                        linkageStack.pop();
1977                        $$ = $5;
1978                }
1979        | EXTENSION external_definition
1980                { $$ = $2; }
1981        ;
1982
1983external_function_definition:
1984        function_definition
1985                // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
1986                // code with functions missing a type-specifier on the return type.  Parsing is possible because
1987                // function_definition does not appear in the context of an expression (nested functions would preclude this
1988                // concession). A function prototype declaration must still have a type_specifier.  OBSOLESCENT (see 1)
1989        | function_declarator compound_statement
1990                {
1991                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1992                        typedefTable.leaveScope();
1993                        $$ = $1->addFunctionBody( $2 );
1994                }
1995        | old_function_declarator push old_declaration_list_opt compound_statement
1996                {
1997                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1998                        typedefTable.leaveScope();
1999                        $$ = $1->addOldDeclList( $3 )->addFunctionBody( $4 );
2000                }
2001        ;
2002
2003function_definition:
2004        new_function_declaration compound_statement                     // CFA
2005                {
2006                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2007                        typedefTable.leaveScope();
2008                        $$ = $1->addFunctionBody( $2 );
2009                }
2010        | declaration_specifier function_declarator compound_statement
2011                {
2012                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2013                        typedefTable.leaveScope();
2014                        $$ = $2->addFunctionBody( $3 )->addType( $1 );
2015                }
2016        | type_qualifier_list function_declarator compound_statement
2017                {
2018                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2019                        typedefTable.leaveScope();
2020                        $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
2021                }
2022        | declaration_qualifier_list function_declarator compound_statement
2023                {
2024                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2025                        typedefTable.leaveScope();
2026                        $$ = $2->addFunctionBody( $3 )->addQualifiers( $1 );
2027                }
2028        | declaration_qualifier_list type_qualifier_list function_declarator compound_statement
2029                {
2030                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2031                        typedefTable.leaveScope();
2032                        $$ = $3->addFunctionBody( $4 )->addQualifiers( $2 )->addQualifiers( $1 );
2033                }
2034
2035                // Old-style K&R function definition, OBSOLESCENT (see 4)
2036        | declaration_specifier old_function_declarator push old_declaration_list_opt compound_statement
2037                {
2038                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2039                        typedefTable.leaveScope();
2040                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addType( $1 );
2041                }
2042        | type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2043                {
2044                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2045                        typedefTable.leaveScope();
2046                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2047                }
2048
2049                // Old-style K&R function definition with "implicit int" type_specifier, OBSOLESCENT (see 4)
2050        | declaration_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2051                {
2052                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2053                        typedefTable.leaveScope();
2054                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $5 )->addQualifiers( $1 );
2055                }
2056        | declaration_qualifier_list type_qualifier_list old_function_declarator push old_declaration_list_opt compound_statement
2057                {
2058                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2059                        typedefTable.leaveScope();
2060                        $$ = $3->addOldDeclList( $5 )->addFunctionBody( $6 )->addQualifiers( $2 )->addQualifiers( $1 );
2061                }
2062        ;
2063
2064declarator:
2065        variable_declarator
2066        | function_declarator
2067        | type_redeclarator
2068        ;
2069
2070subrange:
2071        constant_expression '~' constant_expression                     // CFA, integer subrange
2072                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Range ), $1, $3 ); }
2073        ;
2074
2075asm_name_opt:                                                                                   // GCC
2076        // empty
2077        | ASM '(' string_literal_list ')' attribute_list_opt
2078        ;
2079
2080attribute_list_opt:                                                                             // GCC
2081        // empty
2082                { $$ = 0; }
2083        | attribute_list
2084        ;
2085
2086attribute_list:                                                                                 // GCC
2087        attribute
2088        | attribute_list attribute
2089                { $$ = $2->addQualifiers( $1 ); }
2090        ;
2091
2092attribute:                                                                                              // GCC
2093        ATTRIBUTE '(' '(' attribute_parameter_list ')' ')'
2094        //              { $$ = DeclarationNode::newQualifier( DeclarationNode::Attribute ); }
2095                { $$ = 0; }
2096        ;
2097
2098attribute_parameter_list:                                                               // GCC
2099        attrib
2100        | attribute_parameter_list ',' attrib
2101        ;
2102
2103attrib:                                                                                                 // GCC
2104        // empty
2105        | any_word
2106        | any_word '(' comma_expression_opt ')'
2107        ;
2108
2109any_word:                                                                                               // GCC
2110        identifier_or_type_name {}
2111        | storage_class_name {}
2112        | basic_type_name {}
2113        | type_qualifier {}
2114        ;
2115
2116// ============================================================================
2117// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2118// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2119// in:
2120//
2121//              int (*f())[10] { ... };
2122//              ... (*f())[3] += 1;             // definition mimics usage
2123//
2124// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2125// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
2126// ============================================================================
2127
2128// ----------------------------------------------------------------------------
2129// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2130// to define a variable or function prototype, e.g.:
2131//
2132//              valid declaration               invalid definition
2133//              -----------------               ------------------
2134//              int f;                                  int f {}
2135//              int *f;                                 int *f {}
2136//              int f[10];                              int f[10] {}
2137//              int (*f)(int);                  int (*f)(int) {}
2138//
2139// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2140// variable_declarator and function_declarator.
2141// ----------------------------------------------------------------------------
2142
2143// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2144// declaring an array of functions versus a pointer to an array of functions.
2145
2146variable_declarator:
2147        paren_identifier attribute_list_opt
2148                { $$ = $1->addQualifiers( $2 ); }
2149        | variable_ptr
2150        | variable_array attribute_list_opt
2151                { $$ = $1->addQualifiers( $2 ); }
2152        | variable_function attribute_list_opt
2153                { $$ = $1->addQualifiers( $2 ); }
2154        ;
2155
2156paren_identifier:
2157        identifier
2158                {
2159                        typedefTable.setNextIdentifier( *$1 );
2160                        $$ = DeclarationNode::newName( $1 );
2161                }
2162        | '(' paren_identifier ')'                                                      // redundant parenthesis
2163                { $$ = $2; }
2164        ;
2165
2166variable_ptr:
2167        '*' variable_declarator
2168                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2169        | '*' type_qualifier_list variable_declarator
2170                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2171        | '(' variable_ptr ')'
2172                { $$ = $2; }
2173        ;
2174
2175variable_array:
2176        paren_identifier array_dimension
2177                { $$ = $1->addArray( $2 ); }
2178        | '(' variable_ptr ')' array_dimension
2179                { $$ = $2->addArray( $4 ); }
2180        | '(' variable_array ')' multi_array_dimension          // redundant parenthesis
2181                { $$ = $2->addArray( $4 ); }
2182        | '(' variable_array ')'                                                        // redundant parenthesis
2183                { $$ = $2; }
2184        ;
2185
2186variable_function:
2187        '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2188                { $$ = $2->addParamList( $6 ); }
2189        | '(' variable_function ')'                                                     // redundant parenthesis
2190                { $$ = $2; }
2191        ;
2192
2193// This pattern parses a function declarator that is not redefining a typedef name. Because functions cannot be nested,
2194// there is no context where a function definition can redefine a typedef name. To allow nested functions requires
2195// further separation of variable and function declarators in type_redeclarator.  The pattern precludes returning
2196// arrays and functions versus pointers to arrays and functions.
2197
2198function_declarator:
2199        function_no_ptr attribute_list_opt
2200                { $$ = $1->addQualifiers( $2 ); }
2201        | function_ptr
2202        | function_array attribute_list_opt
2203                { $$ = $1->addQualifiers( $2 ); }
2204        ;
2205
2206function_no_ptr:
2207        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2208                { $$ = $1->addParamList( $4 ); }
2209        | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2210                { $$ = $2->addParamList( $6 ); }
2211        | '(' function_no_ptr ')'                                                       // redundant parenthesis
2212                { $$ = $2; }
2213        ;
2214
2215function_ptr:
2216        '*' function_declarator
2217                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2218        | '*' type_qualifier_list function_declarator
2219                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2220        | '(' function_ptr ')'
2221                { $$ = $2; }
2222        ;
2223
2224function_array:
2225        '(' function_ptr ')' array_dimension
2226                { $$ = $2->addArray( $4 ); }
2227        | '(' function_array ')' multi_array_dimension          // redundant parenthesis
2228                { $$ = $2->addArray( $4 ); }
2229        | '(' function_array ')'                                                        // redundant parenthesis
2230                { $$ = $2; }
2231        ;
2232
2233// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4) that is not redefining a typedef name
2234// (see function_declarator for additional comments). The pattern precludes returning arrays and functions versus
2235// pointers to arrays and functions.
2236
2237old_function_declarator:
2238        old_function_no_ptr
2239        | old_function_ptr
2240        | old_function_array
2241        ;
2242
2243old_function_no_ptr:
2244        paren_identifier '(' identifier_list ')'                        // function_declarator handles empty parameter
2245                { $$ = $1->addIdList( $3 ); }
2246        | '(' old_function_ptr ')' '(' identifier_list ')'
2247                { $$ = $2->addIdList( $5 ); }
2248        | '(' old_function_no_ptr ')'                                           // redundant parenthesis
2249                { $$ = $2; }
2250        ;
2251
2252old_function_ptr:
2253        '*' old_function_declarator
2254                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2255        | '*' type_qualifier_list old_function_declarator
2256                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2257        | '(' old_function_ptr ')'
2258                { $$ = $2; }
2259        ;
2260
2261old_function_array:
2262        '(' old_function_ptr ')' array_dimension
2263                { $$ = $2->addArray( $4 ); }
2264        | '(' old_function_array ')' multi_array_dimension      // redundant parenthesis
2265                { $$ = $2->addArray( $4 ); }
2266        | '(' old_function_array ')'                                            // redundant parenthesis
2267                { $$ = $2; }
2268        ;
2269
2270// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
2271//
2272//              typedef int foo;
2273//              {
2274//                 int foo; // redefine typedef name in new scope
2275//              }
2276//
2277// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2278// and functions versus pointers to arrays and functions.
2279
2280type_redeclarator:
2281        paren_type attribute_list_opt
2282                { $$ = $1->addQualifiers( $2 ); }
2283        | type_ptr
2284        | type_array attribute_list_opt
2285                { $$ = $1->addQualifiers( $2 ); }
2286        | type_function attribute_list_opt
2287                { $$ = $1->addQualifiers( $2 ); }
2288        ;
2289
2290paren_type:
2291        typedef
2292        | '(' paren_type ')'
2293                { $$ = $2; }
2294        ;
2295
2296type_ptr:
2297        '*' type_redeclarator
2298                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2299        | '*' type_qualifier_list type_redeclarator
2300                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2301        | '(' type_ptr ')'
2302                { $$ = $2; }
2303        ;
2304
2305type_array:
2306        paren_type array_dimension
2307                { $$ = $1->addArray( $2 ); }
2308        | '(' type_ptr ')' array_dimension
2309                { $$ = $2->addArray( $4 ); }
2310        | '(' type_array ')' multi_array_dimension                      // redundant parenthesis
2311                { $$ = $2->addArray( $4 ); }
2312        | '(' type_array ')'                                                            // redundant parenthesis
2313                { $$ = $2; }
2314        ;
2315
2316type_function:
2317        paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2318                { $$ = $1->addParamList( $4 ); }
2319        | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2320                { $$ = $2->addParamList( $6 ); }
2321        | '(' type_function ')'                                                         // redundant parenthesis
2322                { $$ = $2; }
2323        ;
2324
2325// This pattern parses a declaration for a parameter variable or function prototype that is not redefining a typedef
2326// name and allows the C99 array options, which can only appear in a parameter list.  The pattern precludes declaring an
2327// array of functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to
2328// arrays and functions.
2329
2330identifier_parameter_declarator:
2331        paren_identifier attribute_list_opt
2332                { $$ = $1->addQualifiers( $2 ); }
2333        | identifier_parameter_ptr
2334        | identifier_parameter_array attribute_list_opt
2335                { $$ = $1->addQualifiers( $2 ); }
2336        | identifier_parameter_function attribute_list_opt
2337                { $$ = $1->addQualifiers( $2 ); }
2338        ;
2339
2340identifier_parameter_ptr:
2341        '*' identifier_parameter_declarator
2342                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2343        | '*' type_qualifier_list identifier_parameter_declarator
2344                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2345        | '(' identifier_parameter_ptr ')'
2346                { $$ = $2; }
2347        ;
2348
2349identifier_parameter_array:
2350        paren_identifier array_parameter_dimension
2351                { $$ = $1->addArray( $2 ); }
2352        | '(' identifier_parameter_ptr ')' array_dimension
2353                { $$ = $2->addArray( $4 ); }
2354        | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2355                { $$ = $2->addArray( $4 ); }
2356        | '(' identifier_parameter_array ')'                            // redundant parenthesis
2357                { $$ = $2; }
2358        ;
2359
2360identifier_parameter_function:
2361        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2362                { $$ = $1->addParamList( $4 ); }
2363        | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2364                { $$ = $2->addParamList( $6 ); }
2365        | '(' identifier_parameter_function ')'                         // redundant parenthesis
2366                { $$ = $2; }
2367        ;
2368
2369// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2370// e.g.:
2371//
2372//              typedef int foo;
2373//              int f( int foo ); // redefine typedef name in new scope
2374//
2375// and allows the C99 array options, which can only appear in a parameter list.  In addition, the pattern handles the
2376// special meaning of parenthesis around a typedef name:
2377//
2378//              ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2379//              parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2380//              not as redundant parentheses around the identifier."
2381//
2382// which precludes the following cases:
2383//
2384//              typedef float T;
2385//              int f( int ( T [5] ) );                                 // see abstract_parameter_declarator
2386//              int g( int ( T ( int ) ) );                             // see abstract_parameter_declarator
2387//              int f( int f1( T a[5] ) );                              // see identifier_parameter_declarator
2388//              int g( int g1( T g2( int p ) ) );               // see identifier_parameter_declarator
2389//
2390// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2391// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2392// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2393// functions.
2394
2395type_parameter_redeclarator:
2396        typedef attribute_list_opt
2397                { $$ = $1->addQualifiers( $2 ); }
2398        | type_parameter_ptr
2399        | type_parameter_array attribute_list_opt
2400                { $$ = $1->addQualifiers( $2 ); }
2401        | type_parameter_function attribute_list_opt
2402                { $$ = $1->addQualifiers( $2 ); }
2403        ;
2404
2405typedef:
2406        TYPEDEFname
2407                {
2408                        typedefTable.setNextIdentifier( *$1 );
2409                        $$ = DeclarationNode::newName( $1 );
2410                }
2411        | TYPEGENname
2412                {
2413                        typedefTable.setNextIdentifier( *$1 );
2414                        $$ = DeclarationNode::newName( $1 );
2415                }
2416        ;
2417
2418type_parameter_ptr:
2419        '*' type_parameter_redeclarator
2420                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2421        | '*' type_qualifier_list type_parameter_redeclarator
2422                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2423        | '(' type_parameter_ptr ')'
2424                { $$ = $2; }
2425        ;
2426
2427type_parameter_array:
2428        typedef array_parameter_dimension
2429                { $$ = $1->addArray( $2 ); }
2430        | '(' type_parameter_ptr ')' array_parameter_dimension
2431                { $$ = $2->addArray( $4 ); }
2432        ;
2433
2434type_parameter_function:
2435        typedef '(' push parameter_type_list_opt pop ')'        // empty parameter list OBSOLESCENT (see 3)
2436                { $$ = $1->addParamList( $4 ); }
2437        | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2438                { $$ = $2->addParamList( $6 ); }
2439        ;
2440
2441// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2442// which the type applies, e.g.:
2443//
2444//              sizeof( int );
2445//              sizeof( int [10] );
2446//
2447// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2448// and functions versus pointers to arrays and functions.
2449
2450abstract_declarator:
2451        abstract_ptr
2452        | abstract_array attribute_list_opt
2453                { $$ = $1->addQualifiers( $2 ); }
2454        | abstract_function attribute_list_opt
2455                { $$ = $1->addQualifiers( $2 ); }
2456        ;
2457
2458abstract_ptr:
2459        '*'
2460                { $$ = DeclarationNode::newPointer( 0 ); }
2461        | '*' type_qualifier_list
2462                { $$ = DeclarationNode::newPointer( $2 ); }
2463        | '*' abstract_declarator
2464                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2465        | '*' type_qualifier_list abstract_declarator
2466                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2467        | '(' abstract_ptr ')'
2468                { $$ = $2; }
2469        ;
2470
2471abstract_array:
2472        array_dimension
2473        | '(' abstract_ptr ')' array_dimension
2474                { $$ = $2->addArray( $4 ); }
2475        | '(' abstract_array ')' multi_array_dimension          // redundant parenthesis
2476                { $$ = $2->addArray( $4 ); }
2477        | '(' abstract_array ')'                                                        // redundant parenthesis
2478                { $$ = $2; }
2479        ;
2480
2481abstract_function:
2482        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2483                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2484        | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2485                { $$ = $2->addParamList( $6 ); }
2486        | '(' abstract_function ')'                                                     // redundant parenthesis
2487                { $$ = $2; }
2488        ;
2489
2490array_dimension:
2491                // Only the first dimension can be empty.
2492        '[' ']'
2493                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2494        | '[' ']' multi_array_dimension
2495                { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
2496        | multi_array_dimension
2497        ;
2498
2499multi_array_dimension:
2500        '[' push assignment_expression pop ']'
2501                { $$ = DeclarationNode::newArray( $3, 0, false ); }
2502        | '[' push '*' pop ']'                                                          // C99
2503                { $$ = DeclarationNode::newVarArray( 0 ); }
2504        | multi_array_dimension '[' push assignment_expression pop ']'
2505                { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2506        | multi_array_dimension '[' push '*' pop ']'            // C99
2507                { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2508        ;
2509
2510// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2511// identifier to which the type applies, e.g.:
2512//
2513//              int f( int );                   // abstract variable parameter; no parameter name specified
2514//              int f( int (int) );             // abstract function-prototype parameter; no parameter name specified
2515//
2516// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2517// and functions versus pointers to arrays and functions.
2518
2519abstract_parameter_declarator:
2520        abstract_parameter_ptr
2521        | abstract_parameter_array attribute_list_opt
2522                { $$ = $1->addQualifiers( $2 ); }
2523        | abstract_parameter_function attribute_list_opt
2524                { $$ = $1->addQualifiers( $2 ); }
2525        ;
2526
2527abstract_parameter_ptr:
2528        '*'
2529                { $$ = DeclarationNode::newPointer( 0 ); }
2530        | '*' type_qualifier_list
2531                { $$ = DeclarationNode::newPointer( $2 ); }
2532        | '*' abstract_parameter_declarator
2533                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2534        | '*' type_qualifier_list abstract_parameter_declarator
2535                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2536        | '(' abstract_parameter_ptr ')'
2537                { $$ = $2; }
2538        ;
2539
2540abstract_parameter_array:
2541        array_parameter_dimension
2542        | '(' abstract_parameter_ptr ')' array_parameter_dimension
2543                { $$ = $2->addArray( $4 ); }
2544        | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2545                { $$ = $2->addArray( $4 ); }
2546        | '(' abstract_parameter_array ')'                                      // redundant parenthesis
2547                { $$ = $2; }
2548        ;
2549
2550abstract_parameter_function:
2551        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2552                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2553        | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2554                { $$ = $2->addParamList( $6 ); }
2555        | '(' abstract_parameter_function ')'                           // redundant parenthesis
2556                { $$ = $2; }
2557        ;
2558
2559array_parameter_dimension:
2560                // Only the first dimension can be empty or have qualifiers.
2561        array_parameter_1st_dimension
2562        | array_parameter_1st_dimension multi_array_dimension
2563                { $$ = $1->addArray( $2 ); }
2564        | multi_array_dimension
2565        ;
2566
2567// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2568//
2569//              ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2570//              a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
2571
2572array_parameter_1st_dimension:
2573        '[' ']'
2574                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2575        // multi_array_dimension handles the '[' '*' ']' case
2576        | '[' push type_qualifier_list '*' pop ']'                      // remaining C99
2577                { $$ = DeclarationNode::newVarArray( $3 ); }
2578        | '[' push type_qualifier_list pop ']'
2579                { $$ = DeclarationNode::newArray( 0, $3, false ); }
2580        // multi_array_dimension handles the '[' assignment_expression ']' case
2581        | '[' push type_qualifier_list assignment_expression pop ']'
2582                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2583        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2584                { $$ = DeclarationNode::newArray( $5, $4, true ); }
2585        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2586                { $$ = DeclarationNode::newArray( $5, $3, true ); }
2587        ;
2588
2589// This pattern parses a declaration of an abstract variable, i.e., there is no identifier to which the type applies,
2590// e.g.:
2591//
2592//              sizeof( int ); // abstract variable; no identifier name specified
2593//
2594// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2595// and functions versus pointers to arrays and functions.
2596
2597variable_abstract_declarator:
2598        variable_abstract_ptr
2599        | variable_abstract_array attribute_list_opt
2600                { $$ = $1->addQualifiers( $2 ); }
2601        | variable_abstract_function attribute_list_opt
2602                { $$ = $1->addQualifiers( $2 ); }
2603        ;
2604
2605variable_abstract_ptr:
2606        '*'
2607                { $$ = DeclarationNode::newPointer( 0 ); }
2608        | '*' type_qualifier_list
2609                { $$ = DeclarationNode::newPointer( $2 ); }
2610        | '*' variable_abstract_declarator
2611                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2612        | '*' type_qualifier_list variable_abstract_declarator
2613                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2614        | '(' variable_abstract_ptr ')'
2615                { $$ = $2; }
2616        ;
2617
2618variable_abstract_array:
2619        array_dimension
2620        | '(' variable_abstract_ptr ')' array_dimension
2621                { $$ = $2->addArray( $4 ); }
2622        | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
2623                { $$ = $2->addArray( $4 ); }
2624        | '(' variable_abstract_array ')'                                       // redundant parenthesis
2625                { $$ = $2; }
2626        ;
2627
2628variable_abstract_function:
2629        '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2630                { $$ = $2->addParamList( $6 ); }
2631        | '(' variable_abstract_function ')'                            // redundant parenthesis
2632                { $$ = $2; }
2633        ;
2634
2635// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
2636// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
2637
2638new_identifier_parameter_declarator_tuple:                              // CFA
2639        new_identifier_parameter_declarator_no_tuple
2640        | new_abstract_tuple
2641        | type_qualifier_list new_abstract_tuple
2642                { $$ = $2->addQualifiers( $1 ); }
2643        ;
2644
2645new_identifier_parameter_declarator_no_tuple:                   // CFA
2646        new_identifier_parameter_ptr
2647        | new_identifier_parameter_array
2648        ;
2649
2650new_identifier_parameter_ptr:                                                   // CFA
2651        '*' type_specifier
2652                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2653        | type_qualifier_list '*' type_specifier
2654                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2655        | '*' new_abstract_function
2656                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2657        | type_qualifier_list '*' new_abstract_function
2658                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2659        | '*' new_identifier_parameter_declarator_tuple
2660                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2661        | type_qualifier_list '*' new_identifier_parameter_declarator_tuple
2662                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2663        ;
2664
2665new_identifier_parameter_array:                                                 // CFA
2666                // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
2667                // shift/reduce conflict with new-style empty (void) function return type.
2668        '[' ']' type_specifier
2669                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2670        | new_array_parameter_1st_dimension type_specifier
2671                { $$ = $2->addNewArray( $1 ); }
2672        | '[' ']' multi_array_dimension type_specifier
2673                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2674        | new_array_parameter_1st_dimension multi_array_dimension type_specifier
2675                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2676        | multi_array_dimension type_specifier
2677                { $$ = $2->addNewArray( $1 ); }
2678        | '[' ']' new_identifier_parameter_ptr
2679                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2680        | new_array_parameter_1st_dimension new_identifier_parameter_ptr
2681                { $$ = $2->addNewArray( $1 ); }
2682        | '[' ']' multi_array_dimension new_identifier_parameter_ptr
2683                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2684        | new_array_parameter_1st_dimension multi_array_dimension new_identifier_parameter_ptr
2685                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2686        | multi_array_dimension new_identifier_parameter_ptr
2687                { $$ = $2->addNewArray( $1 ); }
2688        ;
2689
2690new_array_parameter_1st_dimension:
2691        '[' push type_qualifier_list '*' pop ']'                        // remaining C99
2692                { $$ = DeclarationNode::newVarArray( $3 ); }
2693        | '[' push type_qualifier_list assignment_expression pop ']'
2694                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2695        | '[' push declaration_qualifier_list assignment_expression pop ']'
2696                // declaration_qualifier_list must be used because of shift/reduce conflict with
2697                // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
2698                // appear in this context.
2699                { $$ = DeclarationNode::newArray( $4, $3, true ); }
2700        | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
2701                { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
2702        ;
2703
2704// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
2705// identifier to which the type applies, e.g.:
2706//
2707//              [int] f( int );                         // abstract variable parameter; no parameter name specified
2708//              [int] f( [int] (int) );         // abstract function-prototype parameter; no parameter name specified
2709//
2710// These rules need LR(3):
2711//
2712//              new_abstract_tuple identifier_or_type_name
2713//              '[' new_parameter_list ']' identifier_or_type_name '(' new_parameter_type_list_opt ')'
2714//
2715// since a function return type can be syntactically identical to a tuple type:
2716//
2717//              [int, int] t;
2718//              [int, int] f( int );
2719//
2720// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
2721// new_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
2722// lookahead. To accomplish this, new_abstract_declarator has an entry point without tuple, and tuple declarations are
2723// duplicated when appearing with new_function_specifier.
2724
2725new_abstract_declarator_tuple:                                                  // CFA
2726        new_abstract_tuple
2727        | type_qualifier_list new_abstract_tuple
2728                { $$ = $2->addQualifiers( $1 ); }
2729        | new_abstract_declarator_no_tuple
2730        ;
2731
2732new_abstract_declarator_no_tuple:                                               // CFA
2733        new_abstract_ptr
2734        | new_abstract_array
2735        ;
2736
2737new_abstract_ptr:                                                                               // CFA
2738        '*' type_specifier
2739                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2740        | type_qualifier_list '*' type_specifier
2741                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2742        | '*' new_abstract_function
2743                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2744        | type_qualifier_list '*' new_abstract_function
2745                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2746        | '*' new_abstract_declarator_tuple
2747                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2748        | type_qualifier_list '*' new_abstract_declarator_tuple
2749                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2750        ;
2751
2752new_abstract_array:                                                                             // CFA
2753                // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
2754                // empty (void) function return type.
2755        '[' ']' type_specifier
2756                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2757        | '[' ']' multi_array_dimension type_specifier
2758                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2759        | multi_array_dimension type_specifier
2760                { $$ = $2->addNewArray( $1 ); }
2761        | '[' ']' new_abstract_ptr
2762                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2763        | '[' ']' multi_array_dimension new_abstract_ptr
2764                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2765        | multi_array_dimension new_abstract_ptr
2766                { $$ = $2->addNewArray( $1 ); }
2767        ;
2768
2769new_abstract_tuple:                                                                             // CFA
2770        '[' push new_abstract_parameter_list pop ']'
2771                { $$ = DeclarationNode::newTuple( $3 ); }
2772        ;
2773
2774new_abstract_function:                                                                  // CFA
2775        '[' ']' '(' new_parameter_type_list_opt ')'
2776                { $$ = DeclarationNode::newFunction( 0, DeclarationNode::newTuple( 0 ), $4, 0 ); }
2777        | new_abstract_tuple '(' push new_parameter_type_list_opt pop ')'
2778                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2779        | new_function_return '(' push new_parameter_type_list_opt pop ')'
2780                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2781        ;
2782
2783// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
2784//    each declaration, and in the specifier-qualifier list in each structure declaration and type name."
2785//
2786// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
2787//    the declaration specifiers in a declaration is an obsolescent feature."
2788//
2789// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
2790//    prototype-format parameter type declarators) is an obsolescent feature."
2791//
2792// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
2793//    declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
2794
2795//************************* MISCELLANEOUS ********************************
2796
2797comma_opt:                                                                                              // redundant comma
2798        // empty
2799        | ','
2800        ;
2801
2802assignment_opt:
2803        // empty
2804                { $$ = 0; }
2805        | '=' assignment_expression
2806                { $$ = $2; }
2807        ;
2808
2809%%
2810// ----end of grammar----
2811
2812void yyerror( const char * ) {
2813        std::cout << "Error ";
2814        if ( yyfilename ) {
2815            std::cout << "in file " << yyfilename << " ";
2816        } // if
2817        std::cout << "at line " << yylineno << " reading token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << "\"" << std::endl;
2818}
2819
2820// Local Variables: //
2821// mode: c++ //
2822// tab-width: 4 //
2823// compile-command: "make install" //
2824// End: //
Note: See TracBrowser for help on using the repository browser.