source: src/Parser/parser.yy @ de62360d

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

fix computed goto, fixed -std=, implicit typedefs for enum and aggregates, add _Noreturn _Thread_local

  • Property mode set to 100644
File size: 99.3 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
[de62360d]12// Last Modified On : Mon Jun 22 15:19:44 2015
13// Update Count     : 1082
[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
120%type<tok> identifier_or_typedef_name  no_attr_identifier_or_typedef_name  no_01_identifier_or_typedef_name
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
206%type<decl> paren_identifier paren_typedef
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
215%type<decl> typedef typedef_array typedef_declaration typedef_declaration_specifier typedef_expression
216%type<decl> typedef_function typedef_parameter_array typedef_parameter_function typedef_parameter_ptr
217%type<decl> typedef_parameter_redeclarator typedef_ptr typedef_redeclarator typedef_type_specifier
218%type<decl> typegen_declaration_specifier typegen_type_specifier
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 ); }
378                // Only a list of no_attr_identifier_or_typedef_name is allowed in this context. However, there is insufficient
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
[4d51835]566        '[' push pop ']'
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
[4d51835]926        no_attr_identifier_or_typedef_name                      {}
927        | label_list ',' no_attr_identifier_or_typedef_name {}
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                }
967        | new_variable_declaration pop ',' push identifier_or_typedef_name initializer_opt
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
[4d51835]977        new_abstract_declarator_no_tuple identifier_or_typedef_name asm_name_opt
978                {
979                        typedefTable.setNextIdentifier( *$2 );
980                        $$ = $1->addName( $2 );
981                }
982        | new_abstract_tuple identifier_or_typedef_name asm_name_opt
983                {
984                        typedefTable.setNextIdentifier( *$2 );
985                        $$ = $1->addName( $2 );
986                }
987        | type_qualifier_list new_abstract_tuple identifier_or_typedef_name asm_name_opt
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                }
1015        | new_function_declaration pop ',' push identifier_or_typedef_name
1016                {
1017                        typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
1018                        $$ = $1->appendList( $1->cloneType( $5 ) );
1019                }
1020        ;
[b87a5ed]1021
1022new_function_specifier:                                                                 // CFA
[4d51835]1023        '[' push pop ']' identifier '(' push new_parameter_type_list_opt pop ')'
1024                {
[de62360d]1025                        typedefTable.setNextIdentifier( *( $5 ) );
[4d51835]1026                        $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1027                }
1028        | '[' push pop ']' TYPEDEFname '(' push new_parameter_type_list_opt pop ')'
1029                {
[de62360d]1030                        typedefTable.setNextIdentifier( *( $5 ) );
[4d51835]1031                        $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1032                }
1033                // identifier_or_typedef_name must be broken apart because of the sequence:
1034                //
1035                //   '[' ']' identifier_or_typedef_name '(' new_parameter_type_list_opt ')'
1036                //   '[' ']' type_specifier
1037                //
[de62360d]1038                // type_specifier can resolve to just TYPEDEFname (e.g. typedef int T; int f( T );). Therefore this must be
1039                // flattened to allow lookahead to the '(' without having to reduce identifier_or_typedef_name.
[4d51835]1040        | new_abstract_tuple identifier_or_typedef_name '(' push new_parameter_type_list_opt pop ')'
[de62360d]1041                // To obtain LR(1 ), this rule must be factored out from function return type (see new_abstract_declarator).
[4d51835]1042                {
1043                        $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1044                }
1045        | new_function_return identifier_or_typedef_name '(' push new_parameter_type_list_opt pop ')'
1046                {
1047                        $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1048                }
1049        ;
[b87a5ed]1050
1051new_function_return:                                                                    // CFA
[4d51835]1052        '[' push new_parameter_list pop ']'
1053                { $$ = DeclarationNode::newTuple( $3 ); }
1054        | '[' push new_parameter_list pop ',' push new_abstract_parameter_list pop ']'
[de62360d]1055                // To obtain LR(1 ), the last new_abstract_parameter_list is added into this flattened rule to lookahead to the
1056                // ']'.
[4d51835]1057                { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1058        ;
[b87a5ed]1059
1060new_typedef_declaration:                                                                // CFA
[4d51835]1061        TYPEDEF new_variable_specifier
1062                {
[de62360d]1063                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1064                        $$ = $2->addTypedef();
1065                }
1066        | TYPEDEF new_function_specifier
1067                {
[de62360d]1068                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1069                        $$ = $2->addTypedef();
1070                }
1071        | new_typedef_declaration pop ',' push no_attr_identifier
1072                {
[de62360d]1073                        typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
[4d51835]1074                        $$ = $1->appendList( $1->cloneType( $5 ) );
1075                }
1076        ;
[b87a5ed]1077
[de62360d]1078// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1079// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
[51b7345]1080
1081typedef_declaration:
[4d51835]1082        TYPEDEF type_specifier declarator
1083                {
[de62360d]1084                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1085                        $$ = $3->addType( $2 )->addTypedef();
1086                }
1087        | typedef_declaration pop ',' push declarator
1088                {
[de62360d]1089                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1090                        $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1091                }
[de62360d]1092        | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
[4d51835]1093                {
[de62360d]1094                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1095                        $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1096                }
1097        | type_specifier TYPEDEF declarator
1098                {
[de62360d]1099                        typedefTable.addToEnclosingScope( TypedefTable::TD );
[4d51835]1100                        $$ = $3->addType( $1 )->addTypedef();
1101                }
1102        | type_specifier TYPEDEF type_qualifier_list declarator
1103                {
[de62360d]1104                        typedefTable.addToEnclosingScope( TypedefTable::TD );
1105                        $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
[4d51835]1106                }
1107        ;
[b87a5ed]1108
1109typedef_expression:                                                                             // GCC, naming expression type
[4d51835]1110        TYPEDEF no_attr_identifier '=' assignment_expression
1111                {
[de62360d]1112                        typedefTable.addToEnclosingScope( *$2, TypedefTable::TD );
[4d51835]1113                        $$ = DeclarationNode::newName( 0 ); // XXX
1114                }
1115        | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
1116                {
[de62360d]1117                        typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
[4d51835]1118                        $$ = DeclarationNode::newName( 0 ); // XXX
1119                }
1120        ;
[51b7345]1121
1122old_declaration:
[4d51835]1123        declaring_list pop ';'
1124        | typedef_declaration pop ';'
1125        | typedef_expression pop ';'                                            // GCC, naming expression type
1126        | sue_declaration_specifier pop ';'
1127        ;
[51b7345]1128
1129declaring_list:
[de62360d]1130                // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1131                // storage-class
[4d51835]1132        declaration_specifier declarator asm_name_opt initializer_opt
1133                {
1134                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[de62360d]1135                        $$ = ( $2->addType( $1 ))->addInitializer( $4 );
[4d51835]1136                }
1137        | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1138                {
1139                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[de62360d]1140                        $$ = $1->appendList( $1->cloneBaseType( $4->addInitializer( $6 ) ) );
[4d51835]1141                }
1142        ;
[b87a5ed]1143
1144declaration_specifier:                                                                  // type specifier + storage class
[4d51835]1145        basic_declaration_specifier
1146        | sue_declaration_specifier
1147        | typedef_declaration_specifier
1148        | typegen_declaration_specifier
1149        ;
[b87a5ed]1150
1151type_specifier:                                                                                 // declaration specifier - storage class
[4d51835]1152        basic_type_specifier
1153        | sue_type_specifier
1154        | typedef_type_specifier
1155        | typegen_type_specifier
1156        ;
[b87a5ed]1157
1158type_qualifier_list_opt:                                                                // GCC, used in asm_statement
[4d51835]1159        // empty
1160                { $$ = 0; }
1161        | type_qualifier_list
1162        ;
[51b7345]1163
1164type_qualifier_list:
[de62360d]1165                // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
[4d51835]1166                //
[de62360d]1167                // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1168                // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1169                // appeared only once.
[4d51835]1170        type_qualifier
1171        | type_qualifier_list type_qualifier
1172                { $$ = $1->addQualifiers( $2 ); }
1173        ;
[51b7345]1174
1175type_qualifier:
[4d51835]1176        type_qualifier_name
1177        | attribute
1178                { $$ = DeclarationNode::newQualifier( DeclarationNode::Attribute ); }
1179        ;
[51b7345]1180
1181type_qualifier_name:
[4d51835]1182        CONST
1183                { $$ = DeclarationNode::newQualifier( DeclarationNode::Const ); }
1184        | RESTRICT
1185                { $$ = DeclarationNode::newQualifier( DeclarationNode::Restrict ); }
1186        | VOLATILE
1187                { $$ = DeclarationNode::newQualifier( DeclarationNode::Volatile ); }
1188        | LVALUE                                                                                        // CFA
1189                { $$ = DeclarationNode::newQualifier( DeclarationNode::Lvalue ); }
1190        | ATOMIC
1191                { $$ = DeclarationNode::newQualifier( DeclarationNode::Atomic ); }
1192        | FORALL '('
1193                {
1194                        typedefTable.enterScope();
1195                }
1196          type_parameter_list ')'                                                       // CFA
1197                {
1198                        typedefTable.leaveScope();
1199                        $$ = DeclarationNode::newForall( $4 );
1200                }
1201        ;
[51b7345]1202
1203declaration_qualifier_list:
[4d51835]1204        storage_class_list
[de62360d]1205        | type_qualifier_list storage_class_list                        // remaining OBSOLESCENT (see 2 )
[4d51835]1206                { $$ = $1->addQualifiers( $2 ); }
1207        | declaration_qualifier_list type_qualifier_list storage_class_list
1208                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1209        ;
[51b7345]1210
1211storage_class_list:
[de62360d]1212                // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1213                // only one of each is specified, except for inline, which can appear with the others.
[4d51835]1214                //
[de62360d]1215                // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1216                // specifiers in a declaration.
[4d51835]1217        storage_class
1218        | storage_class_list storage_class
1219                { $$ = $1->addQualifiers( $2 ); }
1220        ;
[51b7345]1221
1222storage_class:
[4d51835]1223        storage_class_name
1224        ;
[51b7345]1225
1226storage_class_name:
[4d51835]1227        EXTERN
1228                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Extern ); }
1229        | STATIC
1230                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Static ); }
1231        | AUTO
1232                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Auto ); }
1233        | REGISTER
1234                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Register ); }
1235        | INLINE                                                                                        // C99
1236                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Inline ); }
1237        | FORTRAN                                                                                       // C99
1238                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Fortran ); }
[68cd1ce]1239        | NORETURN                                                                                      // C11
1240                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Noreturn ); }
1241        | THREADLOCAL                                                                           // C11
1242                { $$ = DeclarationNode::newStorageClass( DeclarationNode::Threadlocal ); }
[4d51835]1243        ;
[51b7345]1244
1245basic_type_name:
[4d51835]1246        CHAR
1247                { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
1248        | DOUBLE
1249                { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
1250        | FLOAT
1251                { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
1252        | INT
1253                { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
1254        | LONG
1255                { $$ = DeclarationNode::newModifier( DeclarationNode::Long ); }
1256        | SHORT
1257                { $$ = DeclarationNode::newModifier( DeclarationNode::Short ); }
1258        | SIGNED
1259                { $$ = DeclarationNode::newModifier( DeclarationNode::Signed ); }
1260        | UNSIGNED
1261                { $$ = DeclarationNode::newModifier( DeclarationNode::Unsigned ); }
1262        | VOID
1263                { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1264        | BOOL                                                                                          // C99
1265                { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
1266        | COMPLEX                                                                                       // C99
1267                { $$ = DeclarationNode::newBasicType( DeclarationNode::Complex ); }
1268        | IMAGINARY                                                                                     // C99
1269                { $$ = DeclarationNode::newBasicType( DeclarationNode::Imaginary ); }
1270        ;
[51b7345]1271
1272basic_declaration_specifier:
[4d51835]1273                // A semantic check is necessary for conflicting storage classes.
1274        basic_type_specifier
1275        | declaration_qualifier_list basic_type_specifier
1276                { $$ = $2->addQualifiers( $1 ); }
1277        | basic_declaration_specifier storage_class                     // remaining OBSOLESCENT (see 2)
1278                { $$ = $1->addQualifiers( $2 ); }
1279        | basic_declaration_specifier storage_class type_qualifier_list
1280                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1281        | basic_declaration_specifier storage_class basic_type_specifier
1282                { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
1283        ;
[51b7345]1284
1285basic_type_specifier:
[4d51835]1286        direct_type_name
1287        | type_qualifier_list_opt indirect_type_name type_qualifier_list_opt
1288                { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
1289        ;
[51b7345]1290
1291direct_type_name:
[4d51835]1292                // A semantic check is necessary for conflicting type qualifiers.
1293        basic_type_name
1294        | type_qualifier_list basic_type_name
1295                { $$ = $2->addQualifiers( $1 ); }
1296        | direct_type_name type_qualifier
1297                { $$ = $1->addQualifiers( $2 ); }
1298        | direct_type_name basic_type_name
1299                { $$ = $1->addType( $2 ); }
1300        ;
[51b7345]1301
1302indirect_type_name:
[4d51835]1303        TYPEOF '(' type_name ')'                                                        // GCC: typeof(x) y;
1304                { $$ = $3; }
1305        | TYPEOF '(' comma_expression ')'                                       // GCC: typeof(a+b) y;
1306                { $$ = DeclarationNode::newTypeof( $3 ); }
1307        | ATTR_TYPEGENname '(' type_name ')'                            // CFA: e.g., @type(x) y;
1308                { $$ = DeclarationNode::newAttr( $1, $3 ); }
1309        | ATTR_TYPEGENname '(' comma_expression ')'                     // CFA: e.g., @type(a+b) y;
1310                { $$ = DeclarationNode::newAttr( $1, $3 ); }
1311        ;
[51b7345]1312
1313sue_declaration_specifier:
[4d51835]1314        sue_type_specifier
1315        | declaration_qualifier_list sue_type_specifier
1316                { $$ = $2->addQualifiers( $1 ); }
1317        | sue_declaration_specifier storage_class                       // remaining OBSOLESCENT (see 2)
1318                { $$ = $1->addQualifiers( $2 ); }
1319        | sue_declaration_specifier storage_class type_qualifier_list
1320                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1321        ;
[51b7345]1322
1323sue_type_specifier:
[4d51835]1324        elaborated_type_name                                                            // struct, union, enum
1325        | type_qualifier_list elaborated_type_name
1326                { $$ = $2->addQualifiers( $1 ); }
1327        | sue_type_specifier type_qualifier
1328                { $$ = $1->addQualifiers( $2 ); }
1329        ;
[51b7345]1330
1331typedef_declaration_specifier:
[4d51835]1332        typedef_type_specifier
1333        | declaration_qualifier_list typedef_type_specifier
1334                { $$ = $2->addQualifiers( $1 ); }
1335        | typedef_declaration_specifier storage_class           // remaining OBSOLESCENT (see 2)
1336                { $$ = $1->addQualifiers( $2 ); }
1337        | typedef_declaration_specifier storage_class type_qualifier_list
1338                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1339        ;
[b87a5ed]1340
1341typedef_type_specifier:                                                                 // typedef types
[4d51835]1342        TYPEDEFname
1343                { $$ = DeclarationNode::newFromTypedef( $1 ); }
1344        | type_qualifier_list TYPEDEFname
1345                { $$ = DeclarationNode::newFromTypedef( $2 )->addQualifiers( $1 ); }
1346        | typedef_type_specifier type_qualifier
1347                { $$ = $1->addQualifiers( $2 ); }
1348        ;
[51b7345]1349
1350elaborated_type_name:
[4d51835]1351        aggregate_name
1352        | enum_name
1353        ;
[51b7345]1354
1355aggregate_name:
[4d51835]1356        aggregate_key '{' field_declaration_list '}'
1357                { $$ = DeclarationNode::newAggregate( $1, 0, 0, 0, $3 ); }
1358        | aggregate_key no_attr_identifier_or_typedef_name
1359                { $$ = DeclarationNode::newAggregate( $1, $2, 0, 0, 0 ); }
1360        | aggregate_key no_attr_identifier_or_typedef_name '{' field_declaration_list '}'
1361                { $$ = DeclarationNode::newAggregate( $1, $2, 0, 0, $4 ); }
1362        | aggregate_key '(' push type_parameter_list pop ')' '{' field_declaration_list '}' // CFA
1363                { $$ = DeclarationNode::newAggregate( $1, 0, $4, 0, $8 ); }
1364        | aggregate_key '(' push type_parameter_list pop ')' no_attr_identifier_or_typedef_name // CFA
1365                { $$ = DeclarationNode::newAggregate( $1, $7, $4, 0, 0 ); }
1366        | aggregate_key '(' push type_parameter_list pop ')' no_attr_identifier_or_typedef_name '{' field_declaration_list '}' // CFA
1367                { $$ = DeclarationNode::newAggregate( $1, $7, $4, 0, $9 ); }
1368        | aggregate_key '(' push type_parameter_list pop ')' '(' type_name_list ')' '{' field_declaration_list '}' // CFA
1369                { $$ = DeclarationNode::newAggregate( $1, 0, $4, $8, $11 ); }
[de62360d]1370        | aggregate_key TYPEGENname '(' type_name_list ')' // CFA
1371                {}
[4d51835]1372        | aggregate_key '(' push type_name_list pop ')' no_attr_identifier_or_typedef_name // CFA
1373                // push and pop are only to prevent S/R conflicts
1374                { $$ = DeclarationNode::newAggregate( $1, $7, 0, $4, 0 ); }
1375        | aggregate_key '(' push type_parameter_list pop ')' '(' type_name_list ')' no_attr_identifier_or_typedef_name '{' field_declaration_list '}' // CFA
1376                { $$ = DeclarationNode::newAggregate( $1, $10, $4, $8, $12 ); }
1377        ;
[51b7345]1378
1379aggregate_key:
[4d51835]1380        STRUCT attribute_list_opt
1381                { $$ = DeclarationNode::Struct; }
1382        | UNION attribute_list_opt
1383                { $$ = DeclarationNode::Union; }
1384        ;
[51b7345]1385
1386field_declaration_list:
[4d51835]1387        field_declaration
1388                { $$ = $1; }
1389        | field_declaration_list field_declaration
1390                { $$ = $1->appendList( $2 ); }
1391        ;
[51b7345]1392
1393field_declaration:
[4d51835]1394        new_field_declaring_list ';'                                            // CFA, new style field declaration
1395        | EXTENSION new_field_declaring_list ';'                        // GCC
1396                { $$ = $2; }
1397        | field_declaring_list ';'
1398        | EXTENSION field_declaring_list ';'                            // GCC
1399                { $$ = $2; }
1400        ;
[b87a5ed]1401
1402new_field_declaring_list:                                                               // CFA, new style field declaration
[4d51835]1403        new_abstract_declarator_tuple                                           // CFA, no field name
1404        | new_abstract_declarator_tuple no_attr_identifier_or_typedef_name
1405                { $$ = $1->addName( $2 ); }
1406        | new_field_declaring_list ',' no_attr_identifier_or_typedef_name
1407                { $$ = $1->appendList( $1->cloneType( $3 ) ); }
1408        | new_field_declaring_list ','                                          // CFA, no field name
1409                { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1410        ;
[51b7345]1411
1412field_declaring_list:
[4d51835]1413        type_specifier field_declarator
1414                { $$ = $2->addType( $1 ); }
1415        | field_declaring_list ',' attribute_list_opt field_declarator
1416                { $$ = $1->appendList( $1->cloneBaseType( $4 ) ); }
1417        ;
[51b7345]1418
1419field_declarator:
[4d51835]1420        // empty
1421                { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
1422        | bit_subrange_size                                                                     // no field name
1423                { $$ = DeclarationNode::newBitfield( $1 ); }
1424        | variable_declarator bit_subrange_size_opt
1425                // A semantic check is required to ensure bit_subrange only appears on base type int.
1426                { $$ = $1->addBitfield( $2 ); }
1427        | typedef_redeclarator bit_subrange_size_opt
1428                // A semantic check is required to ensure bit_subrange only appears on base type int.
1429                { $$ = $1->addBitfield( $2 ); }
1430        | variable_abstract_declarator                                          // CFA, no field name
1431        ;
[51b7345]1432
1433bit_subrange_size_opt:
[4d51835]1434        // empty
1435                { $$ = 0; }
1436        | bit_subrange_size
1437                { $$ = $1; }
1438        ;
[51b7345]1439
1440bit_subrange_size:
[4d51835]1441        ':' constant_expression
1442                { $$ = $2; }
1443        ;
[51b7345]1444
1445enum_key:
[4d51835]1446        ENUM attribute_list_opt
1447        ;
[51b7345]1448
1449enum_name:
[4d51835]1450        enum_key '{' enumerator_list comma_opt '}'
1451                { $$ = DeclarationNode::newEnum( 0, $3 ); }
1452        | enum_key no_attr_identifier_or_typedef_name '{' enumerator_list comma_opt '}'
1453                { $$ = DeclarationNode::newEnum( $2, $4 ); }
1454        | enum_key no_attr_identifier_or_typedef_name
1455                { $$ = DeclarationNode::newEnum( $2, 0 ); }
1456        ;
[51b7345]1457
1458enumerator_list:
[4d51835]1459        no_attr_identifier_or_typedef_name enumerator_value_opt
1460                { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
1461        | enumerator_list ',' no_attr_identifier_or_typedef_name enumerator_value_opt
1462                { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1463        ;
[51b7345]1464
1465enumerator_value_opt:
[4d51835]1466        // empty
1467                { $$ = 0; }
1468        | '=' constant_expression
1469                { $$ = $2; }
1470        ;
[51b7345]1471
[c11e31c]1472// Minimum of one parameter after which ellipsis is allowed only at the end.
[51b7345]1473
[b87a5ed]1474new_parameter_type_list_opt:                                                    // CFA
[4d51835]1475        // empty
1476                { $$ = 0; }
1477        | new_parameter_type_list
1478        ;
[b87a5ed]1479
1480new_parameter_type_list:                                                                // CFA, abstract + real
[4d51835]1481        new_abstract_parameter_list
1482        | new_parameter_list
1483        | new_parameter_list pop ',' push new_abstract_parameter_list
1484                { $$ = $1->appendList( $5 ); }
1485        | new_abstract_parameter_list pop ',' push ELLIPSIS
1486                { $$ = $1->addVarArgs(); }
1487        | new_parameter_list pop ',' push ELLIPSIS
1488                { $$ = $1->addVarArgs(); }
1489        ;
[b87a5ed]1490
1491new_parameter_list:                                                                             // CFA
[de62360d]1492                // To obtain LR(1) between new_parameter_list and new_abstract_tuple, the last new_abstract_parameter_list is
1493                // factored out from new_parameter_list, flattening the rules to get lookahead to the ']'.
[4d51835]1494        new_parameter_declaration
1495        | new_abstract_parameter_list pop ',' push new_parameter_declaration
1496                { $$ = $1->appendList( $5 ); }
1497        | new_parameter_list pop ',' push new_parameter_declaration
1498                { $$ = $1->appendList( $5 ); }
1499        | new_parameter_list pop ',' push new_abstract_parameter_list pop ',' push new_parameter_declaration
1500                { $$ = $1->appendList( $5 )->appendList( $9 ); }
1501        ;
[b87a5ed]1502
1503new_abstract_parameter_list:                                                    // CFA, new & old style abstract
[4d51835]1504        new_abstract_parameter_declaration
1505        | new_abstract_parameter_list pop ',' push new_abstract_parameter_declaration
1506                { $$ = $1->appendList( $5 ); }
1507        ;
[51b7345]1508
1509parameter_type_list_opt:
[4d51835]1510        // empty
1511                { $$ = 0; }
1512        | parameter_type_list
1513        ;
[51b7345]1514
1515parameter_type_list:
[4d51835]1516        parameter_list
1517        | parameter_list pop ',' push ELLIPSIS
1518                { $$ = $1->addVarArgs(); }
1519        ;
[b87a5ed]1520
1521parameter_list:                                                                                 // abstract + real
[4d51835]1522        abstract_parameter_declaration
1523        | parameter_declaration
1524        | parameter_list pop ',' push abstract_parameter_declaration
1525                { $$ = $1->appendList( $5 ); }
1526        | parameter_list pop ',' push parameter_declaration
1527                { $$ = $1->appendList( $5 ); }
1528        ;
[51b7345]1529
[de62360d]1530// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
1531// for typedef name by using typedef_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
[51b7345]1532
[b87a5ed]1533new_parameter_declaration:                                                              // CFA, new & old style parameter declaration
[4d51835]1534        parameter_declaration
1535        | new_identifier_parameter_declarator_no_tuple identifier_or_typedef_name assignment_opt
1536                { $$ = $1->addName( $2 ); }
1537        | new_abstract_tuple identifier_or_typedef_name assignment_opt
1538                // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1539                { $$ = $1->addName( $2 ); }
1540        | type_qualifier_list new_abstract_tuple identifier_or_typedef_name assignment_opt
1541                { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
1542        | new_function_specifier
1543        ;
[b87a5ed]1544
1545new_abstract_parameter_declaration:                                             // CFA, new & old style parameter declaration
[4d51835]1546        abstract_parameter_declaration
1547        | new_identifier_parameter_declarator_no_tuple
1548        | new_abstract_tuple
1549                // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1550        | type_qualifier_list new_abstract_tuple
1551                { $$ = $2->addQualifiers( $1 ); }
1552        | new_abstract_function
1553        ;
[51b7345]1554
1555parameter_declaration:
[4d51835]1556        declaration_specifier identifier_parameter_declarator assignment_opt
1557                {
1558                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[de62360d]1559                        $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
[4d51835]1560                }
1561        | declaration_specifier typedef_parameter_redeclarator assignment_opt
1562                {
1563                        typedefTable.addToEnclosingScope( TypedefTable::ID );
[de62360d]1564                        $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
[4d51835]1565                }
1566        ;
[51b7345]1567
1568abstract_parameter_declaration:
[4d51835]1569        declaration_specifier
1570        | declaration_specifier abstract_parameter_declarator
1571                { $$ = $2->addType( $1 ); }
1572        ;
[51b7345]1573
[c11e31c]1574// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
[de62360d]1575// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
1576// identifiers.  The ANSI-style parameter-list can redefine a typedef name.
[51b7345]1577
[b87a5ed]1578identifier_list:                                                                                // K&R-style parameter list => no types
[4d51835]1579        no_attr_identifier
1580                { $$ = DeclarationNode::newName( $1 ); }
1581        | identifier_list ',' no_attr_identifier
1582                { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
1583        ;
[51b7345]1584
1585identifier_or_typedef_name:
[4d51835]1586        identifier
1587        | TYPEDEFname
1588        | TYPEGENname
1589        ;
[51b7345]1590
1591no_01_identifier_or_typedef_name:
[4d51835]1592        no_01_identifier
1593        | TYPEDEFname
1594        | TYPEGENname
1595        ;
[51b7345]1596
1597no_attr_identifier_or_typedef_name:
[4d51835]1598        no_attr_identifier
1599        | TYPEDEFname
[de62360d]1600        //      | TYPEGENname
[4d51835]1601        ;
[b87a5ed]1602
1603type_name_no_function:                                                                  // sizeof, alignof, cast (constructor)
[4d51835]1604        new_abstract_declarator_tuple                                           // CFA
1605        | type_specifier
1606        | type_specifier variable_abstract_declarator
1607                { $$ = $2->addType( $1 ); }
1608        ;
[b87a5ed]1609
1610type_name:                                                                                              // typeof, assertion
[4d51835]1611        new_abstract_declarator_tuple                                           // CFA
1612        | new_abstract_function                                                         // CFA
1613        | type_specifier
1614        | type_specifier abstract_declarator
1615                { $$ = $2->addType( $1 ); }
1616        ;
[51b7345]1617
1618initializer_opt:
[4d51835]1619        // empty
1620                { $$ = 0; }
1621        | '=' initializer                                                       { $$ = $2; }
1622        ;
[51b7345]1623
1624initializer:
[de62360d]1625        assignment_expression                                           { $$ = new InitializerNode( $1 ); }
1626        | '{' initializer_list comma_opt '}'            { $$ = new InitializerNode( $2, true ); }
[4d51835]1627        ;
[51b7345]1628
1629initializer_list:
[4d51835]1630        initializer
1631        | designation initializer                                       { $$ = $2->set_designators( $1 ); }
[de62360d]1632        | initializer_list ',' initializer                      { $$ = (InitializerNode *)( $1->set_link( $3 ) ); }
[4d51835]1633        | initializer_list ',' designation initializer
[de62360d]1634                { $$ = (InitializerNode *)( $1->set_link( $4->set_designators( $3 ) ) ); }
[4d51835]1635        ;
[b87a5ed]1636
[de62360d]1637// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
1638// '=' to separator the designator from the initializer value, as in:
[c11e31c]1639//
[b87a5ed]1640//              int x[10] = { [1] = 3 };
[c11e31c]1641//
[de62360d]1642// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment.  To disambiguate this case, CFA
1643// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
1644// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
1645// shift/reduce conflicts
[51b7345]1646
1647designation:
[4d51835]1648        designator_list ':'                                                                     // C99, CFA uses ":" instead of "="
1649        | no_attr_identifier_or_typedef_name ':'                        // GCC, field name
1650                                { $$ = new VarRefNode( $1 ); }
1651        ;
[51b7345]1652
[b87a5ed]1653designator_list:                                                                                // C99
[4d51835]1654        designator
[de62360d]1655        | designator_list designator                                    { $$ = (ExpressionNode *)( $1->set_link( $2 )); }
1656        //| designator_list designator                                          { $$ = new CompositeExprNode( $1, $2 ); }
[4d51835]1657        ;
[51b7345]1658
1659designator:
[4d51835]1660        '.' no_attr_identifier_or_typedef_name                          // C99, field name
1661                { $$ = new VarRefNode( $2 ); }
1662        | '[' push assignment_expression pop ']'                        // C99, single array element
[de62360d]1663                // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
[4d51835]1664                { $$ = $3; }
1665        | '[' push subrange pop ']'                                                     // CFA, multiple array elements
1666                { $$ = $3; }
1667        | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
[de62360d]1668                { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Range ), $3, $5 ); }
[4d51835]1669        | '.' '[' push field_list pop ']'                                       // CFA, tuple field selector
1670                { $$ = $4; }
1671        ;
[51b7345]1672
[de62360d]1673// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
1674// rather than an object-oriented type system. This required four groups of extensions:
[c11e31c]1675//
1676// Overloading: function, data, and operator identifiers may be overloaded.
1677//
[de62360d]1678// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
1679//     and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
1680//     definitions of new types. Type declarations with storage class "extern" provide opaque types.
[c11e31c]1681//
[de62360d]1682// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
1683//     site. A polymorphic function is not a template; it is a function, with an address and a type.
[c11e31c]1684//
1685// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
[de62360d]1686//     types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
1687//     hierarchies. Unlike classes, they can define relationships between types.  Assertions declare that a type or
1688//     types provide the operations declared by a specification.  Assertions are normally used to declare requirements
1689//     on type arguments of polymorphic functions.
[c11e31c]1690
[b87a5ed]1691typegen_declaration_specifier:                                                  // CFA
[4d51835]1692        typegen_type_specifier
1693        | declaration_qualifier_list typegen_type_specifier
1694                { $$ = $2->addQualifiers( $1 ); }
1695        | typegen_declaration_specifier storage_class           // remaining OBSOLESCENT (see 2)
1696                { $$ = $1->addQualifiers( $2 ); }
1697        | typegen_declaration_specifier storage_class type_qualifier_list
1698                { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1699        ;
[b87a5ed]1700
1701typegen_type_specifier:                                                                 // CFA
[4d51835]1702        TYPEGENname '(' type_name_list ')'
1703                { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
1704        | type_qualifier_list TYPEGENname '(' type_name_list ')'
1705                { $$ = DeclarationNode::newFromTypeGen( $2, $4 )->addQualifiers( $1 ); }
1706        | typegen_type_specifier type_qualifier
1707                { $$ = $1->addQualifiers( $2 ); }
1708        ;
[b87a5ed]1709
1710type_parameter_list:                                                                    // CFA
[4d51835]1711        type_parameter assignment_opt
1712        | type_parameter_list ',' type_parameter assignment_opt
1713                { $$ = $1->appendList( $3 ); }
1714        ;
[b87a5ed]1715
1716type_parameter:                                                                                 // CFA
[4d51835]1717        type_class no_attr_identifier_or_typedef_name
[de62360d]1718                { typedefTable.addToEnclosingScope(*( $2 ), TypedefTable::TD ); }
[4d51835]1719          assertion_list_opt
1720                { $$ = DeclarationNode::newTypeParam( $1, $2 )->addAssertions( $4 ); }
1721        | type_specifier identifier_parameter_declarator
1722        ;
[b87a5ed]1723
1724type_class:                                                                                             // CFA
[4d51835]1725        TYPE
1726                { $$ = DeclarationNode::Type; }
1727        | DTYPE
1728                { $$ = DeclarationNode::Ftype; }
1729        | FTYPE
1730                { $$ = DeclarationNode::Dtype; }
1731        ;
[b87a5ed]1732
1733assertion_list_opt:                                                                             // CFA
[4d51835]1734        // empty
1735                { $$ = 0; }
1736        | assertion_list_opt assertion
1737                { $$ = $1 == 0 ? $2 : $1->appendList( $2 ); }
1738        ;
[b87a5ed]1739
1740assertion:                                                                                              // CFA
[4d51835]1741        '|' no_attr_identifier_or_typedef_name '(' type_name_list ')'
1742                {
[de62360d]1743                        typedefTable.openContext( *( $2 ) );
[4d51835]1744                        $$ = DeclarationNode::newContextUse( $2, $4 );
1745                }
1746        | '|' '{' push context_declaration_list '}'
1747                { $$ = $4; }
1748        | '|' '(' push type_parameter_list pop ')' '{' push context_declaration_list '}' '(' type_name_list ')'
1749                { $$ = 0; }
1750        ;
[b87a5ed]1751
1752type_name_list:                                                                                 // CFA
[4d51835]1753        type_name
1754                { $$ = new TypeValueNode( $1 ); }
1755        | assignment_expression
1756        | type_name_list ',' type_name
[de62360d]1757                { $$ = (ExpressionNode *)( $1->set_link( new TypeValueNode( $3 ))); }
[4d51835]1758        | type_name_list ',' assignment_expression
[de62360d]1759                { $$ = (ExpressionNode *)( $1->set_link( $3 )); }
[4d51835]1760        ;
[b87a5ed]1761
1762type_declaring_list:                                                                    // CFA
[4d51835]1763        TYPE type_declarator
1764                { $$ = $2; }
1765        | storage_class_list TYPE type_declarator
1766                { $$ = $3->addQualifiers( $1 ); }
1767        | type_declaring_list ',' type_declarator
1768                { $$ = $1->appendList( $3->copyStorageClasses( $1 ) ); }
1769        ;
[b87a5ed]1770
1771type_declarator:                                                                                // CFA
[4d51835]1772        type_declarator_name assertion_list_opt
1773                { $$ = $1->addAssertions( $2 ); }
1774        | type_declarator_name assertion_list_opt '=' type_name
1775                { $$ = $1->addAssertions( $2 )->addType( $4 ); }
1776        ;
[b87a5ed]1777
1778type_declarator_name:                                                                   // CFA
[4d51835]1779        no_attr_identifier_or_typedef_name
1780                {
[de62360d]1781                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
[4d51835]1782                        $$ = DeclarationNode::newTypeDecl( $1, 0 );
1783                }
1784        | no_01_identifier_or_typedef_name '(' push type_parameter_list pop ')'
1785                {
[de62360d]1786                        typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
[4d51835]1787                        $$ = DeclarationNode::newTypeDecl( $1, $4 );
1788                }
1789        ;
[b87a5ed]1790
1791context_specifier:                                                                              // CFA
[4d51835]1792        CONTEXT no_attr_identifier_or_typedef_name '(' push type_parameter_list pop ')' '{' '}'
1793                {
[de62360d]1794                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4d51835]1795                        $$ = DeclarationNode::newContext( $2, $5, 0 );
1796                }
1797        | CONTEXT no_attr_identifier_or_typedef_name '(' push type_parameter_list pop ')' '{'
1798                {
[de62360d]1799                        typedefTable.enterContext( *$2 );
[4d51835]1800                        typedefTable.enterScope();
1801                }
1802          context_declaration_list '}'
1803                {
1804                        typedefTable.leaveContext();
[de62360d]1805                        typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
[4d51835]1806                        $$ = DeclarationNode::newContext( $2, $5, $10 );
1807                }
1808        ;
[b87a5ed]1809
1810context_declaration_list:                                                               // CFA
[4d51835]1811        context_declaration
1812        | context_declaration_list push context_declaration
1813                { $$ = $1->appendList( $3 ); }
1814        ;
[b87a5ed]1815
1816context_declaration:                                                                    // CFA
[4d51835]1817        new_context_declaring_list pop ';'
1818        | context_declaring_list pop ';'
1819        ;
[b87a5ed]1820
1821new_context_declaring_list:                                                             // CFA
[4d51835]1822        new_variable_specifier
1823                {
1824                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1825                        $$ = $1;
1826                }
1827        | new_function_specifier
1828                {
1829                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1830                        $$ = $1;
1831                }
1832        | new_context_declaring_list pop ',' push identifier_or_typedef_name
1833                {
[de62360d]1834                        typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
[4d51835]1835                        $$ = $1->appendList( $1->cloneType( $5 ) );
1836                }
1837        ;
[b87a5ed]1838
1839context_declaring_list:                                                                 // CFA
[4d51835]1840        type_specifier declarator
1841                {
1842                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1843                        $$ = $2->addType( $1 );
1844                }
1845        | context_declaring_list pop ',' push declarator
1846                {
1847                        typedefTable.addToEnclosingScope2( TypedefTable::ID );
1848                        $$ = $1->appendList( $1->cloneBaseType( $5 ) );
1849                }
1850        ;
[51b7345]1851
[c11e31c]1852//***************************** EXTERNAL DEFINITIONS *****************************
[51b7345]1853
1854translation_unit:
[4d51835]1855        // empty
1856                {}                                                                                              // empty input file
1857        | external_definition_list
1858                {
1859                        if ( theTree ) {
1860                                theTree->appendList( $1 );
1861                        } else {
1862                                theTree = $1;
1863                        }
1864                }
1865        ;
[51b7345]1866
1867external_definition_list:
[4d51835]1868        external_definition
1869        | external_definition_list push external_definition
[de62360d]1870                { $$ = ( $1 != NULL ) ? $1->appendList( $3 ) : $3; }
[4d51835]1871        ;
[51b7345]1872
1873external_definition_list_opt:
[4d51835]1874        // empty
1875                { $$ = 0; }
1876        | external_definition_list
1877        ;
[51b7345]1878
1879external_definition:
[4d51835]1880        declaration
1881        | external_function_definition
1882        | asm_statement                                                                         // GCC, global assembler statement
1883                {}
1884        | EXTERN STRINGliteral
1885                {
1886                        linkageStack.push( linkage );
1887                        linkage = LinkageSpec::fromString( *$2 );
1888                }
1889          '{' external_definition_list_opt '}'                          // C++-style linkage specifier
1890                {
1891                        linkage = linkageStack.top();
1892                        linkageStack.pop();
1893                        $$ = $5;
1894                }
1895        | EXTENSION external_definition
1896                { $$ = $2; }
1897        ;
1898
1899external_function_definition:
1900        function_definition
1901
[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
1984        | typedef_redeclarator
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
[4d51835]2023        identifier_or_typedef_name {}
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
2105// further separation of variable and function declarators in typedef_redeclarator.  The pattern precludes returning
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
[c11e31c]2178// This pattern parses a declaration for a variable or function prototype that redefines a typedef name, e.g.:
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
2188typedef_redeclarator:
[4d51835]2189        paren_typedef attribute_list_opt
2190        | typedef_ptr
2191        | typedef_array attribute_list_opt
2192        | typedef_function attribute_list_opt
2193        ;
[51b7345]2194
2195paren_typedef:
[4d51835]2196        TYPEDEFname
2197                {
[de62360d]2198                        typedefTable.setNextIdentifier( *( $1 ) );
[4d51835]2199                        $$ = DeclarationNode::newName( $1 );
2200                }
2201        | '(' paren_typedef ')'
2202                { $$ = $2; }
2203        ;
[51b7345]2204
2205typedef_ptr:
[4d51835]2206        '*' typedef_redeclarator
2207                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2208        | '*' type_qualifier_list typedef_redeclarator
2209                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2210        | '(' typedef_ptr ')'
2211                { $$ = $2; }
2212        ;
[51b7345]2213
2214typedef_array:
[4d51835]2215        paren_typedef array_dimension
2216                { $$ = $1->addArray( $2 ); }
2217        | '(' typedef_ptr ')' array_dimension
2218                { $$ = $2->addArray( $4 ); }
2219        | '(' typedef_array ')' multi_array_dimension           // redundant parenthesis
2220                { $$ = $2->addArray( $4 ); }
2221        | '(' typedef_array ')'                                                         // redundant parenthesis
2222                { $$ = $2; }
2223        ;
[51b7345]2224
2225typedef_function:
[4d51835]2226        paren_typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2227                { $$ = $1->addParamList( $4 ); }
2228        | '(' typedef_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2229                { $$ = $2->addParamList( $6 ); }
2230        | '(' typedef_function ')'                                                      // redundant parenthesis
2231                { $$ = $2; }
2232        ;
[51b7345]2233
[de62360d]2234// This pattern parses a declaration for a parameter variable or function prototype that is not redefining a typedef
2235// name and allows the C99 array options, which can only appear in a parameter list.  The pattern precludes declaring an
2236// array of functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to
2237// arrays and functions.
[51b7345]2238
2239identifier_parameter_declarator:
[4d51835]2240        paren_identifier attribute_list_opt
2241        | identifier_parameter_ptr
2242        | identifier_parameter_array attribute_list_opt
2243        | identifier_parameter_function attribute_list_opt
2244        ;
[51b7345]2245
2246identifier_parameter_ptr:
[4d51835]2247        '*' identifier_parameter_declarator
2248                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2249        | '*' type_qualifier_list identifier_parameter_declarator
2250                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2251        | '(' identifier_parameter_ptr ')'
2252                { $$ = $2; }
2253        ;
[51b7345]2254
2255identifier_parameter_array:
[4d51835]2256        paren_identifier array_parameter_dimension
2257                { $$ = $1->addArray( $2 ); }
2258        | '(' identifier_parameter_ptr ')' array_dimension
2259                { $$ = $2->addArray( $4 ); }
2260        | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2261                { $$ = $2->addArray( $4 ); }
2262        | '(' identifier_parameter_array ')'                            // redundant parenthesis
2263                { $$ = $2; }
2264        ;
[51b7345]2265
2266identifier_parameter_function:
[4d51835]2267        paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2268                { $$ = $1->addParamList( $4 ); }
2269        | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2270                { $$ = $2->addParamList( $6 ); }
2271        | '(' identifier_parameter_function ')'                         // redundant parenthesis
2272                { $$ = $2; }
2273        ;
[b87a5ed]2274
[de62360d]2275// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2276// e.g.:
[c11e31c]2277//
[b87a5ed]2278//              typedef int foo;
2279//              int f( int foo ); // redefine typedef name in new scope
[c11e31c]2280//
[de62360d]2281// and allows the C99 array options, which can only appear in a parameter list.  In addition, the pattern handles the
2282// special meaning of parenthesis around a typedef name:
[c11e31c]2283//
[b87a5ed]2284//              ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2285//              parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2286//              not as redundant parentheses around the identifier."
[c11e31c]2287//
2288// which precludes the following cases:
2289//
[b87a5ed]2290//              typedef float T;
[4d51835]2291//              int f( int ( T [5] ) );                                 // see abstract_parameter_declarator
[b87a5ed]2292//              int g( int ( T ( int ) ) );                             // see abstract_parameter_declarator
2293//              int f( int f1( T a[5] ) );                              // see identifier_parameter_declarator
2294//              int g( int g1( T g2( int p ) ) );               // see identifier_parameter_declarator
[c11e31c]2295//
[de62360d]2296// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2297// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2298// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2299// functions.
[51b7345]2300
2301typedef_parameter_redeclarator:
[4d51835]2302        typedef attribute_list_opt
2303        | typedef_parameter_ptr
2304        | typedef_parameter_array attribute_list_opt
2305        | typedef_parameter_function attribute_list_opt
2306        ;
[51b7345]2307
2308typedef:
[4d51835]2309        TYPEDEFname
2310                {
[de62360d]2311                        typedefTable.setNextIdentifier( *$1 );
[4d51835]2312                        $$ = DeclarationNode::newName( $1 );
2313                }
2314        ;
[51b7345]2315
2316typedef_parameter_ptr:
[4d51835]2317        '*' typedef_parameter_redeclarator
2318                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2319        | '*' type_qualifier_list typedef_parameter_redeclarator
2320                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2321        | '(' typedef_parameter_ptr ')'
2322                { $$ = $2; }
2323        ;
[51b7345]2324
2325typedef_parameter_array:
[4d51835]2326        typedef array_parameter_dimension
2327                { $$ = $1->addArray( $2 ); }
2328        | '(' typedef_parameter_ptr ')' array_parameter_dimension
2329                { $$ = $2->addArray( $4 ); }
2330        ;
[51b7345]2331
2332typedef_parameter_function:
[4d51835]2333        typedef '(' push parameter_type_list_opt pop ')'        // empty parameter list OBSOLESCENT (see 3)
2334                { $$ = $1->addParamList( $4 ); }
2335        | '(' typedef_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2336                { $$ = $2->addParamList( $6 ); }
2337        ;
[b87a5ed]2338
[de62360d]2339// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2340// which the type applies, e.g.:
[c11e31c]2341//
[b87a5ed]2342//              sizeof( int );
2343//              sizeof( int [10] );
[c11e31c]2344//
[de62360d]2345// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2346// and functions versus pointers to arrays and functions.
[51b7345]2347
2348abstract_declarator:
[4d51835]2349        abstract_ptr
2350        | abstract_array attribute_list_opt
2351        | abstract_function attribute_list_opt
2352        ;
[51b7345]2353
2354abstract_ptr:
[4d51835]2355        '*'
2356                { $$ = DeclarationNode::newPointer( 0 ); }
2357        | '*' type_qualifier_list
2358                { $$ = DeclarationNode::newPointer( $2 ); }
2359        | '*' abstract_declarator
2360                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2361        | '*' type_qualifier_list abstract_declarator
2362                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2363        | '(' abstract_ptr ')'
2364                { $$ = $2; }
2365        ;
[51b7345]2366
2367abstract_array:
[4d51835]2368        array_dimension
2369        | '(' abstract_ptr ')' array_dimension
2370                { $$ = $2->addArray( $4 ); }
2371        | '(' abstract_array ')' multi_array_dimension          // redundant parenthesis
2372                { $$ = $2->addArray( $4 ); }
2373        | '(' abstract_array ')'                                                        // redundant parenthesis
2374                { $$ = $2; }
2375        ;
[51b7345]2376
2377abstract_function:
[4d51835]2378        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2379                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2380        | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2381                { $$ = $2->addParamList( $6 ); }
2382        | '(' abstract_function ')'                                                     // redundant parenthesis
2383                { $$ = $2; }
2384        ;
[51b7345]2385
2386array_dimension:
[4d51835]2387                // Only the first dimension can be empty.
2388        '[' push pop ']'
2389                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2390        | '[' push pop ']' multi_array_dimension
2391                { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $5 ); }
2392        | multi_array_dimension
2393        ;
[51b7345]2394
2395multi_array_dimension:
[4d51835]2396        '[' push assignment_expression pop ']'
2397                { $$ = DeclarationNode::newArray( $3, 0, false ); }
2398        | '[' push '*' pop ']'                                                          // C99
2399                { $$ = DeclarationNode::newVarArray( 0 ); }
2400        | multi_array_dimension '[' push assignment_expression pop ']'
2401                { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2402        | multi_array_dimension '[' push '*' pop ']'            // C99
2403                { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2404        ;
[51b7345]2405
[c11e31c]2406// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2407// identifier to which the type applies, e.g.:
2408//
[b87a5ed]2409//              int f( int );                   // abstract variable parameter; no parameter name specified
2410//              int f( int (int) );             // abstract function-prototype parameter; no parameter name specified
[c11e31c]2411//
[de62360d]2412// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2413// and functions versus pointers to arrays and functions.
[51b7345]2414
2415abstract_parameter_declarator:
[4d51835]2416        abstract_parameter_ptr
2417        | abstract_parameter_array attribute_list_opt
2418        | abstract_parameter_function attribute_list_opt
2419        ;
[51b7345]2420
2421abstract_parameter_ptr:
[4d51835]2422        '*'
2423                { $$ = DeclarationNode::newPointer( 0 ); }
2424        | '*' type_qualifier_list
2425                { $$ = DeclarationNode::newPointer( $2 ); }
2426        | '*' abstract_parameter_declarator
2427                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2428        | '*' type_qualifier_list abstract_parameter_declarator
2429                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2430        | '(' abstract_parameter_ptr ')'
2431                { $$ = $2; }
2432        ;
[51b7345]2433
2434abstract_parameter_array:
[4d51835]2435        array_parameter_dimension
2436        | '(' abstract_parameter_ptr ')' array_parameter_dimension
2437                { $$ = $2->addArray( $4 ); }
2438        | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2439                { $$ = $2->addArray( $4 ); }
2440        | '(' abstract_parameter_array ')'                                      // redundant parenthesis
2441                { $$ = $2; }
2442        ;
[51b7345]2443
2444abstract_parameter_function:
[4d51835]2445        '(' push parameter_type_list_opt pop ')'                        // empty parameter list OBSOLESCENT (see 3)
2446                { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2447        | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2448                { $$ = $2->addParamList( $6 ); }
2449        | '(' abstract_parameter_function ')'                           // redundant parenthesis
2450                { $$ = $2; }
2451        ;
[51b7345]2452
2453array_parameter_dimension:
[4d51835]2454                // Only the first dimension can be empty or have qualifiers.
2455        array_parameter_1st_dimension
2456        | array_parameter_1st_dimension multi_array_dimension
2457                { $$ = $1->addArray( $2 ); }
2458        | multi_array_dimension
2459        ;
[51b7345]2460
[c11e31c]2461// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2462//
[de62360d]2463//              ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2464//              a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
[51b7345]2465
2466array_parameter_1st_dimension:
[4d51835]2467        '[' push pop ']'
2468                { $$ = DeclarationNode::newArray( 0, 0, false ); }
2469        // multi_array_dimension handles the '[' '*' ']' case
2470        | '[' push type_qualifier_list '*' pop ']'                      // remaining C99
2471                { $$ = DeclarationNode::newVarArray( $3 ); }
2472        | '[' push type_qualifier_list pop ']'
2473                { $$ = DeclarationNode::newArray( 0, $3, false ); }
2474        // multi_array_dimension handles the '[' assignment_expression ']' case
2475        | '[' push type_qualifier_list assignment_expression pop ']'
2476                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2477        | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2478                { $$ = DeclarationNode::newArray( $5, $4, true ); }
2479        | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2480                { $$ = DeclarationNode::newArray( $5, $3, true ); }
2481        ;
[b87a5ed]2482
[de62360d]2483// This pattern parses a declaration of an abstract variable, i.e., there is no identifier to which the type applies,
2484// e.g.:
[c11e31c]2485//
[b87a5ed]2486//              sizeof( int ); // abstract variable; no identifier name specified
[c11e31c]2487//
[de62360d]2488// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2489// and functions versus pointers to arrays and functions.
[51b7345]2490
2491variable_abstract_declarator:
[4d51835]2492        variable_abstract_ptr
2493        | variable_abstract_array attribute_list_opt
2494        | variable_abstract_function attribute_list_opt
2495        ;
[51b7345]2496
2497variable_abstract_ptr:
[4d51835]2498        '*'
2499                { $$ = DeclarationNode::newPointer( 0 ); }
2500        | '*' type_qualifier_list
2501                { $$ = DeclarationNode::newPointer( $2 ); }
2502        | '*' variable_abstract_declarator
2503                { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2504        | '*' type_qualifier_list variable_abstract_declarator
2505                { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2506        | '(' variable_abstract_ptr ')'
2507                { $$ = $2; }
2508        ;
[51b7345]2509
2510variable_abstract_array:
[4d51835]2511        array_dimension
2512        | '(' variable_abstract_ptr ')' array_dimension
2513                { $$ = $2->addArray( $4 ); }
2514        | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
2515                { $$ = $2->addArray( $4 ); }
2516        | '(' variable_abstract_array ')'                                       // redundant parenthesis
2517                { $$ = $2; }
2518        ;
[51b7345]2519
2520variable_abstract_function:
[4d51835]2521        '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2522                { $$ = $2->addParamList( $6 ); }
2523        | '(' variable_abstract_function ')'                            // redundant parenthesis
2524                { $$ = $2; }
2525        ;
[b87a5ed]2526
[de62360d]2527// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
2528// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
[b87a5ed]2529
2530new_identifier_parameter_declarator_tuple:                              // CFA
[4d51835]2531        new_identifier_parameter_declarator_no_tuple
2532        | new_abstract_tuple
2533        | type_qualifier_list new_abstract_tuple
2534                { $$ = $2->addQualifiers( $1 ); }
2535        ;
[b87a5ed]2536
2537new_identifier_parameter_declarator_no_tuple:                   // CFA
[4d51835]2538        new_identifier_parameter_ptr
2539        | new_identifier_parameter_array
2540        ;
[b87a5ed]2541
2542new_identifier_parameter_ptr:                                                   // CFA
[4d51835]2543        '*' type_specifier
2544                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2545        | type_qualifier_list '*' type_specifier
2546                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2547        | '*' new_abstract_function
2548                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2549        | type_qualifier_list '*' new_abstract_function
2550                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2551        | '*' new_identifier_parameter_declarator_tuple
2552                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2553        | type_qualifier_list '*' new_identifier_parameter_declarator_tuple
2554                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2555        ;
[b87a5ed]2556
2557new_identifier_parameter_array:                                                 // CFA
[de62360d]2558                // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
2559                // shift/reduce conflict with new-style empty (void) function return type.
[4d51835]2560        '[' push pop ']' type_specifier
2561                { $$ = $5->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2562        | new_array_parameter_1st_dimension type_specifier
2563                { $$ = $2->addNewArray( $1 ); }
2564        | '[' push pop ']' multi_array_dimension type_specifier
2565                { $$ = $6->addNewArray( $5 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2566        | new_array_parameter_1st_dimension multi_array_dimension type_specifier
2567                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2568        | multi_array_dimension type_specifier
2569                { $$ = $2->addNewArray( $1 ); }
2570        | '[' push pop ']' new_identifier_parameter_ptr
2571                { $$ = $5->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2572        | new_array_parameter_1st_dimension new_identifier_parameter_ptr
2573                { $$ = $2->addNewArray( $1 ); }
2574        | '[' push pop ']' multi_array_dimension new_identifier_parameter_ptr
2575                { $$ = $6->addNewArray( $5 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2576        | new_array_parameter_1st_dimension multi_array_dimension new_identifier_parameter_ptr
2577                { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2578        | multi_array_dimension new_identifier_parameter_ptr
2579                { $$ = $2->addNewArray( $1 ); }
2580        ;
[51b7345]2581
2582new_array_parameter_1st_dimension:
[4d51835]2583        '[' push type_qualifier_list '*' pop ']'                        // remaining C99
2584                { $$ = DeclarationNode::newVarArray( $3 ); }
2585        | '[' push type_qualifier_list assignment_expression pop ']'
2586                { $$ = DeclarationNode::newArray( $4, $3, false ); }
2587        | '[' push declaration_qualifier_list assignment_expression pop ']'
2588                // declaration_qualifier_list must be used because of shift/reduce conflict with
2589                // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
2590                // appear in this context.
2591                { $$ = DeclarationNode::newArray( $4, $3, true ); }
2592        | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
2593                { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
2594        ;
[b87a5ed]2595
[de62360d]2596// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
2597// identifier to which the type applies, e.g.:
[c11e31c]2598//
[b87a5ed]2599//              [int] f( int );                         // abstract variable parameter; no parameter name specified
2600//              [int] f( [int] (int) );         // abstract function-prototype parameter; no parameter name specified
[c11e31c]2601//
2602// These rules need LR(3):
2603//
[b87a5ed]2604//              new_abstract_tuple identifier_or_typedef_name
2605//              '[' new_parameter_list ']' identifier_or_typedef_name '(' new_parameter_type_list_opt ')'
[c11e31c]2606//
2607// since a function return type can be syntactically identical to a tuple type:
2608//
[b87a5ed]2609//              [int, int] t;
2610//              [int, int] f( int );
[c11e31c]2611//
2612// Therefore, it is necessary to look at the token after identifier_or_typedef_name to know when to reduce
[de62360d]2613// new_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
2614// lookahead. To accomplish this, new_abstract_declarator has an entry point without tuple, and tuple declarations are
2615// duplicated when appearing with new_function_specifier.
[b87a5ed]2616
2617new_abstract_declarator_tuple:                                                  // CFA
[4d51835]2618        new_abstract_tuple
2619        | type_qualifier_list new_abstract_tuple
2620                { $$ = $2->addQualifiers( $1 ); }
2621        | new_abstract_declarator_no_tuple
2622        ;
[b87a5ed]2623
2624new_abstract_declarator_no_tuple:                                               // CFA
[4d51835]2625        new_abstract_ptr
2626        | new_abstract_array
2627        ;
[b87a5ed]2628
2629new_abstract_ptr:                                                                               // CFA
[4d51835]2630        '*' type_specifier
2631                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2632        | type_qualifier_list '*' type_specifier
2633                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2634        | '*' new_abstract_function
2635                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2636        | type_qualifier_list '*' new_abstract_function
2637                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2638        | '*' new_abstract_declarator_tuple
2639                { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2640        | type_qualifier_list '*' new_abstract_declarator_tuple
2641                { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2642        ;
[b87a5ed]2643
2644new_abstract_array:                                                                             // CFA
[de62360d]2645                // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
2646                // empty (void) function return type.
[4d51835]2647        '[' push pop ']' type_specifier
2648                { $$ = $5->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2649        | '[' push pop ']' multi_array_dimension type_specifier
2650                { $$ = $6->addNewArray( $5 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2651        | multi_array_dimension type_specifier
2652                { $$ = $2->addNewArray( $1 ); }
2653        | '[' push pop ']' new_abstract_ptr
2654                { $$ = $5->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2655        | '[' push pop ']' multi_array_dimension new_abstract_ptr
2656                { $$ = $6->addNewArray( $5 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2657        | multi_array_dimension new_abstract_ptr
2658                { $$ = $2->addNewArray( $1 ); }
2659        ;
[b87a5ed]2660
2661new_abstract_tuple:                                                                             // CFA
[4d51835]2662        '[' push new_abstract_parameter_list pop ']'
2663                { $$ = DeclarationNode::newTuple( $3 ); }
2664        ;
[b87a5ed]2665
2666new_abstract_function:                                                                  // CFA
[4d51835]2667        '[' push pop ']' '(' new_parameter_type_list_opt ')'
2668                { $$ = DeclarationNode::newFunction( 0, DeclarationNode::newTuple( 0 ), $6, 0 ); }
2669        | new_abstract_tuple '(' push new_parameter_type_list_opt pop ')'
2670                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2671        | new_function_return '(' push new_parameter_type_list_opt pop ')'
2672                { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2673        ;
[b87a5ed]2674
[de62360d]2675// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
2676//    each declaration, and in the specifier-qualifier list in each structure declaration and type name."
[c11e31c]2677//
[de62360d]2678// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
2679//    the declaration specifiers in a declaration is an obsolescent feature."
[c11e31c]2680//
2681// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
2682//    prototype-format parameter type declarators) is an obsolescent feature."
2683//
[de62360d]2684// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
2685//    declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
[51b7345]2686
[c11e31c]2687//************************* MISCELLANEOUS ********************************
[51b7345]2688
[b87a5ed]2689comma_opt:                                                                                              // redundant comma
[4d51835]2690        // empty
2691        | ','
2692        ;
[51b7345]2693
2694assignment_opt:
[4d51835]2695        // empty
2696                { $$ = 0; }
2697        | '=' assignment_expression
2698                { $$ = $2; }
2699        ;
[51b7345]2700
2701%%
[c11e31c]2702// ----end of grammar----
[51b7345]2703
[5f2f2d7]2704void yyerror( const char * ) {
2705        std::cout << "Error ";
[b87a5ed]2706        if ( yyfilename ) {
[5f2f2d7]2707            std::cout << "in file " << yyfilename << " ";
2708        } // if
[b1d6dd5]2709        std::cout << "at line " << yylineno << " reading token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << "\"" << std::endl;
[51b7345]2710}
2711
[c11e31c]2712// Local Variables: //
[b87a5ed]2713// mode: c++ //
[de62360d]2714// tab-width: 4 //
[c11e31c]2715// compile-command: "make install" //
2716// End: //
Note: See TracBrowser for help on using the repository browser.