source: src/Parser/parser.yy @ c6b1105

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

add reference operator, labels cannot be 0/1, small changes to tests

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