source: src/Parser/parser.yy@ 5f5083e

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 5f5083e was 8780e30, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

build appropriate nodes when parsing member tuple expressions

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