source: src/Parser/parser.yy @ e5609dd

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 e5609dd was 2871210, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

include file with keywords, fix type of label address expressions, fix computed goto to any expressions, generic types first attempt

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