source: src/Parser/parser.yy @ 6ea87486

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 6ea87486 was 6ea87486, checked in by Andrew Beach <ajbeach@…>, 7 years ago

That should be all the base code for 'tree structures' to work.

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