source: src/Parser/parser.yy @ 533804b

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

Remove lvalue keyword from the parser

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