source: src/Parser/parser.yy @ 936e9f4

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 936e9f4 was 936e9f4, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

first attempt, add declarations into "if" conditional

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