source: src/Parser/parser.yy @ 9236060

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 9236060 was 9236060, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' into references

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