source: src/Parser/parser.yy @ 20e409e

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 20e409e was 51b1202, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

move constant/string to unary_expression, temporarily separate asm and local label, introduce DesignatorNode?

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