source: src/Parser/parser.yy @ 94ad12f

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 94ad12f was e994912, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

code generation for external asm statement (declaration)

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