source: src/Parser/parser.yy @ 994d080

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

add exponential operator and syntax for catch predicate

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