source: src/Parser/parser.yy @ 02e5ab6

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 02e5ab6 was 02e5ab6, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add preprocessor flag -DCFORALL=1, add syntax for constructor/destructor

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