source: src/Parser/parser.yy@ 35f9114

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 35f9114 was d9e2280, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

even more more refactoring of parser code

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