source: src/Parser/parser.yy @ 71a3593

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 71a3593 was 9706554, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

even more refactoring of parser code

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