source: src/Parser/parser.yy @ 0853178

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 0853178 was 51e076e, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more refactoring of parser code

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