source: src/Parser/parser.yy @ ebb5ed9

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

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

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