source: src/Parser/parser.yy@ 848ce71

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 848ce71 was 7756647, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'master' into tuples

Conflicts:

src/Parser/TypeData.cc
src/Parser/parser.cc
src/Parser/parser.yy

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