source: src/Parser/parser.yy @ 764db43

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 764db43 was 72457b6, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add optional virtual keyword in cast

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