source: src/Parser/parser.yy@ 12bc63a

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 12bc63a was 12bc63a, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'master' into tuples

Conflicts:

src/Parser/parser.cc

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