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

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

even more more refactoring of parser code

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