source: src/Parser/parser.yy @ cbce272

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 cbce272 was cbce272, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Structure based exception handling.

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