source: src/Parser/parser.yy @ 3eab0ef6

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 3eab0ef6 was 5b2edbc, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add waitfor statement

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