source: src/Parser/parser.yy @ 9335ecc

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

update internal names for renamed files

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