source: src/Parser/parser.yy @ 6165ce7

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

remove old zero/one constant, replaced by zero_t/one_t types

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