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

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 6b224a52 was 6b224a52, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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