source: src/Parser/parser.yy@ 77971f6

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 stuck-waitfor-destruct with_gc
Last change on this file since 77971f6 was a1e67dd, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into tuples

Conflicts:

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

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