source: src/Parser/parser.yy @ b3b2077

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

build appropriate nodes when parsing member tuple expressions

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