source: src/Parser/parser.yy @ d14d96a

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

automake change gnu back to foreign (do not know why it changed), add := and & (reference) to lexer/parser

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