source: src/Parser/parser.yy @ 365da371

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

dtor operator syntax is now a void-typed expression

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