source: src/Parser/parser.yy@ 7bf7fb9

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

more refactoring of parser code

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