source: src/Parser/parser.yy@ e0ff3e6

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

fix computed goto, fixed -std=, implicit typedefs for enum and aggregates, add _Noreturn _Thread_local

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