source: src/Parser/parser.yy @ 4b234f0

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 4b234f0 was 578e6037, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

extend syntax for with to include expressions

  • Property mode set to 100644
File size: 111.4 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// parser.yy --
8//
9// Author           : Peter A. Buhr
10// Created On       : Sat Sep  1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Jul 13 14:38:54 2017
13// Update Count     : 2431
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
1671aggregate_key:
1672        STRUCT
1673                { $$ = DeclarationNode::Struct; }
1674        | UNION
1675                { $$ = DeclarationNode::Union; }
1676        | COROUTINE
1677                { $$ = DeclarationNode::Coroutine; }
1678        | MONITOR
1679                { $$ = DeclarationNode::Monitor; }
1680        | THREAD
1681                { $$ = DeclarationNode::Thread; }
1682        ;
1683
1684field_declaration_list:
1685        // empty
1686                { $$ = nullptr; }
1687        | field_declaration_list field_declaration
1688                { $$ = $1 ? $1->appendList( $2 ) : $2; }
1689        ;
1690
1691field_declaration:
1692        cfa_field_declaring_list ';'                                            // CFA, new style field declaration
1693        | EXTENSION cfa_field_declaring_list ';'                        // GCC
1694                {
1695                        distExt( $2 );                                                          // mark all fields in list
1696                        $$ = $2;
1697                }
1698        | type_specifier field_declaring_list ';'
1699                {
1700                        $$ = distAttr( $1, $2 ); }
1701        | EXTENSION type_specifier field_declaring_list ';'     // GCC
1702                {
1703                        distExt( $3 );                                                          // mark all fields in list
1704                        $$ = distAttr( $2, $3 );
1705                }
1706        ;
1707
1708cfa_field_declaring_list:                                                               // CFA, new style field declaration
1709        cfa_abstract_declarator_tuple                                           // CFA, no field name
1710        | cfa_abstract_declarator_tuple no_attr_identifier_or_type_name
1711                { $$ = $1->addName( $2 ); }
1712        | cfa_field_declaring_list ',' no_attr_identifier_or_type_name
1713                { $$ = $1->appendList( $1->cloneType( $3 ) ); }
1714        | cfa_field_declaring_list ','                                          // CFA, no field name
1715                { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1716        ;
1717
1718field_declaring_list:
1719        field_declarator
1720        | field_declaring_list ',' attribute_list_opt field_declarator
1721                { $$ = $1->appendList( $4->addQualifiers( $3 ) ); }
1722        ;
1723
1724field_declarator:
1725        // empty
1726                { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
1727        // '@'
1728        //      { $$ = DeclarationNode::newName( new string( DeclarationNode::anonymous.newName() ) ); } // CFA, no field name
1729        | bit_subrange_size                                                                     // no field name
1730                { $$ = DeclarationNode::newBitfield( $1 ); }
1731        | variable_declarator bit_subrange_size_opt
1732                // A semantic check is required to ensure bit_subrange only appears on base type int.
1733                { $$ = $1->addBitfield( $2 ); }
1734        | variable_type_redeclarator bit_subrange_size_opt
1735                // A semantic check is required to ensure bit_subrange only appears on base type int.
1736                { $$ = $1->addBitfield( $2 ); }
1737        | variable_abstract_declarator                                          // CFA, no field name
1738        ;
1739
1740bit_subrange_size_opt:
1741        // empty
1742                { $$ = nullptr; }
1743        | bit_subrange_size
1744                { $$ = $1; }
1745        ;
1746
1747bit_subrange_size:
1748        ':' constant_expression
1749                { $$ = $2; }
1750        ;
1751
1752enum_type:                                                                                              // enum
1753        ENUM attribute_list_opt '{' enumerator_list comma_opt '}'
1754                { $$ = DeclarationNode::newEnum( new string( DeclarationNode::anonymous.newName() ), $4, true )->addQualifiers( $2 ); }
1755        | ENUM attribute_list_opt no_attr_identifier_or_type_name
1756                { typedefTable.makeTypedef( *$3 ); }
1757          '{' enumerator_list comma_opt '}'
1758                { $$ = DeclarationNode::newEnum( $3, $6, true )->addQualifiers( $2 ); }
1759        | enum_type_nobody
1760        ;
1761
1762enum_type_nobody:                                                                               // enum - {...}
1763        ENUM attribute_list_opt no_attr_identifier_or_type_name
1764                {
1765                        typedefTable.makeTypedef( *$3 );
1766                        $$ = DeclarationNode::newEnum( $3, 0, false )->addQualifiers( $2 );
1767                }
1768        ;
1769
1770enumerator_list:
1771        no_attr_identifier_or_type_name enumerator_value_opt
1772                { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
1773        | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
1774                { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1775        ;
1776
1777enumerator_value_opt:
1778        // empty
1779                { $$ = nullptr; }
1780        | '=' constant_expression
1781                { $$ = $2; }
1782        ;
1783
1784// Minimum of one parameter after which ellipsis is allowed only at the end.
1785
1786cfa_parameter_type_list_opt:                                                    // CFA
1787        // empty
1788                { $$ = nullptr; }
1789        | cfa_parameter_type_list
1790        ;
1791
1792cfa_parameter_type_list:                                                                // CFA, abstract + real
1793        cfa_abstract_parameter_list
1794        | cfa_parameter_list
1795        | cfa_parameter_list pop ',' push cfa_abstract_parameter_list
1796                { $$ = $1->appendList( $5 ); }
1797        | cfa_abstract_parameter_list pop ',' push ELLIPSIS
1798                { $$ = $1->addVarArgs(); }
1799        | cfa_parameter_list pop ',' push ELLIPSIS
1800                { $$ = $1->addVarArgs(); }
1801        ;
1802
1803cfa_parameter_list:                                                                             // CFA
1804                // To obtain LR(1) between cfa_parameter_list and cfa_abstract_tuple, the last cfa_abstract_parameter_list is
1805                // factored out from cfa_parameter_list, flattening the rules to get lookahead to the ']'.
1806        cfa_parameter_declaration
1807        | cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
1808                { $$ = $1->appendList( $5 ); }
1809        | cfa_parameter_list pop ',' push cfa_parameter_declaration
1810                { $$ = $1->appendList( $5 ); }
1811        | cfa_parameter_list pop ',' push cfa_abstract_parameter_list pop ',' push cfa_parameter_declaration
1812                { $$ = $1->appendList( $5 )->appendList( $9 ); }
1813        ;
1814
1815cfa_abstract_parameter_list:                                                    // CFA, new & old style abstract
1816        cfa_abstract_parameter_declaration
1817        | cfa_abstract_parameter_list pop ',' push cfa_abstract_parameter_declaration
1818                { $$ = $1->appendList( $5 ); }
1819        ;
1820
1821parameter_type_list_opt:
1822        // empty
1823                { $$ = nullptr; }
1824        | parameter_type_list
1825        ;
1826
1827parameter_type_list:
1828        parameter_list
1829        | parameter_list pop ',' push ELLIPSIS
1830                { $$ = $1->addVarArgs(); }
1831        ;
1832
1833parameter_list:                                                                                 // abstract + real
1834        abstract_parameter_declaration
1835        | parameter_declaration
1836        | parameter_list pop ',' push abstract_parameter_declaration
1837                { $$ = $1->appendList( $5 ); }
1838        | parameter_list pop ',' push parameter_declaration
1839                { $$ = $1->appendList( $5 ); }
1840        ;
1841
1842// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
1843// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
1844
1845cfa_parameter_declaration:                                                              // CFA, new & old style parameter declaration
1846        parameter_declaration
1847        | cfa_identifier_parameter_declarator_no_tuple identifier_or_type_name assignment_opt
1848                { $$ = $1->addName( $2 ); }
1849        | cfa_abstract_tuple identifier_or_type_name assignment_opt
1850                // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
1851                { $$ = $1->addName( $2 ); }
1852        | type_qualifier_list cfa_abstract_tuple identifier_or_type_name assignment_opt
1853                { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
1854        | cfa_function_specifier
1855        ;
1856
1857cfa_abstract_parameter_declaration:                                             // CFA, new & old style parameter declaration
1858        abstract_parameter_declaration
1859        | cfa_identifier_parameter_declarator_no_tuple
1860        | cfa_abstract_tuple
1861                // To obtain LR(1), these rules must be duplicated here (see cfa_abstract_declarator).
1862        | type_qualifier_list cfa_abstract_tuple
1863                { $$ = $2->addQualifiers( $1 ); }
1864        | cfa_abstract_function
1865        ;
1866
1867parameter_declaration:
1868                // No SUE declaration in parameter list.
1869        declaration_specifier_nobody identifier_parameter_declarator assignment_opt
1870                {
1871                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1872                        $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
1873                }
1874        | declaration_specifier_nobody type_parameter_redeclarator assignment_opt
1875                {
1876                        typedefTable.addToEnclosingScope( TypedefTable::ID );
1877                        $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr );
1878                }
1879        ;
1880
1881abstract_parameter_declaration:
1882        declaration_specifier_nobody assignment_opt
1883                { $$ = $1->addInitializer( $2 ? new InitializerNode( $2 ) : nullptr ); }
1884        | declaration_specifier_nobody abstract_parameter_declarator assignment_opt
1885                { $$ = $2->addType( $1 )->addInitializer( $3 ? new InitializerNode( $3 ) : nullptr ); }
1886        ;
1887
1888// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
1889// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
1890// identifiers.  The ANSI-style parameter-list can redefine a typedef name.
1891
1892identifier_list:                                                                                // K&R-style parameter list => no types
1893        no_attr_identifier
1894                { $$ = DeclarationNode::newName( $1 ); }
1895        | identifier_list ',' no_attr_identifier
1896                { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
1897        ;
1898
1899identifier_or_type_name:
1900        identifier
1901        | TYPEDEFname
1902        | TYPEGENname
1903        ;
1904
1905no_attr_identifier_or_type_name:
1906        no_attr_identifier
1907        | TYPEDEFname
1908        | TYPEGENname
1909        ;
1910
1911type_no_function:                                                                               // sizeof, alignof, cast (constructor)
1912        cfa_abstract_declarator_tuple                                           // CFA
1913        | type_specifier
1914        | type_specifier abstract_declarator
1915                { $$ = $2->addType( $1 ); }
1916        ;
1917
1918type:                                                                                                   // typeof, assertion
1919        type_no_function
1920        | cfa_abstract_function                                                         // CFA
1921        ;
1922
1923initializer_opt:
1924        // empty
1925                { $$ = nullptr; }
1926        | '=' initializer
1927                { $$ = $2; }
1928        | ATassign initializer
1929                { $$ = $2->set_maybeConstructed( false ); }
1930        ;
1931
1932initializer:
1933        assignment_expression                                           { $$ = new InitializerNode( $1 ); }
1934        | '{' initializer_list comma_opt '}'            { $$ = new InitializerNode( $2, true ); }
1935        ;
1936
1937initializer_list:
1938        // empty
1939                { $$ = nullptr; }
1940        | initializer
1941        | designation initializer                                       { $$ = $2->set_designators( $1 ); }
1942        | initializer_list ',' initializer                      { $$ = (InitializerNode *)( $1->set_last( $3 ) ); }
1943        | initializer_list ',' designation initializer
1944                { $$ = (InitializerNode *)( $1->set_last( $4->set_designators( $3 ) ) ); }
1945        ;
1946
1947// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
1948// '=' to separator the designator from the initializer value, as in:
1949//
1950//              int x[10] = { [1] = 3 };
1951//
1952// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment.  To disambiguate this case, CFA
1953// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
1954// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
1955// shift/reduce conflicts
1956
1957designation:
1958        designator_list ':'                                                                     // C99, CFA uses ":" instead of "="
1959        | no_attr_identifier ':'                                                        // GCC, field name
1960                { $$ = new ExpressionNode( build_varref( $1 ) ); }
1961        ;
1962
1963designator_list:                                                                                // C99
1964        designator
1965        | designator_list designator
1966                { $$ = (ExpressionNode *)( $1->set_last( $2 ) ); }
1967        //| designator_list designator                                          { $$ = new ExpressionNode( $1, $2 ); }
1968        ;
1969
1970designator:
1971        '.' no_attr_identifier                                                          // C99, field name
1972                { $$ = new ExpressionNode( build_varref( $2 ) ); }
1973        | '[' push assignment_expression pop ']'                        // C99, single array element
1974                // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
1975                { $$ = $3; }
1976        | '[' push subrange pop ']'                                                     // CFA, multiple array elements
1977                { $$ = $3; }
1978        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
1979                { $$ = new ExpressionNode( build_range( $3, $5 ) ); }
1980        | '.' '[' push field_list pop ']'                                       // CFA, tuple field selector
1981                { $$ = $4; }
1982        ;
1983
1984// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
1985// rather than an object-oriented type system. This required four groups of extensions:
1986//
1987// Overloading: function, data, and operator identifiers may be overloaded.
1988//
1989// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
1990//     and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
1991//     definitions of new types. Type declarations with storage class "extern" provide opaque types.
1992//
1993// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
1994//     site. A polymorphic function is not a template; it is a function, with an address and a type.
1995//
1996// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
1997//     types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
1998//     hierarchies. Unlike classes, they can define relationships between types.  Assertions declare that a type or
1999//     types provide the operations declared by a specification.  Assertions are normally used to declare requirements
2000//     on type arguments of polymorphic functions.
2001
2002type_parameter_list:                                                                    // CFA
2003        type_parameter
2004                { $$ = $1; }
2005        | type_parameter_list ',' type_parameter
2006                { $$ = $1->appendList( $3 ); }
2007        ;
2008
2009type_initializer_opt:                                                                   // CFA
2010        // empty
2011                { $$ = nullptr; }
2012        | '=' type
2013                { $$ = $2; }
2014        ;
2015
2016type_parameter:                                                                                 // CFA
2017        type_class no_attr_identifier_or_type_name
2018                { typedefTable.addToEnclosingScope( *$2, TypedefTable::TD ); }
2019          type_initializer_opt assertion_list_opt
2020                { $$ = DeclarationNode::newTypeParam( $1, $2 )->addTypeInitializer( $4 )->addAssertions( $5 ); }
2021        | type_specifier identifier_parameter_declarator
2022        ;
2023
2024type_class:                                                                                             // CFA
2025        OTYPE
2026                { $$ = DeclarationNode::Otype; }
2027        | DTYPE
2028                { $$ = DeclarationNode::Dtype; }
2029        | FTYPE
2030                { $$ = DeclarationNode::Ftype; }
2031        | TTYPE
2032                { $$ = DeclarationNode::Ttype; }
2033        ;
2034
2035assertion_list_opt:                                                                             // CFA
2036        // empty
2037                { $$ = nullptr; }
2038        | assertion_list_opt assertion
2039                { $$ = $1 ? $1->appendList( $2 ) : $2; }
2040        ;
2041
2042assertion:                                                                                              // CFA
2043        '|' no_attr_identifier_or_type_name '(' type_list ')'
2044                {
2045                        typedefTable.openTrait( *$2 );
2046                        $$ = DeclarationNode::newTraitUse( $2, $4 );
2047                }
2048        | '|' '{' push trait_declaration_list '}'
2049                { $$ = $4; }
2050        | '|' '(' push type_parameter_list pop ')' '{' push trait_declaration_list '}' '(' type_list ')'
2051                { $$ = nullptr; }
2052        ;
2053
2054type_list:                                                                                              // CFA
2055        type
2056                { $$ = new ExpressionNode( build_typevalue( $1 ) ); }
2057        | assignment_expression
2058        | type_list ',' type
2059                { $$ = (ExpressionNode *)( $1->set_last( new ExpressionNode( build_typevalue( $3 ) ) ) ); }
2060        | type_list ',' assignment_expression
2061                { $$ = (ExpressionNode *)( $1->set_last( $3 )); }
2062        ;
2063
2064type_declaring_list:                                                                    // CFA
2065        OTYPE type_declarator
2066                { $$ = $2; }
2067        | storage_class_list OTYPE type_declarator
2068                { $$ = $3->addQualifiers( $1 ); }
2069        | type_declaring_list ',' type_declarator
2070                { $$ = $1->appendList( $3->copySpecifiers( $1 ) ); }
2071        ;
2072
2073type_declarator:                                                                                // CFA
2074        type_declarator_name assertion_list_opt
2075                { $$ = $1->addAssertions( $2 ); }
2076        | type_declarator_name assertion_list_opt '=' type
2077                { $$ = $1->addAssertions( $2 )->addType( $4 ); }
2078        ;
2079
2080type_declarator_name:                                                                   // CFA
2081        no_attr_identifier_or_type_name
2082                {
2083                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
2084                        $$ = DeclarationNode::newTypeDecl( $1, 0 );
2085                }
2086        | no_attr_identifier_or_type_name '(' push type_parameter_list pop ')'
2087                {
2088                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
2089                        $$ = DeclarationNode::newTypeDecl( $1, $4 );
2090                }
2091        ;
2092
2093trait_specifier:                                                                                // CFA
2094        TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{' '}'
2095                {
2096                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
2097                        $$ = DeclarationNode::newTrait( $2, $5, 0 );
2098                }
2099        | TRAIT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{'
2100                {
2101                        typedefTable.enterTrait( *$2 );
2102                        typedefTable.enterScope();
2103                }
2104          trait_declaration_list '}'
2105                {
2106                        typedefTable.leaveTrait();
2107                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
2108                        $$ = DeclarationNode::newTrait( $2, $5, $10 );
2109                }
2110        ;
2111
2112trait_declaration_list:                                                                 // CFA
2113        trait_declaration
2114        | trait_declaration_list push trait_declaration
2115                { $$ = $1->appendList( $3 ); }
2116        ;
2117
2118trait_declaration:                                                                              // CFA
2119        cfa_trait_declaring_list pop ';'
2120        | trait_declaring_list pop ';'
2121        ;
2122
2123cfa_trait_declaring_list:                                                               // CFA
2124        cfa_variable_specifier
2125                {
2126                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2127                        $$ = $1;
2128                }
2129        | cfa_function_specifier
2130                {
2131                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2132                        $$ = $1;
2133                }
2134        | cfa_trait_declaring_list pop ',' push identifier_or_type_name
2135                {
2136                        typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
2137                        $$ = $1->appendList( $1->cloneType( $5 ) );
2138                }
2139        ;
2140
2141trait_declaring_list:                                                                   // CFA
2142        type_specifier declarator
2143                {
2144                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2145                        $$ = $2->addType( $1 );
2146                }
2147        | trait_declaring_list pop ',' push declarator
2148                {
2149                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
2150                        $$ = $1->appendList( $1->cloneBaseType( $5 ) );
2151                }
2152        ;
2153
2154//***************************** EXTERNAL DEFINITIONS *****************************
2155
2156translation_unit:
2157        // empty
2158                {}                                                                                              // empty input file
2159        | external_definition_list
2160                { parseTree = parseTree ? parseTree->appendList( $1 ) : $1;     }
2161        ;
2162
2163external_definition_list:
2164        external_definition
2165        | external_definition_list push external_definition
2166                { $$ = $1 ? $1->appendList( $3 ) : $3; }
2167        ;
2168
2169external_definition_list_opt:
2170        // empty
2171                { $$ = nullptr; }
2172        | external_definition_list
2173        ;
2174
2175external_definition:
2176        declaration
2177        | external_function_definition
2178        | ASM '(' string_literal ')' ';'                                        // GCC, global assembler statement
2179                {
2180                        $$ = DeclarationNode::newAsmStmt( new StatementNode( build_asmstmt( false, $3, 0 ) ) );
2181                }
2182        | EXTERN STRINGliteral                                                          // C++-style linkage specifier
2183                {
2184                        linkageStack.push( linkage );                           // handle nested extern "C"/"Cforall"
2185                        linkage = LinkageSpec::linkageUpdate( linkage, $2 );
2186                }
2187          '{' external_definition_list_opt '}'
2188                {
2189                        linkage = linkageStack.top();
2190                        linkageStack.pop();
2191                        $$ = $5;
2192                }
2193        | EXTENSION external_definition                                         // GCC, multiple __extension__ allowed, meaning unknown
2194                {
2195                        distExt( $2 );                                                          // mark all fields in list
2196                        $$ = $2;
2197                }
2198        ;
2199
2200external_function_definition:
2201        function_definition
2202                // These rules are a concession to the "implicit int" type_specifier because there is a significant amount of
2203                // legacy code with global functions missing the type-specifier for the return type, and assuming "int".
2204                // Parsing is possible because function_definition does not appear in the context of an expression (nested
2205                // functions preclude this concession, i.e., all nested function must have a return type). A function prototype
2206                // declaration must still have a type_specifier.  OBSOLESCENT (see 1)
2207        | function_declarator compound_statement
2208                {
2209                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2210                        typedefTable.leaveScope();
2211                        $$ = $1->addFunctionBody( $2 );
2212                }
2213        | KR_function_declarator push KR_declaration_list_opt compound_statement
2214                {
2215                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2216                        typedefTable.leaveScope();
2217                        $$ = $1->addOldDeclList( $3 )->addFunctionBody( $4 );
2218                }
2219        ;
2220
2221with_clause_opt:
2222        // empty
2223                { $$ = (StatementNode *)0; }                                    // FIX ME
2224        | WITH '(' tuple_expression_list ')'
2225                { $$ = (StatementNode *)0; }                                    // FIX ME
2226        ;
2227
2228function_definition:
2229        cfa_function_declaration with_clause_opt compound_statement     // CFA
2230                {
2231                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2232                        typedefTable.leaveScope();
2233                        $$ = $1->addFunctionBody( $3 );
2234                }
2235        | declaration_specifier function_declarator with_clause_opt compound_statement
2236                {
2237                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2238                        typedefTable.leaveScope();
2239                        $$ = $2->addFunctionBody( $4 )->addType( $1 );
2240                }
2241        | type_qualifier_list function_declarator with_clause_opt compound_statement
2242                {
2243                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2244                        typedefTable.leaveScope();
2245                        $$ = $2->addFunctionBody( $4 )->addQualifiers( $1 );
2246                }
2247        | declaration_qualifier_list function_declarator with_clause_opt compound_statement
2248                {
2249                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2250                        typedefTable.leaveScope();
2251                        $$ = $2->addFunctionBody( $4 )->addQualifiers( $1 );
2252                }
2253        | declaration_qualifier_list type_qualifier_list function_declarator with_clause_opt compound_statement
2254                {
2255                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2256                        typedefTable.leaveScope();
2257                        $$ = $3->addFunctionBody( $5 )->addQualifiers( $2 )->addQualifiers( $1 );
2258                }
2259
2260                // Old-style K&R function definition, OBSOLESCENT (see 4)
2261        | declaration_specifier KR_function_declarator push KR_declaration_list_opt with_clause_opt compound_statement
2262                {
2263                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2264                        typedefTable.leaveScope();
2265                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $6 )->addType( $1 );
2266                }
2267        | type_qualifier_list KR_function_declarator push KR_declaration_list_opt with_clause_opt compound_statement
2268                {
2269                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2270                        typedefTable.leaveScope();
2271                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $6 )->addQualifiers( $1 );
2272                }
2273
2274                // Old-style K&R function definition with "implicit int" type_specifier, OBSOLESCENT (see 4)
2275        | declaration_qualifier_list KR_function_declarator push KR_declaration_list_opt with_clause_opt compound_statement
2276                {
2277                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2278                        typedefTable.leaveScope();
2279                        $$ = $2->addOldDeclList( $4 )->addFunctionBody( $6 )->addQualifiers( $1 );
2280                }
2281        | declaration_qualifier_list type_qualifier_list KR_function_declarator push KR_declaration_list_opt with_clause_opt compound_statement
2282                {
2283                        typedefTable.addToEnclosingScope( TypedefTable::ID );
2284                        typedefTable.leaveScope();
2285                        $$ = $3->addOldDeclList( $5 )->addFunctionBody( $7 )->addQualifiers( $2 )->addQualifiers( $1 );
2286                }
2287        ;
2288
2289declarator:
2290        variable_declarator
2291        | variable_type_redeclarator
2292        | function_declarator
2293        ;
2294
2295subrange:
2296        constant_expression '~' constant_expression                     // CFA, integer subrange
2297                { $$ = new ExpressionNode( build_range( $1, $3 ) ); }
2298        ;
2299
2300asm_name_opt:                                                                                   // GCC
2301        // empty
2302                { $$ = nullptr; }
2303        | ASM '(' string_literal ')' attribute_list_opt
2304                {
2305                        DeclarationNode * name = new DeclarationNode();
2306                        name->asmName = $3;
2307                        $$ = name->addQualifiers( $5 );
2308                }
2309        ;
2310
2311attribute_list_opt:                                                                             // GCC
2312        // empty
2313                { $$ = nullptr; }
2314        | attribute_list
2315        ;
2316
2317attribute_list:                                                                                 // GCC
2318        attribute
2319        | attribute_list attribute
2320                { $$ = $2->addQualifiers( $1 ); }
2321        ;
2322
2323attribute:                                                                                              // GCC
2324        ATTRIBUTE '(' '(' attribute_name_list ')' ')'
2325                { $$ = $4; }
2326        ;
2327
2328attribute_name_list:                                                                    // GCC
2329        attribute_name
2330        | attribute_name_list ',' attribute_name
2331                { $$ = $3->addQualifiers( $1 ); }
2332        ;
2333
2334attribute_name:                                                                                 // GCC
2335        // empty
2336                { $$ = nullptr; }
2337        | attr_name
2338                { $$ = DeclarationNode::newAttribute( $1 ); }
2339        | attr_name '(' argument_expression_list ')'
2340                { $$ = DeclarationNode::newAttribute( $1, $3 ); }
2341        ;
2342
2343attr_name:                                                                                              // GCC
2344        IDENTIFIER
2345        | TYPEDEFname
2346        | TYPEGENname
2347        | CONST
2348                { $$ = Token{ new string( "__const__" ), { nullptr, -1 } }; }
2349        ;
2350
2351// ============================================================================
2352// The following sections are a series of grammar patterns used to parse declarators. Multiple patterns are necessary
2353// because the type of an identifier in wrapped around the identifier in the same form as its usage in an expression, as
2354// in:
2355//
2356//              int (*f())[10] { ... };
2357//              ... (*f())[3] += 1;             // definition mimics usage
2358//
2359// Because these patterns are highly recursive, changes at a lower level in the recursion require copying some or all of
2360// the pattern. Each of these patterns has some subtle variation to ensure correct syntax in a particular context.
2361// ============================================================================
2362
2363// ----------------------------------------------------------------------------
2364// The set of valid declarators before a compound statement for defining a function is less than the set of declarators
2365// to define a variable or function prototype, e.g.:
2366//
2367//              valid declaration               invalid definition
2368//              -----------------               ------------------
2369//              int f;                                  int f {}
2370//              int *f;                                 int *f {}
2371//              int f[10];                              int f[10] {}
2372//              int (*f)(int);                  int (*f)(int) {}
2373//
2374// To preclude this syntactic anomaly requires separating the grammar rules for variable and function declarators, hence
2375// variable_declarator and function_declarator.
2376// ----------------------------------------------------------------------------
2377
2378// This pattern parses a declaration of a variable that is not redefining a typedef name. The pattern precludes
2379// declaring an array of functions versus a pointer to an array of functions.
2380
2381variable_declarator:
2382        paren_identifier attribute_list_opt
2383                { $$ = $1->addQualifiers( $2 ); }
2384        | variable_ptr
2385        | variable_array attribute_list_opt
2386                { $$ = $1->addQualifiers( $2 ); }
2387        | variable_function attribute_list_opt
2388                { $$ = $1->addQualifiers( $2 ); }
2389        ;
2390
2391paren_identifier:
2392        identifier
2393                {
2394                        typedefTable.setNextIdentifier( *$1 );
2395                        $$ = DeclarationNode::newName( $1 );
2396                }
2397        | '(' paren_identifier ')'                                                      // redundant parenthesis
2398                { $$ = $2; }
2399        ;
2400
2401variable_ptr:
2402        ptrref_operator variable_declarator
2403                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2404        | ptrref_operator type_qualifier_list variable_declarator
2405                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2406        | '(' variable_ptr ')' attribute_list_opt
2407                { $$ = $2->addQualifiers( $4 ); }                               // redundant parenthesis
2408        ;
2409
2410variable_array:
2411        paren_identifier array_dimension
2412                { $$ = $1->addArray( $2 ); }
2413        | '(' variable_ptr ')' array_dimension
2414                { $$ = $2->addArray( $4 ); }
2415        | '(' variable_array ')' multi_array_dimension          // redundant parenthesis
2416                { $$ = $2->addArray( $4 ); }
2417        | '(' variable_array ')'                                                        // redundant parenthesis
2418                { $$ = $2; }
2419        ;
2420
2421variable_function:
2422        '(' variable_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2423                { $$ = $2->addParamList( $6 ); }
2424        | '(' variable_function ')'                                                     // redundant parenthesis
2425                { $$ = $2; }
2426        ;
2427
2428// This pattern parses a function declarator that is not redefining a typedef name. For non-nested functions, there is
2429// no context where a function definition can redefine a typedef name, i.e., the typedef and function name cannot exist
2430// is the same scope.  The pattern precludes returning arrays and functions versus pointers to arrays and functions.
2431
2432function_declarator:
2433        function_no_ptr attribute_list_opt
2434                { $$ = $1->addQualifiers( $2 ); }
2435        | function_ptr
2436        | function_array attribute_list_opt
2437                { $$ = $1->addQualifiers( $2 ); }
2438        ;
2439
2440function_no_ptr:
2441        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2442                { $$ = $1->addParamList( $4 ); }
2443        | '(' function_ptr ')' '(' push parameter_type_list_opt pop ')'
2444                { $$ = $2->addParamList( $6 ); }
2445        | '(' function_no_ptr ')'                                                       // redundant parenthesis
2446                { $$ = $2; }
2447        ;
2448
2449function_ptr:
2450        ptrref_operator function_declarator
2451                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2452        | ptrref_operator type_qualifier_list function_declarator
2453                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2454        | '(' function_ptr ')'
2455                { $$ = $2; }
2456        ;
2457
2458function_array:
2459        '(' function_ptr ')' array_dimension
2460                { $$ = $2->addArray( $4 ); }
2461        | '(' function_array ')' multi_array_dimension          // redundant parenthesis
2462                { $$ = $2->addArray( $4 ); }
2463        | '(' function_array ')'                                                        // redundant parenthesis
2464                { $$ = $2; }
2465        ;
2466
2467// This pattern parses an old-style K&R function declarator (OBSOLESCENT, see 4)
2468//
2469//   f( a, b, c ) int a, *b, c[]; {}
2470//
2471// that is not redefining a typedef name (see function_declarator for additional comments). The pattern precludes
2472// returning arrays and functions versus pointers to arrays and functions.
2473
2474KR_function_declarator:
2475        KR_function_no_ptr
2476        | KR_function_ptr
2477        | KR_function_array
2478        ;
2479
2480KR_function_no_ptr:
2481        paren_identifier '(' identifier_list ')'                        // function_declarator handles empty parameter
2482                { $$ = $1->addIdList( $3 ); }
2483        | '(' KR_function_ptr ')' '(' push parameter_type_list_opt pop ')'
2484                { $$ = $2->addParamList( $6 ); }
2485        | '(' KR_function_no_ptr ')'                                            // redundant parenthesis
2486                { $$ = $2; }
2487        ;
2488
2489KR_function_ptr:
2490        ptrref_operator KR_function_declarator
2491                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2492        | ptrref_operator type_qualifier_list KR_function_declarator
2493                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2494        | '(' KR_function_ptr ')'
2495                { $$ = $2; }
2496        ;
2497
2498KR_function_array:
2499        '(' KR_function_ptr ')' array_dimension
2500                { $$ = $2->addArray( $4 ); }
2501        | '(' KR_function_array ')' multi_array_dimension       // redundant parenthesis
2502                { $$ = $2->addArray( $4 ); }
2503        | '(' KR_function_array ')'                                                     // redundant parenthesis
2504                { $$ = $2; }
2505        ;
2506
2507// This pattern parses a declaration for a variable or function prototype that redefines a type name, e.g.:
2508//
2509//              typedef int foo;
2510//              {
2511//                 int foo; // redefine typedef name in new scope
2512//              }
2513//
2514// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2515// and functions versus pointers to arrays and functions.
2516
2517variable_type_redeclarator:
2518        paren_type attribute_list_opt
2519                { $$ = $1->addQualifiers( $2 ); }
2520        | type_ptr
2521        | type_array attribute_list_opt
2522                { $$ = $1->addQualifiers( $2 ); }
2523        | type_function attribute_list_opt
2524                { $$ = $1->addQualifiers( $2 ); }
2525        ;
2526
2527paren_type:
2528        typedef
2529        | '(' paren_type ')'
2530                { $$ = $2; }
2531        ;
2532
2533type_ptr:
2534        ptrref_operator variable_type_redeclarator
2535                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2536        | ptrref_operator type_qualifier_list variable_type_redeclarator
2537                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2538        | '(' type_ptr ')' attribute_list_opt
2539                { $$ = $2->addQualifiers( $4 ); }
2540        ;
2541
2542type_array:
2543        paren_type array_dimension
2544                { $$ = $1->addArray( $2 ); }
2545        | '(' type_ptr ')' array_dimension
2546                { $$ = $2->addArray( $4 ); }
2547        | '(' type_array ')' multi_array_dimension                      // redundant parenthesis
2548                { $$ = $2->addArray( $4 ); }
2549        | '(' type_array ')'                                                            // redundant parenthesis
2550                { $$ = $2; }
2551        ;
2552
2553type_function:
2554        paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2555                { $$ = $1->addParamList( $4 ); }
2556        | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2557                { $$ = $2->addParamList( $6 ); }
2558        | '(' type_function ')'                                                         // redundant parenthesis
2559                { $$ = $2; }
2560        ;
2561
2562// This pattern parses a declaration for a parameter variable of a function prototype or actual that is not redefining a
2563// typedef name and allows the C99 array options, which can only appear in a parameter list.  The pattern precludes
2564// declaring an array of functions versus a pointer to an array of functions, and returning arrays and functions versus
2565// pointers to arrays and functions.
2566
2567identifier_parameter_declarator:
2568        paren_identifier attribute_list_opt
2569                { $$ = $1->addQualifiers( $2 ); }
2570        | identifier_parameter_ptr
2571        | identifier_parameter_array attribute_list_opt
2572                { $$ = $1->addQualifiers( $2 ); }
2573        | identifier_parameter_function attribute_list_opt
2574                { $$ = $1->addQualifiers( $2 ); }
2575        ;
2576
2577identifier_parameter_ptr:
2578        ptrref_operator identifier_parameter_declarator
2579                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2580        | ptrref_operator type_qualifier_list identifier_parameter_declarator
2581                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2582        | '(' identifier_parameter_ptr ')' attribute_list_opt
2583                { $$ = $2->addQualifiers( $4 ); }
2584        ;
2585
2586identifier_parameter_array:
2587        paren_identifier array_parameter_dimension
2588                { $$ = $1->addArray( $2 ); }
2589        | '(' identifier_parameter_ptr ')' array_dimension
2590                { $$ = $2->addArray( $4 ); }
2591        | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2592                { $$ = $2->addArray( $4 ); }
2593        | '(' identifier_parameter_array ')'                            // redundant parenthesis
2594                { $$ = $2; }
2595        ;
2596
2597identifier_parameter_function:
2598        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2599                { $$ = $1->addParamList( $4 ); }
2600        | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2601                { $$ = $2->addParamList( $6 ); }
2602        | '(' identifier_parameter_function ')'                         // redundant parenthesis
2603                { $$ = $2; }
2604        ;
2605
2606// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2607// e.g.:
2608//
2609//              typedef int foo;
2610//              int f( int foo ); // redefine typedef name in new scope
2611//
2612// and allows the C99 array options, which can only appear in a parameter list.
2613
2614type_parameter_redeclarator:
2615        typedef attribute_list_opt
2616                { $$ = $1->addQualifiers( $2 ); }
2617        | type_parameter_ptr
2618        | type_parameter_array attribute_list_opt
2619                { $$ = $1->addQualifiers( $2 ); }
2620        | type_parameter_function attribute_list_opt
2621                { $$ = $1->addQualifiers( $2 ); }
2622        ;
2623
2624typedef:
2625        TYPEDEFname
2626                {
2627                        typedefTable.setNextIdentifier( *$1 );
2628                        $$ = DeclarationNode::newName( $1 );
2629                }
2630        | TYPEGENname
2631                {
2632                        typedefTable.setNextIdentifier( *$1 );
2633                        $$ = DeclarationNode::newName( $1 );
2634                }
2635        ;
2636
2637type_parameter_ptr:
2638        ptrref_operator type_parameter_redeclarator
2639                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2640        | ptrref_operator type_qualifier_list type_parameter_redeclarator
2641                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2642        | '(' type_parameter_ptr ')' attribute_list_opt
2643                { $$ = $2->addQualifiers( $4 ); }
2644        ;
2645
2646type_parameter_array:
2647        typedef array_parameter_dimension
2648                { $$ = $1->addArray( $2 ); }
2649        | '(' type_parameter_ptr ')' array_parameter_dimension
2650                { $$ = $2->addArray( $4 ); }
2651        ;
2652
2653type_parameter_function:
2654        typedef '(' push parameter_type_list_opt pop ')'        // empty parameter list OBSOLESCENT (see 3)
2655                { $$ = $1->addParamList( $4 ); }
2656        | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2657                { $$ = $2->addParamList( $6 ); }
2658        ;
2659
2660// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2661// which the type applies, e.g.:
2662//
2663//              sizeof( int );
2664//              sizeof( int * );
2665//              sizeof( int [10] );
2666//              sizeof( int (*)() );
2667//              sizeof( int () );
2668//
2669// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2670// and functions versus pointers to arrays and functions.
2671
2672abstract_declarator:
2673        abstract_ptr
2674        | abstract_array attribute_list_opt
2675                { $$ = $1->addQualifiers( $2 ); }
2676        | abstract_function attribute_list_opt
2677                { $$ = $1->addQualifiers( $2 ); }
2678        ;
2679
2680abstract_ptr:
2681        ptrref_operator
2682                { $$ = DeclarationNode::newPointer( 0 ); }
2683        | ptrref_operator type_qualifier_list
2684                { $$ = DeclarationNode::newPointer( $2 ); }
2685        | ptrref_operator abstract_declarator
2686                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2687        | ptrref_operator type_qualifier_list abstract_declarator
2688                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2689        | '(' abstract_ptr ')' attribute_list_opt
2690                { $$ = $2->addQualifiers( $4 ); }
2691        ;
2692
2693abstract_array:
2694        array_dimension
2695        | '(' abstract_ptr ')' array_dimension
2696                { $$ = $2->addArray( $4 ); }
2697        | '(' abstract_array ')' multi_array_dimension          // redundant parenthesis
2698                { $$ = $2->addArray( $4 ); }
2699        | '(' abstract_array ')'                                                        // redundant parenthesis
2700                { $$ = $2; }
2701        ;
2702
2703abstract_function:
2704        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2705                { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
2706        | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2707                { $$ = $2->addParamList( $6 ); }
2708        | '(' abstract_function ')'                                                     // redundant parenthesis
2709                { $$ = $2; }
2710        ;
2711
2712array_dimension:
2713                // Only the first dimension can be empty.
2714        '[' ']'
2715                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2716        | '[' ']' multi_array_dimension
2717                { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
2718        | multi_array_dimension
2719        ;
2720
2721multi_array_dimension:
2722        '[' push assignment_expression pop ']'
2723                { $$ = DeclarationNode::newArray( $3, 0, false ); }
2724        | '[' push '*' pop ']'                                                          // C99
2725                { $$ = DeclarationNode::newVarArray( 0 ); }
2726        | multi_array_dimension '[' push assignment_expression pop ']'
2727                { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2728        | multi_array_dimension '[' push '*' pop ']'            // C99
2729                { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2730        ;
2731
2732// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2733// identifier to which the type applies, e.g.:
2734//
2735//              int f( int );                   // not handled here
2736//              int f( int * );                 // abstract function-prototype parameter; no parameter name specified
2737//              int f( int (*)() );             // abstract function-prototype parameter; no parameter name specified
2738//              int f( int (int) );             // abstract function-prototype parameter; no parameter name specified
2739//
2740// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2741// and functions versus pointers to arrays and functions. In addition, the pattern handles the
2742// special meaning of parenthesis around a typedef name:
2743//
2744//              ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2745//              parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2746//              not as redundant parentheses around the identifier."
2747//
2748// For example:
2749//
2750//              typedef float T;
2751//              int f( int ( T [5] ) );                                 // see abstract_parameter_declarator
2752//              int g( int ( T ( int ) ) );                             // see abstract_parameter_declarator
2753//              int f( int f1( T a[5] ) );                              // see identifier_parameter_declarator
2754//              int g( int g1( T g2( int p ) ) );               // see identifier_parameter_declarator
2755//
2756// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2757// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2758// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2759// functions.
2760
2761abstract_parameter_declarator:
2762        abstract_parameter_ptr
2763        | abstract_parameter_array attribute_list_opt
2764                { $$ = $1->addQualifiers( $2 ); }
2765        | abstract_parameter_function attribute_list_opt
2766                { $$ = $1->addQualifiers( $2 ); }
2767        ;
2768
2769abstract_parameter_ptr:
2770        ptrref_operator
2771                { $$ = DeclarationNode::newPointer( nullptr ); }
2772        | ptrref_operator type_qualifier_list
2773                { $$ = DeclarationNode::newPointer( $2 ); }
2774        | ptrref_operator abstract_parameter_declarator
2775                { $$ = $2->addPointer( DeclarationNode::newPointer( nullptr ) ); }
2776        | ptrref_operator type_qualifier_list abstract_parameter_declarator
2777                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2778        | '(' abstract_parameter_ptr ')' attribute_list_opt
2779                { $$ = $2->addQualifiers( $4 ); }
2780        ;
2781
2782abstract_parameter_array:
2783        array_parameter_dimension
2784        | '(' abstract_parameter_ptr ')' array_parameter_dimension
2785                { $$ = $2->addArray( $4 ); }
2786        | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2787                { $$ = $2->addArray( $4 ); }
2788        | '(' abstract_parameter_array ')'                                      // redundant parenthesis
2789                { $$ = $2; }
2790        ;
2791
2792abstract_parameter_function:
2793        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2794                { $$ = DeclarationNode::newFunction( nullptr, nullptr, $3, nullptr ); }
2795        | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2796                { $$ = $2->addParamList( $6 ); }
2797        | '(' abstract_parameter_function ')'                           // redundant parenthesis
2798                { $$ = $2; }
2799        ;
2800
2801array_parameter_dimension:
2802                // Only the first dimension can be empty or have qualifiers.
2803        array_parameter_1st_dimension
2804        | array_parameter_1st_dimension multi_array_dimension
2805                { $$ = $1->addArray( $2 ); }
2806        | multi_array_dimension
2807        ;
2808
2809// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2810//
2811//              ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2812//              a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
2813
2814array_parameter_1st_dimension:
2815        '[' ']'
2816                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2817        // multi_array_dimension handles the '[' '*' ']' case
2818        | '[' push type_qualifier_list '*' pop ']'                      // remaining C99
2819                { $$ = DeclarationNode::newVarArray( $3 ); }
2820        | '[' push type_qualifier_list pop ']'
2821                { $$ = DeclarationNode::newArray( 0, $3, false ); }
2822        // multi_array_dimension handles the '[' assignment_expression ']' case
2823        | '[' push type_qualifier_list assignment_expression pop ']'
2824                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2825        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2826                { $$ = DeclarationNode::newArray( $5, $4, true ); }
2827        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2828                { $$ = DeclarationNode::newArray( $5, $3, true ); }
2829        ;
2830
2831// This pattern parses a declaration of an abstract variable, but does not allow "int ()" for a function pointer.
2832//
2833//              struct S {
2834//          int;
2835//          int *;
2836//          int [10];
2837//          int (*)();
2838//      };
2839
2840variable_abstract_declarator:
2841        variable_abstract_ptr
2842        | variable_abstract_array attribute_list_opt
2843                { $$ = $1->addQualifiers( $2 ); }
2844        | variable_abstract_function attribute_list_opt
2845                { $$ = $1->addQualifiers( $2 ); }
2846        ;
2847
2848variable_abstract_ptr:
2849        ptrref_operator
2850                { $$ = DeclarationNode::newPointer( 0 ); }
2851        | ptrref_operator type_qualifier_list
2852                { $$ = DeclarationNode::newPointer( $2 ); }
2853        | ptrref_operator variable_abstract_declarator
2854                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2855        | ptrref_operator type_qualifier_list variable_abstract_declarator
2856                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2857        | '(' variable_abstract_ptr ')' attribute_list_opt
2858                { $$ = $2->addQualifiers( $4 ); }
2859        ;
2860
2861variable_abstract_array:
2862        array_dimension
2863        | '(' variable_abstract_ptr ')' array_dimension
2864                { $$ = $2->addArray( $4 ); }
2865        | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
2866                { $$ = $2->addArray( $4 ); }
2867        | '(' variable_abstract_array ')'                                       // redundant parenthesis
2868                { $$ = $2; }
2869        ;
2870
2871variable_abstract_function:
2872        '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2873                { $$ = $2->addParamList( $6 ); }
2874        | '(' variable_abstract_function ')'                            // redundant parenthesis
2875                { $$ = $2; }
2876        ;
2877
2878// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
2879// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
2880
2881cfa_identifier_parameter_declarator_tuple:                              // CFA
2882        cfa_identifier_parameter_declarator_no_tuple
2883        | cfa_abstract_tuple
2884        | type_qualifier_list cfa_abstract_tuple
2885                { $$ = $2->addQualifiers( $1 ); }
2886        ;
2887
2888cfa_identifier_parameter_declarator_no_tuple:                   // CFA
2889        cfa_identifier_parameter_ptr
2890        | cfa_identifier_parameter_array
2891        ;
2892
2893cfa_identifier_parameter_ptr:                                                   // CFA
2894                // No SUE declaration in parameter list.
2895        ptrref_operator type_specifier_nobody
2896                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2897        | type_qualifier_list ptrref_operator type_specifier_nobody
2898                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2899        | ptrref_operator cfa_abstract_function
2900                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2901        | type_qualifier_list ptrref_operator cfa_abstract_function
2902                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2903        | ptrref_operator cfa_identifier_parameter_declarator_tuple
2904                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2905        | type_qualifier_list ptrref_operator cfa_identifier_parameter_declarator_tuple
2906                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2907        ;
2908
2909cfa_identifier_parameter_array:                                                 // CFA
2910                // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
2911                // shift/reduce conflict with new-style empty (void) function return type.
2912        '[' ']' type_specifier_nobody
2913                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2914        | cfa_array_parameter_1st_dimension type_specifier_nobody
2915                { $$ = $2->addNewArray( $1 ); }
2916        | '[' ']' multi_array_dimension type_specifier_nobody
2917                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2918        | cfa_array_parameter_1st_dimension multi_array_dimension type_specifier_nobody
2919                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2920        | multi_array_dimension type_specifier_nobody
2921                { $$ = $2->addNewArray( $1 ); }
2922
2923        | '[' ']' cfa_identifier_parameter_ptr
2924                { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2925        | cfa_array_parameter_1st_dimension cfa_identifier_parameter_ptr
2926                { $$ = $2->addNewArray( $1 ); }
2927        | '[' ']' multi_array_dimension cfa_identifier_parameter_ptr
2928                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2929        | cfa_array_parameter_1st_dimension multi_array_dimension cfa_identifier_parameter_ptr
2930                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2931        | multi_array_dimension cfa_identifier_parameter_ptr
2932                { $$ = $2->addNewArray( $1 ); }
2933        ;
2934
2935cfa_array_parameter_1st_dimension:
2936        '[' push type_qualifier_list '*' pop ']'                        // remaining C99
2937                { $$ = DeclarationNode::newVarArray( $3 ); }
2938        | '[' push type_qualifier_list assignment_expression pop ']'
2939                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2940        | '[' push declaration_qualifier_list assignment_expression pop ']'
2941                // declaration_qualifier_list must be used because of shift/reduce conflict with
2942                // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
2943                // appear in this context.
2944                { $$ = DeclarationNode::newArray( $4, $3, true ); }
2945        | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
2946                { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
2947        ;
2948
2949// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
2950// identifier to which the type applies, e.g.:
2951//
2952//              [int] f( int );                         // abstract variable parameter; no parameter name specified
2953//              [int] f( [int] (int) );         // abstract function-prototype parameter; no parameter name specified
2954//
2955// These rules need LR(3):
2956//
2957//              cfa_abstract_tuple identifier_or_type_name
2958//              '[' cfa_parameter_list ']' identifier_or_type_name '(' cfa_parameter_type_list_opt ')'
2959//
2960// since a function return type can be syntactically identical to a tuple type:
2961//
2962//              [int, int] t;
2963//              [int, int] f( int );
2964//
2965// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
2966// cfa_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
2967// lookahead. To accomplish this, cfa_abstract_declarator has an entry point without tuple, and tuple declarations are
2968// duplicated when appearing with cfa_function_specifier.
2969
2970cfa_abstract_declarator_tuple:                                                  // CFA
2971        cfa_abstract_tuple
2972        | type_qualifier_list cfa_abstract_tuple
2973                { $$ = $2->addQualifiers( $1 ); }
2974        | cfa_abstract_declarator_no_tuple
2975        ;
2976
2977cfa_abstract_declarator_no_tuple:                                               // CFA
2978        cfa_abstract_ptr
2979        | cfa_abstract_array
2980        ;
2981
2982cfa_abstract_ptr:                                                                               // CFA
2983        ptrref_operator type_specifier
2984                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2985        | type_qualifier_list ptrref_operator type_specifier
2986                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2987        | ptrref_operator cfa_abstract_function
2988                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2989        | type_qualifier_list ptrref_operator cfa_abstract_function
2990                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2991        | ptrref_operator cfa_abstract_declarator_tuple
2992                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2993        | type_qualifier_list ptrref_operator cfa_abstract_declarator_tuple
2994                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2995        ;
2996
2997cfa_abstract_array:                                                                             // CFA
2998                // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
2999                // empty (void) function return type.
3000        '[' ']' type_specifier
3001                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3002        | '[' ']' multi_array_dimension type_specifier
3003                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3004        | multi_array_dimension type_specifier
3005                { $$ = $2->addNewArray( $1 ); }
3006        | '[' ']' cfa_abstract_ptr
3007                { $$ = $3->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3008        | '[' ']' multi_array_dimension cfa_abstract_ptr
3009                { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( nullptr, nullptr, false ) ); }
3010        | multi_array_dimension cfa_abstract_ptr
3011                { $$ = $2->addNewArray( $1 ); }
3012        ;
3013
3014cfa_abstract_tuple:                                                                             // CFA
3015        '[' push cfa_abstract_parameter_list pop ']'
3016                { $$ = DeclarationNode::newTuple( $3 ); }
3017        ;
3018
3019cfa_abstract_function:                                                                  // CFA
3020//      '[' ']' '(' cfa_parameter_type_list_opt ')'
3021//              { $$ = DeclarationNode::newFunction( nullptr, DeclarationNode::newTuple( nullptr ), $4, nullptr ); }
3022        cfa_abstract_tuple '(' push cfa_parameter_type_list_opt pop ')'
3023                { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3024        | cfa_function_return '(' push cfa_parameter_type_list_opt pop ')'
3025                { $$ = DeclarationNode::newFunction( nullptr, $1, $4, nullptr ); }
3026        ;
3027
3028// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
3029//    each declaration, and in the specifier-qualifier list in each structure declaration and type name."
3030//
3031// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
3032//    the declaration specifiers in a declaration is an obsolescent feature."
3033//
3034// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
3035//    prototype-format parameter type declarators) is an obsolescent feature."
3036//
3037// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
3038//    declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
3039
3040//************************* MISCELLANEOUS ********************************
3041
3042comma_opt:                                                                                              // redundant comma
3043        // empty
3044        | ','
3045        ;
3046
3047assignment_opt:
3048        // empty
3049                { $$ = nullptr; }
3050        | '=' assignment_expression
3051                { $$ = $2; }
3052        ;
3053
3054%%
3055// ----end of grammar----
3056
3057extern char *yytext;
3058
3059void yyerror( const char * ) {
3060        cout << "Error ";
3061        if ( yyfilename ) {
3062                cout << "in file " << yyfilename << " ";
3063        } // if
3064        cout << "at line " << yylineno << " reading token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << "\"" << endl;
3065}
3066
3067// Local Variables: //
3068// mode: c++ //
3069// tab-width: 4 //
3070// compile-command: "make install" //
3071// End: //
Note: See TracBrowser for help on using the repository browser.