source: src/Parser/parser.yy @ 7f612112

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

parse default values for generic type parameters and nested type names

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