source: src/Parser/parser.yy @ e497c1d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since e497c1d was 1db21619, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add CFA flag, remove -p from cc1, typedef on functions become function declarations, move isInline/isNoreturn to Declaration, cleaned up label code

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