source: src/Parser/parser.yy @ b128d3e

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

remove contraction from error message

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