source: src/Parser/parser.yy @ 4040425

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 4040425 was 4040425, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change keyword type to otype and context to trait

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