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

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

more refactoring of parser code

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