source: src/Parser/parser.yy@ cff1143

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 cff1143 was 2871210, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

include file with keywords, fix type of label address expressions, fix computed goto to any expressions, generic types first attempt

  • Property mode set to 100644
File size: 98.2 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// cfa.y --
8//
9// Author : Peter A. Buhr
10// Created On : Sat Sep 1 20:22:55 2001
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 3 13:53:45 2015
13// Update Count : 1220
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_type_name no_attr_identifier_or_type_name no_01_identifier_or_type_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_type
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 type_array typedef_declaration typedef_declaration_specifier typedef_expression
216%type<decl> type_function type_parameter_array type_parameter_function type_parameter_ptr
217%type<decl> type_parameter_redeclarator type_ptr type_redeclarator typedef_type_specifier
218%type<decl> typegen_declaration_specifier typegen_type_specifier typegen_name
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_type_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 '[' ']'
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_type_name {}
927 | label_list ',' no_attr_identifier_or_type_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_type_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_type_name asm_name_opt
978 {
979 typedefTable.setNextIdentifier( *$2 );
980 $$ = $1->addName( $2 );
981 }
982 | new_abstract_tuple identifier_or_type_name asm_name_opt
983 {
984 typedefTable.setNextIdentifier( *$2 );
985 $$ = $1->addName( $2 );
986 }
987 | type_qualifier_list new_abstract_tuple identifier_or_type_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_type_name
1016 {
1017 typedefTable.addToEnclosingScope( *$5, TypedefTable::ID );
1018 $$ = $1->appendList( $1->cloneType( $5 ) );
1019 }
1020 ;
1021
1022new_function_specifier: // CFA
1023 '[' ']' identifier_or_type_name '(' push new_parameter_type_list_opt pop ')' // S/R conflict
1024 {
1025 $$ = DeclarationNode::newFunction( $3, DeclarationNode::newTuple( 0 ), $6, 0, true );
1026 }
1027// '[' ']' identifier '(' push new_parameter_type_list_opt pop ')'
1028// {
1029// typedefTable.setNextIdentifier( *$5 );
1030// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1031// }
1032// | '[' ']' TYPEDEFname '(' push new_parameter_type_list_opt pop ')'
1033// {
1034// typedefTable.setNextIdentifier( *$5 );
1035// $$ = DeclarationNode::newFunction( $5, DeclarationNode::newTuple( 0 ), $8, 0, true );
1036// }
1037// | '[' ']' typegen_name
1038 // identifier_or_type_name must be broken apart because of the sequence:
1039 //
1040 // '[' ']' identifier_or_type_name '(' new_parameter_type_list_opt ')'
1041 // '[' ']' type_specifier
1042 //
1043 // type_specifier can resolve to just TYPEDEFname (e.g., typedef int T; int f( T );). Therefore this must be
1044 // flattened to allow lookahead to the '(' without having to reduce identifier_or_type_name.
1045 | new_abstract_tuple identifier_or_type_name '(' push new_parameter_type_list_opt pop ')'
1046 // To obtain LR(1 ), this rule must be factored out from function return type (see new_abstract_declarator).
1047 {
1048 $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1049 }
1050 | new_function_return identifier_or_type_name '(' push new_parameter_type_list_opt pop ')'
1051 {
1052 $$ = DeclarationNode::newFunction( $2, $1, $5, 0, true );
1053 }
1054 ;
1055
1056new_function_return: // CFA
1057 '[' push new_parameter_list pop ']'
1058 { $$ = DeclarationNode::newTuple( $3 ); }
1059 | '[' push new_parameter_list pop ',' push new_abstract_parameter_list pop ']'
1060 // To obtain LR(1 ), the last new_abstract_parameter_list is added into this flattened rule to lookahead to the
1061 // ']'.
1062 { $$ = DeclarationNode::newTuple( $3->appendList( $7 ) ); }
1063 ;
1064
1065new_typedef_declaration: // CFA
1066 TYPEDEF new_variable_specifier
1067 {
1068 typedefTable.addToEnclosingScope( TypedefTable::TD );
1069 $$ = $2->addTypedef();
1070 }
1071 | TYPEDEF new_function_specifier
1072 {
1073 typedefTable.addToEnclosingScope( TypedefTable::TD );
1074 $$ = $2->addTypedef();
1075 }
1076 | new_typedef_declaration pop ',' push no_attr_identifier
1077 {
1078 typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
1079 $$ = $1->appendList( $1->cloneType( $5 ) );
1080 }
1081 ;
1082
1083// Traditionally typedef is part of storage-class specifier for syntactic convenience only. Here, it is factored out as
1084// a separate form of declaration, which syntactically precludes storage-class specifiers and initialization.
1085
1086typedef_declaration:
1087 TYPEDEF type_specifier declarator
1088 {
1089 typedefTable.addToEnclosingScope( TypedefTable::TD );
1090 $$ = $3->addType( $2 )->addTypedef();
1091 }
1092 | typedef_declaration pop ',' push declarator
1093 {
1094 typedefTable.addToEnclosingScope( TypedefTable::TD );
1095 $$ = $1->appendList( $1->cloneBaseType( $5 )->addTypedef() );
1096 }
1097 | type_qualifier_list TYPEDEF type_specifier declarator // remaining OBSOLESCENT (see 2 )
1098 {
1099 typedefTable.addToEnclosingScope( TypedefTable::TD );
1100 $$ = $4->addType( $3 )->addQualifiers( $1 )->addTypedef();
1101 }
1102 | type_specifier TYPEDEF declarator
1103 {
1104 typedefTable.addToEnclosingScope( TypedefTable::TD );
1105 $$ = $3->addType( $1 )->addTypedef();
1106 }
1107 | type_specifier TYPEDEF type_qualifier_list declarator
1108 {
1109 typedefTable.addToEnclosingScope( TypedefTable::TD );
1110 $$ = $4->addQualifiers( $1 )->addTypedef()->addType( $1 );
1111 }
1112 ;
1113
1114typedef_expression:
1115 // GCC, naming expression type: typedef name = exp; gives a name to the type of an expression
1116 TYPEDEF no_attr_identifier '=' assignment_expression
1117 {
1118 typedefTable.addToEnclosingScope( *$2, TypedefTable::TD );
1119 $$ = DeclarationNode::newName( 0 ); // XXX
1120 }
1121 | typedef_expression pop ',' push no_attr_identifier '=' assignment_expression
1122 {
1123 typedefTable.addToEnclosingScope( *$5, TypedefTable::TD );
1124 $$ = DeclarationNode::newName( 0 ); // XXX
1125 }
1126 ;
1127
1128old_declaration:
1129 declaring_list pop ';'
1130 | typedef_declaration pop ';'
1131 | typedef_expression pop ';' // GCC, naming expression type
1132 | sue_declaration_specifier pop ';'
1133 ;
1134
1135declaring_list:
1136 // A semantic check is required to ensure asm_name only appears on declarations with implicit or explicit static
1137 // storage-class
1138 declaration_specifier declarator asm_name_opt initializer_opt
1139 {
1140 typedefTable.addToEnclosingScope( TypedefTable::ID );
1141 $$ = ( $2->addType( $1 ))->addInitializer( $4 );
1142 }
1143 | declaring_list ',' attribute_list_opt declarator asm_name_opt initializer_opt
1144 {
1145 typedefTable.addToEnclosingScope( TypedefTable::ID );
1146 $$ = $1->appendList( $1->cloneBaseType( $4->addInitializer( $6 ) ) );
1147 }
1148 ;
1149
1150declaration_specifier: // type specifier + storage class
1151 basic_declaration_specifier
1152 | sue_declaration_specifier
1153 | typedef_declaration_specifier
1154 | typegen_declaration_specifier
1155 ;
1156
1157type_specifier: // declaration specifier - storage class
1158 basic_type_specifier
1159 | sue_type_specifier
1160 | typedef_type_specifier
1161 | typegen_type_specifier
1162 ;
1163
1164type_qualifier_list_opt: // GCC, used in asm_statement
1165 // empty
1166 { $$ = 0; }
1167 | type_qualifier_list
1168 ;
1169
1170type_qualifier_list:
1171 // A semantic check is necessary to ensure a type qualifier is appropriate for the kind of declaration.
1172 //
1173 // ISO/IEC 9899:1999 Section 6.7.3(4 ) : If the same qualifier appears more than once in the same
1174 // specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it
1175 // appeared only once.
1176 type_qualifier
1177 | type_qualifier_list type_qualifier
1178 { $$ = $1->addQualifiers( $2 ); }
1179 ;
1180
1181type_qualifier:
1182 type_qualifier_name
1183 | attribute
1184 { $$ = DeclarationNode::newQualifier( DeclarationNode::Attribute ); }
1185 ;
1186
1187type_qualifier_name:
1188 CONST
1189 { $$ = DeclarationNode::newQualifier( DeclarationNode::Const ); }
1190 | RESTRICT
1191 { $$ = DeclarationNode::newQualifier( DeclarationNode::Restrict ); }
1192 | VOLATILE
1193 { $$ = DeclarationNode::newQualifier( DeclarationNode::Volatile ); }
1194 | LVALUE // CFA
1195 { $$ = DeclarationNode::newQualifier( DeclarationNode::Lvalue ); }
1196 | ATOMIC
1197 { $$ = DeclarationNode::newQualifier( DeclarationNode::Atomic ); }
1198 | FORALL '('
1199 {
1200 typedefTable.enterScope();
1201 }
1202 type_parameter_list ')' // CFA
1203 {
1204 typedefTable.leaveScope();
1205 $$ = DeclarationNode::newForall( $4 );
1206 }
1207 ;
1208
1209declaration_qualifier_list:
1210 storage_class_list
1211 | type_qualifier_list storage_class_list // remaining OBSOLESCENT (see 2 )
1212 { $$ = $1->addQualifiers( $2 ); }
1213 | declaration_qualifier_list type_qualifier_list storage_class_list
1214 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1215 ;
1216
1217storage_class_list:
1218 // A semantic check is necessary to ensure a storage class is appropriate for the kind of declaration and that
1219 // only one of each is specified, except for inline, which can appear with the others.
1220 //
1221 // ISO/IEC 9899:1999 Section 6.7.1(2) : At most, one storage-class specifier may be given in the declaration
1222 // specifiers in a declaration.
1223 storage_class
1224 | storage_class_list storage_class
1225 { $$ = $1->addQualifiers( $2 ); }
1226 ;
1227
1228storage_class:
1229 storage_class_name
1230 ;
1231
1232storage_class_name:
1233 EXTERN
1234 { $$ = DeclarationNode::newStorageClass( DeclarationNode::Extern ); }
1235 | STATIC
1236 { $$ = DeclarationNode::newStorageClass( DeclarationNode::Static ); }
1237 | AUTO
1238 { $$ = DeclarationNode::newStorageClass( DeclarationNode::Auto ); }
1239 | REGISTER
1240 { $$ = DeclarationNode::newStorageClass( DeclarationNode::Register ); }
1241 | INLINE // C99
1242 { $$ = DeclarationNode::newStorageClass( DeclarationNode::Inline ); }
1243 | FORTRAN // C99
1244 { $$ = DeclarationNode::newStorageClass( DeclarationNode::Fortran ); }
1245 | NORETURN // C11
1246 { $$ = DeclarationNode::newStorageClass( DeclarationNode::Noreturn ); }
1247 | THREADLOCAL // C11
1248 { $$ = DeclarationNode::newStorageClass( DeclarationNode::Threadlocal ); }
1249 ;
1250
1251basic_type_name:
1252 CHAR
1253 { $$ = DeclarationNode::newBasicType( DeclarationNode::Char ); }
1254 | DOUBLE
1255 { $$ = DeclarationNode::newBasicType( DeclarationNode::Double ); }
1256 | FLOAT
1257 { $$ = DeclarationNode::newBasicType( DeclarationNode::Float ); }
1258 | INT
1259 { $$ = DeclarationNode::newBasicType( DeclarationNode::Int ); }
1260 | LONG
1261 { $$ = DeclarationNode::newModifier( DeclarationNode::Long ); }
1262 | SHORT
1263 { $$ = DeclarationNode::newModifier( DeclarationNode::Short ); }
1264 | SIGNED
1265 { $$ = DeclarationNode::newModifier( DeclarationNode::Signed ); }
1266 | UNSIGNED
1267 { $$ = DeclarationNode::newModifier( DeclarationNode::Unsigned ); }
1268 | VOID
1269 { $$ = DeclarationNode::newBasicType( DeclarationNode::Void ); }
1270 | BOOL // C99
1271 { $$ = DeclarationNode::newBasicType( DeclarationNode::Bool ); }
1272 | COMPLEX // C99
1273 { $$ = DeclarationNode::newBasicType( DeclarationNode::Complex ); }
1274 | IMAGINARY // C99
1275 { $$ = DeclarationNode::newBasicType( DeclarationNode::Imaginary ); }
1276 ;
1277
1278basic_declaration_specifier:
1279 // A semantic check is necessary for conflicting storage classes.
1280 basic_type_specifier
1281 | declaration_qualifier_list basic_type_specifier
1282 { $$ = $2->addQualifiers( $1 ); }
1283 | basic_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1284 { $$ = $1->addQualifiers( $2 ); }
1285 | basic_declaration_specifier storage_class type_qualifier_list
1286 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1287 | basic_declaration_specifier storage_class basic_type_specifier
1288 { $$ = $3->addQualifiers( $2 )->addType( $1 ); }
1289 ;
1290
1291basic_type_specifier:
1292 direct_type_name
1293 | type_qualifier_list_opt indirect_type_name type_qualifier_list_opt
1294 { $$ = $2->addQualifiers( $1 )->addQualifiers( $3 ); }
1295 ;
1296
1297direct_type_name:
1298 // A semantic check is necessary for conflicting type qualifiers.
1299 basic_type_name
1300 | type_qualifier_list basic_type_name
1301 { $$ = $2->addQualifiers( $1 ); }
1302 | direct_type_name type_qualifier
1303 { $$ = $1->addQualifiers( $2 ); }
1304 | direct_type_name basic_type_name
1305 { $$ = $1->addType( $2 ); }
1306 ;
1307
1308indirect_type_name:
1309 TYPEOF '(' type_name ')' // GCC: typeof(x) y;
1310 { $$ = $3; }
1311 | TYPEOF '(' comma_expression ')' // GCC: typeof(a+b) y;
1312 { $$ = DeclarationNode::newTypeof( $3 ); }
1313 | ATTR_TYPEGENname '(' type_name ')' // CFA: e.g., @type(x) y;
1314 { $$ = DeclarationNode::newAttr( $1, $3 ); }
1315 | ATTR_TYPEGENname '(' comma_expression ')' // CFA: e.g., @type(a+b) y;
1316 { $$ = DeclarationNode::newAttr( $1, $3 ); }
1317 ;
1318
1319sue_declaration_specifier:
1320 sue_type_specifier
1321 | declaration_qualifier_list sue_type_specifier
1322 { $$ = $2->addQualifiers( $1 ); }
1323 | sue_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1324 { $$ = $1->addQualifiers( $2 ); }
1325 | sue_declaration_specifier storage_class type_qualifier_list
1326 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1327 ;
1328
1329sue_type_specifier:
1330 elaborated_type_name // struct, union, enum
1331 | type_qualifier_list elaborated_type_name
1332 { $$ = $2->addQualifiers( $1 ); }
1333 | sue_type_specifier type_qualifier
1334 { $$ = $1->addQualifiers( $2 ); }
1335 ;
1336
1337typedef_declaration_specifier:
1338 typedef_type_specifier
1339 | declaration_qualifier_list typedef_type_specifier
1340 { $$ = $2->addQualifiers( $1 ); }
1341 | typedef_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1342 { $$ = $1->addQualifiers( $2 ); }
1343 | typedef_declaration_specifier storage_class type_qualifier_list
1344 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1345 ;
1346
1347typedef_type_specifier: // typedef types
1348 TYPEDEFname
1349 { $$ = DeclarationNode::newFromTypedef( $1 ); }
1350 | type_qualifier_list TYPEDEFname
1351 { $$ = DeclarationNode::newFromTypedef( $2 )->addQualifiers( $1 ); }
1352 | typedef_type_specifier type_qualifier
1353 { $$ = $1->addQualifiers( $2 ); }
1354 ;
1355
1356elaborated_type_name:
1357 aggregate_name
1358 | enum_name
1359 ;
1360
1361aggregate_name:
1362 aggregate_key '{' field_declaration_list '}'
1363 { $$ = DeclarationNode::newAggregate( $1, 0, 0, $3 ); }
1364 | aggregate_key no_attr_identifier_or_type_name
1365 { $$ = DeclarationNode::newAggregate( $1, $2, 0, 0 ); }
1366 | aggregate_key no_attr_identifier_or_type_name '{' field_declaration_list '}'
1367 { $$ = DeclarationNode::newAggregate( $1, $2, 0, $4 ); }
1368 | aggregate_key '(' type_name_list ')' '{' field_declaration_list '}' // CFA
1369 { $$ = DeclarationNode::newAggregate( $1, 0, $3, $6 ); }
1370 | aggregate_key typegen_name // CFA, S/R conflict
1371 { $$ = $2; }
1372 ;
1373
1374aggregate_key:
1375 STRUCT attribute_list_opt
1376 { $$ = DeclarationNode::Struct; }
1377 | UNION attribute_list_opt
1378 { $$ = DeclarationNode::Union; }
1379 ;
1380
1381field_declaration_list:
1382 field_declaration
1383 { $$ = $1; }
1384 | field_declaration_list field_declaration
1385 { $$ = $1->appendList( $2 ); }
1386 ;
1387
1388field_declaration:
1389 new_field_declaring_list ';' // CFA, new style field declaration
1390 | EXTENSION new_field_declaring_list ';' // GCC
1391 { $$ = $2; }
1392 | field_declaring_list ';'
1393 | EXTENSION field_declaring_list ';' // GCC
1394 { $$ = $2; }
1395 ;
1396
1397new_field_declaring_list: // CFA, new style field declaration
1398 new_abstract_declarator_tuple // CFA, no field name
1399 | new_abstract_declarator_tuple no_attr_identifier_or_type_name
1400 { $$ = $1->addName( $2 ); }
1401 | new_field_declaring_list ',' no_attr_identifier_or_type_name
1402 { $$ = $1->appendList( $1->cloneType( $3 ) ); }
1403 | new_field_declaring_list ',' // CFA, no field name
1404 { $$ = $1->appendList( $1->cloneType( 0 ) ); }
1405 ;
1406
1407field_declaring_list:
1408 type_specifier field_declarator
1409 { $$ = $2->addType( $1 ); }
1410 | field_declaring_list ',' attribute_list_opt field_declarator
1411 { $$ = $1->appendList( $1->cloneBaseType( $4 ) ); }
1412 ;
1413
1414field_declarator:
1415 // empty
1416 { $$ = DeclarationNode::newName( 0 ); /* XXX */ } // CFA, no field name
1417 | bit_subrange_size // no field name
1418 { $$ = DeclarationNode::newBitfield( $1 ); }
1419 | variable_declarator bit_subrange_size_opt
1420 // A semantic check is required to ensure bit_subrange only appears on base type int.
1421 { $$ = $1->addBitfield( $2 ); }
1422 | type_redeclarator bit_subrange_size_opt
1423 // A semantic check is required to ensure bit_subrange only appears on base type int.
1424 { $$ = $1->addBitfield( $2 ); }
1425 | variable_abstract_declarator // CFA, no field name
1426 ;
1427
1428bit_subrange_size_opt:
1429 // empty
1430 { $$ = 0; }
1431 | bit_subrange_size
1432 { $$ = $1; }
1433 ;
1434
1435bit_subrange_size:
1436 ':' constant_expression
1437 { $$ = $2; }
1438 ;
1439
1440enum_key:
1441 ENUM attribute_list_opt
1442 ;
1443
1444enum_name:
1445 enum_key '{' enumerator_list comma_opt '}'
1446 { $$ = DeclarationNode::newEnum( 0, $3 ); }
1447 | enum_key no_attr_identifier_or_type_name '{' enumerator_list comma_opt '}'
1448 { $$ = DeclarationNode::newEnum( $2, $4 ); }
1449 | enum_key no_attr_identifier_or_type_name
1450 { $$ = DeclarationNode::newEnum( $2, 0 ); }
1451 ;
1452
1453enumerator_list:
1454 no_attr_identifier_or_type_name enumerator_value_opt
1455 { $$ = DeclarationNode::newEnumConstant( $1, $2 ); }
1456 | enumerator_list ',' no_attr_identifier_or_type_name enumerator_value_opt
1457 { $$ = $1->appendList( DeclarationNode::newEnumConstant( $3, $4 ) ); }
1458 ;
1459
1460enumerator_value_opt:
1461 // empty
1462 { $$ = 0; }
1463 | '=' constant_expression
1464 { $$ = $2; }
1465 ;
1466
1467// Minimum of one parameter after which ellipsis is allowed only at the end.
1468
1469new_parameter_type_list_opt: // CFA
1470 // empty
1471 { $$ = 0; }
1472 | new_parameter_type_list
1473 ;
1474
1475new_parameter_type_list: // CFA, abstract + real
1476 new_abstract_parameter_list
1477 | new_parameter_list
1478 | new_parameter_list pop ',' push new_abstract_parameter_list
1479 { $$ = $1->appendList( $5 ); }
1480 | new_abstract_parameter_list pop ',' push ELLIPSIS
1481 { $$ = $1->addVarArgs(); }
1482 | new_parameter_list pop ',' push ELLIPSIS
1483 { $$ = $1->addVarArgs(); }
1484 ;
1485
1486new_parameter_list: // CFA
1487 // To obtain LR(1) between new_parameter_list and new_abstract_tuple, the last new_abstract_parameter_list is
1488 // factored out from new_parameter_list, flattening the rules to get lookahead to the ']'.
1489 new_parameter_declaration
1490 | new_abstract_parameter_list pop ',' push new_parameter_declaration
1491 { $$ = $1->appendList( $5 ); }
1492 | new_parameter_list pop ',' push new_parameter_declaration
1493 { $$ = $1->appendList( $5 ); }
1494 | new_parameter_list pop ',' push new_abstract_parameter_list pop ',' push new_parameter_declaration
1495 { $$ = $1->appendList( $5 )->appendList( $9 ); }
1496 ;
1497
1498new_abstract_parameter_list: // CFA, new & old style abstract
1499 new_abstract_parameter_declaration
1500 | new_abstract_parameter_list pop ',' push new_abstract_parameter_declaration
1501 { $$ = $1->appendList( $5 ); }
1502 ;
1503
1504parameter_type_list_opt:
1505 // empty
1506 { $$ = 0; }
1507 | parameter_type_list
1508 ;
1509
1510parameter_type_list:
1511 parameter_list
1512 | parameter_list pop ',' push ELLIPSIS
1513 { $$ = $1->addVarArgs(); }
1514 ;
1515
1516parameter_list: // abstract + real
1517 abstract_parameter_declaration
1518 | parameter_declaration
1519 | parameter_list pop ',' push abstract_parameter_declaration
1520 { $$ = $1->appendList( $5 ); }
1521 | parameter_list pop ',' push parameter_declaration
1522 { $$ = $1->appendList( $5 ); }
1523 ;
1524
1525// Provides optional identifier names (abstract_declarator/variable_declarator), no initialization, different semantics
1526// for typedef name by using type_parameter_redeclarator instead of typedef_redeclarator, and function prototypes.
1527
1528new_parameter_declaration: // CFA, new & old style parameter declaration
1529 parameter_declaration
1530 | new_identifier_parameter_declarator_no_tuple identifier_or_type_name assignment_opt
1531 { $$ = $1->addName( $2 ); }
1532 | new_abstract_tuple identifier_or_type_name assignment_opt
1533 // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1534 { $$ = $1->addName( $2 ); }
1535 | type_qualifier_list new_abstract_tuple identifier_or_type_name assignment_opt
1536 { $$ = $2->addName( $3 )->addQualifiers( $1 ); }
1537 | new_function_specifier
1538 ;
1539
1540new_abstract_parameter_declaration: // CFA, new & old style parameter declaration
1541 abstract_parameter_declaration
1542 | new_identifier_parameter_declarator_no_tuple
1543 | new_abstract_tuple
1544 // To obtain LR(1), these rules must be duplicated here (see new_abstract_declarator).
1545 | type_qualifier_list new_abstract_tuple
1546 { $$ = $2->addQualifiers( $1 ); }
1547 | new_abstract_function
1548 ;
1549
1550parameter_declaration:
1551 declaration_specifier identifier_parameter_declarator assignment_opt
1552 {
1553 typedefTable.addToEnclosingScope( TypedefTable::ID );
1554 $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
1555 }
1556 | declaration_specifier type_parameter_redeclarator assignment_opt
1557 {
1558 typedefTable.addToEnclosingScope( TypedefTable::ID );
1559 $$ = $2->addType( $1 )->addInitializer( new InitializerNode( $3 ) );
1560 }
1561 ;
1562
1563abstract_parameter_declaration:
1564 declaration_specifier
1565 | declaration_specifier abstract_parameter_declarator
1566 { $$ = $2->addType( $1 ); }
1567 ;
1568
1569// ISO/IEC 9899:1999 Section 6.9.1(6) : "An identifier declared as a typedef name shall not be redeclared as a
1570// parameter." Because the scope of the K&R-style parameter-list sees the typedef first, the following is based only on
1571// identifiers. The ANSI-style parameter-list can redefine a typedef name.
1572
1573identifier_list: // K&R-style parameter list => no types
1574 no_attr_identifier
1575 { $$ = DeclarationNode::newName( $1 ); }
1576 | identifier_list ',' no_attr_identifier
1577 { $$ = $1->appendList( DeclarationNode::newName( $3 ) ); }
1578 ;
1579
1580identifier_or_type_name:
1581 identifier
1582 | TYPEDEFname
1583 | TYPEGENname
1584 ;
1585
1586no_01_identifier_or_type_name:
1587 no_01_identifier
1588 | TYPEDEFname
1589 | TYPEGENname
1590 ;
1591
1592no_attr_identifier_or_type_name:
1593 no_attr_identifier
1594 | TYPEDEFname
1595 | TYPEGENname
1596 ;
1597
1598type_name_no_function: // sizeof, alignof, cast (constructor)
1599 new_abstract_declarator_tuple // CFA
1600 | type_specifier
1601 | type_specifier variable_abstract_declarator
1602 { $$ = $2->addType( $1 ); }
1603 ;
1604
1605type_name: // typeof, assertion
1606 new_abstract_declarator_tuple // CFA
1607 | new_abstract_function // CFA
1608 | type_specifier
1609 | type_specifier abstract_declarator
1610 { $$ = $2->addType( $1 ); }
1611 ;
1612
1613initializer_opt:
1614 // empty
1615 { $$ = 0; }
1616 | '=' initializer
1617 { $$ = $2; }
1618 ;
1619
1620initializer:
1621 assignment_expression { $$ = new InitializerNode( $1 ); }
1622 | '{' initializer_list comma_opt '}' { $$ = new InitializerNode( $2, true ); }
1623 ;
1624
1625initializer_list:
1626 initializer
1627 | designation initializer { $$ = $2->set_designators( $1 ); }
1628 | initializer_list ',' initializer { $$ = (InitializerNode *)( $1->set_link( $3 ) ); }
1629 | initializer_list ',' designation initializer
1630 { $$ = (InitializerNode *)( $1->set_link( $4->set_designators( $3 ) ) ); }
1631 ;
1632
1633// There is an unreconcileable parsing problem between C99 and CFA with respect to designators. The problem is use of
1634// '=' to separator the designator from the initializer value, as in:
1635//
1636// int x[10] = { [1] = 3 };
1637//
1638// The string "[1] = 3" can be parsed as a designator assignment or a tuple assignment. To disambiguate this case, CFA
1639// changes the syntax from "=" to ":" as the separator between the designator and initializer. GCC does uses ":" for
1640// field selection. The optional use of the "=" in GCC, or in this case ":", cannot be supported either due to
1641// shift/reduce conflicts
1642
1643designation:
1644 designator_list ':' // C99, CFA uses ":" instead of "="
1645 | no_attr_identifier_or_type_name ':' // GCC, field name
1646 { $$ = new VarRefNode( $1 ); }
1647 ;
1648
1649designator_list: // C99
1650 designator
1651 | designator_list designator
1652 { $$ = (ExpressionNode *)( $1->set_link( $2 )); }
1653 //| designator_list designator { $$ = new CompositeExprNode( $1, $2 ); }
1654 ;
1655
1656designator:
1657 '.' no_attr_identifier_or_type_name // C99, field name
1658 { $$ = new VarRefNode( $2 ); }
1659 | '[' push assignment_expression pop ']' // C99, single array element
1660 // assignment_expression used instead of constant_expression because of shift/reduce conflicts with tuple.
1661 { $$ = $3; }
1662 | '[' push subrange pop ']' // CFA, multiple array elements
1663 { $$ = $3; }
1664 | '[' push constant_expression ELLIPSIS constant_expression pop ']' // GCC, multiple array elements
1665 { $$ = new CompositeExprNode( new OperatorNode( OperatorNode::Range ), $3, $5 ); }
1666 | '.' '[' push field_list pop ']' // CFA, tuple field selector
1667 { $$ = $4; }
1668 ;
1669
1670// The CFA type system is based on parametric polymorphism, the ability to declare functions with type parameters,
1671// rather than an object-oriented type system. This required four groups of extensions:
1672//
1673// Overloading: function, data, and operator identifiers may be overloaded.
1674//
1675// Type declarations: "type" is used to generate new types for declaring objects. Similarly, "dtype" is used for object
1676// and incomplete types, and "ftype" is used for function types. Type declarations with initializers provide
1677// definitions of new types. Type declarations with storage class "extern" provide opaque types.
1678//
1679// Polymorphic functions: A forall clause declares a type parameter. The corresponding argument is inferred at the call
1680// site. A polymorphic function is not a template; it is a function, with an address and a type.
1681//
1682// Specifications and Assertions: Specifications are collections of declarations parameterized by one or more
1683// types. They serve many of the purposes of abstract classes, and specification hierarchies resemble subclass
1684// hierarchies. Unlike classes, they can define relationships between types. Assertions declare that a type or
1685// types provide the operations declared by a specification. Assertions are normally used to declare requirements
1686// on type arguments of polymorphic functions.
1687
1688typegen_declaration_specifier: // CFA
1689 typegen_type_specifier
1690 | declaration_qualifier_list typegen_type_specifier
1691 { $$ = $2->addQualifiers( $1 ); }
1692 | typegen_declaration_specifier storage_class // remaining OBSOLESCENT (see 2)
1693 { $$ = $1->addQualifiers( $2 ); }
1694 | typegen_declaration_specifier storage_class type_qualifier_list
1695 { $$ = $1->addQualifiers( $2 )->addQualifiers( $3 ); }
1696 ;
1697
1698typegen_type_specifier: // CFA
1699 typegen_name
1700 | type_qualifier_list typegen_name
1701 { $$ = $2->addQualifiers( $1 ); }
1702 | typegen_type_specifier type_qualifier
1703 { $$ = $1->addQualifiers( $2 ); }
1704 ;
1705
1706typegen_name: // CFA
1707 TYPEGENname '(' type_name_list ')'
1708 { $$ = DeclarationNode::newFromTypeGen( $1, $3 ); }
1709 ;
1710
1711type_parameter_list: // CFA
1712 type_parameter assignment_opt
1713 | type_parameter_list ',' type_parameter assignment_opt
1714 { $$ = $1->appendList( $3 ); }
1715 ;
1716
1717type_parameter: // CFA
1718 type_class no_attr_identifier_or_type_name
1719 { typedefTable.addToEnclosingScope( *$2, TypedefTable::TD ); }
1720 assertion_list_opt
1721 { $$ = DeclarationNode::newTypeParam( $1, $2 )->addAssertions( $4 ); }
1722 | type_specifier identifier_parameter_declarator
1723 ;
1724
1725type_class: // CFA
1726 TYPE
1727 { $$ = DeclarationNode::Type; }
1728 | DTYPE
1729 { $$ = DeclarationNode::Ftype; }
1730 | FTYPE
1731 { $$ = DeclarationNode::Dtype; }
1732 ;
1733
1734assertion_list_opt: // CFA
1735 // empty
1736 { $$ = 0; }
1737 | assertion_list_opt assertion
1738 { $$ = $1 == 0 ? $2 : $1->appendList( $2 ); }
1739 ;
1740
1741assertion: // CFA
1742 '|' no_attr_identifier_or_type_name '(' type_name_list ')'
1743 {
1744 typedefTable.openContext( *$2 );
1745 $$ = DeclarationNode::newContextUse( $2, $4 );
1746 }
1747 | '|' '{' push context_declaration_list '}'
1748 { $$ = $4; }
1749 | '|' '(' push type_parameter_list pop ')' '{' push context_declaration_list '}' '(' type_name_list ')'
1750 { $$ = 0; }
1751 ;
1752
1753type_name_list: // CFA
1754 type_name
1755 { $$ = new TypeValueNode( $1 ); }
1756 | assignment_expression
1757 | type_name_list ',' type_name
1758 { $$ = (ExpressionNode *)( $1->set_link( new TypeValueNode( $3 ))); }
1759 | type_name_list ',' assignment_expression
1760 { $$ = (ExpressionNode *)( $1->set_link( $3 )); }
1761 ;
1762
1763type_declaring_list: // CFA
1764 TYPE type_declarator
1765 { $$ = $2; }
1766 | storage_class_list TYPE type_declarator
1767 { $$ = $3->addQualifiers( $1 ); }
1768 | type_declaring_list ',' type_declarator
1769 { $$ = $1->appendList( $3->copyStorageClasses( $1 ) ); }
1770 ;
1771
1772type_declarator: // CFA
1773 type_declarator_name assertion_list_opt
1774 { $$ = $1->addAssertions( $2 ); }
1775 | type_declarator_name assertion_list_opt '=' type_name
1776 { $$ = $1->addAssertions( $2 )->addType( $4 ); }
1777 ;
1778
1779type_declarator_name: // CFA
1780 no_attr_identifier_or_type_name
1781 {
1782 typedefTable.addToEnclosingScope( *$1, TypedefTable::TD );
1783 $$ = DeclarationNode::newTypeDecl( $1, 0 );
1784 }
1785 | no_01_identifier_or_type_name '(' push type_parameter_list pop ')'
1786 {
1787 typedefTable.addToEnclosingScope( *$1, TypedefTable::TG );
1788 $$ = DeclarationNode::newTypeDecl( $1, $4 );
1789 }
1790 ;
1791
1792context_specifier: // CFA
1793 CONTEXT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{' '}'
1794 {
1795 typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
1796 $$ = DeclarationNode::newContext( $2, $5, 0 );
1797 }
1798 | CONTEXT no_attr_identifier_or_type_name '(' push type_parameter_list pop ')' '{'
1799 {
1800 typedefTable.enterContext( *$2 );
1801 typedefTable.enterScope();
1802 }
1803 context_declaration_list '}'
1804 {
1805 typedefTable.leaveContext();
1806 typedefTable.addToEnclosingScope( *$2, TypedefTable::ID );
1807 $$ = DeclarationNode::newContext( $2, $5, $10 );
1808 }
1809 ;
1810
1811context_declaration_list: // CFA
1812 context_declaration
1813 | context_declaration_list push context_declaration
1814 { $$ = $1->appendList( $3 ); }
1815 ;
1816
1817context_declaration: // CFA
1818 new_context_declaring_list pop ';'
1819 | context_declaring_list pop ';'
1820 ;
1821
1822new_context_declaring_list: // CFA
1823 new_variable_specifier
1824 {
1825 typedefTable.addToEnclosingScope2( TypedefTable::ID );
1826 $$ = $1;
1827 }
1828 | new_function_specifier
1829 {
1830 typedefTable.addToEnclosingScope2( TypedefTable::ID );
1831 $$ = $1;
1832 }
1833 | new_context_declaring_list pop ',' push identifier_or_type_name
1834 {
1835 typedefTable.addToEnclosingScope2( *$5, TypedefTable::ID );
1836 $$ = $1->appendList( $1->cloneType( $5 ) );
1837 }
1838 ;
1839
1840context_declaring_list: // CFA
1841 type_specifier declarator
1842 {
1843 typedefTable.addToEnclosingScope2( TypedefTable::ID );
1844 $$ = $2->addType( $1 );
1845 }
1846 | context_declaring_list pop ',' push declarator
1847 {
1848 typedefTable.addToEnclosingScope2( TypedefTable::ID );
1849 $$ = $1->appendList( $1->cloneBaseType( $5 ) );
1850 }
1851 ;
1852
1853//***************************** EXTERNAL DEFINITIONS *****************************
1854
1855translation_unit:
1856 // empty
1857 {} // empty input file
1858 | external_definition_list
1859 {
1860 if ( theTree ) {
1861 theTree->appendList( $1 );
1862 } else {
1863 theTree = $1;
1864 }
1865 }
1866 ;
1867
1868external_definition_list:
1869 external_definition
1870 | external_definition_list push external_definition
1871 { $$ = ( $1 != NULL ) ? $1->appendList( $3 ) : $3; }
1872 ;
1873
1874external_definition_list_opt:
1875 // empty
1876 { $$ = 0; }
1877 | external_definition_list
1878 ;
1879
1880external_definition:
1881 declaration
1882 | external_function_definition
1883 | asm_statement // GCC, global assembler statement
1884 {}
1885 | EXTERN STRINGliteral
1886 {
1887 linkageStack.push( linkage );
1888 linkage = LinkageSpec::fromString( *$2 );
1889 }
1890 '{' external_definition_list_opt '}' // C++-style linkage specifier
1891 {
1892 linkage = linkageStack.top();
1893 linkageStack.pop();
1894 $$ = $5;
1895 }
1896 | EXTENSION external_definition
1897 { $$ = $2; }
1898 ;
1899
1900external_function_definition:
1901 function_definition
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 | type_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_type_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 type_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 type 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
2188type_redeclarator:
2189 paren_type attribute_list_opt
2190 | type_ptr
2191 | type_array attribute_list_opt
2192 | type_function attribute_list_opt
2193 ;
2194
2195paren_type:
2196 typedef
2197 | '(' paren_type ')'
2198 { $$ = $2; }
2199 ;
2200
2201type_ptr:
2202 '*' type_redeclarator
2203 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2204 | '*' type_qualifier_list type_redeclarator
2205 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2206 | '(' type_ptr ')'
2207 { $$ = $2; }
2208 ;
2209
2210type_array:
2211 paren_type array_dimension
2212 { $$ = $1->addArray( $2 ); }
2213 | '(' type_ptr ')' array_dimension
2214 { $$ = $2->addArray( $4 ); }
2215 | '(' type_array ')' multi_array_dimension // redundant parenthesis
2216 { $$ = $2->addArray( $4 ); }
2217 | '(' type_array ')' // redundant parenthesis
2218 { $$ = $2; }
2219 ;
2220
2221type_function:
2222 paren_type '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2223 { $$ = $1->addParamList( $4 ); }
2224 | '(' type_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2225 { $$ = $2->addParamList( $6 ); }
2226 | '(' type_function ')' // redundant parenthesis
2227 { $$ = $2; }
2228 ;
2229
2230// This pattern parses a declaration for a parameter variable or function prototype that is not redefining a typedef
2231// name and allows the C99 array options, which can only appear in a parameter list. The pattern precludes declaring an
2232// array of functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to
2233// arrays and functions.
2234
2235identifier_parameter_declarator:
2236 paren_identifier attribute_list_opt
2237 | identifier_parameter_ptr
2238 | identifier_parameter_array attribute_list_opt
2239 | identifier_parameter_function attribute_list_opt
2240 ;
2241
2242identifier_parameter_ptr:
2243 '*' identifier_parameter_declarator
2244 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2245 | '*' type_qualifier_list identifier_parameter_declarator
2246 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2247 | '(' identifier_parameter_ptr ')'
2248 { $$ = $2; }
2249 ;
2250
2251identifier_parameter_array:
2252 paren_identifier array_parameter_dimension
2253 { $$ = $1->addArray( $2 ); }
2254 | '(' identifier_parameter_ptr ')' array_dimension
2255 { $$ = $2->addArray( $4 ); }
2256 | '(' identifier_parameter_array ')' multi_array_dimension // redundant parenthesis
2257 { $$ = $2->addArray( $4 ); }
2258 | '(' identifier_parameter_array ')' // redundant parenthesis
2259 { $$ = $2; }
2260 ;
2261
2262identifier_parameter_function:
2263 paren_identifier '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2264 { $$ = $1->addParamList( $4 ); }
2265 | '(' identifier_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2266 { $$ = $2->addParamList( $6 ); }
2267 | '(' identifier_parameter_function ')' // redundant parenthesis
2268 { $$ = $2; }
2269 ;
2270
2271// This pattern parses a declaration for a parameter variable or function prototype that is redefining a typedef name,
2272// e.g.:
2273//
2274// typedef int foo;
2275// int f( int foo ); // redefine typedef name in new scope
2276//
2277// and allows the C99 array options, which can only appear in a parameter list. In addition, the pattern handles the
2278// special meaning of parenthesis around a typedef name:
2279//
2280// ISO/IEC 9899:1999 Section 6.7.5.3(11) : "In a parameter declaration, a single typedef name in
2281// parentheses is taken to be an abstract declarator that specifies a function with a single parameter,
2282// not as redundant parentheses around the identifier."
2283//
2284// which precludes the following cases:
2285//
2286// typedef float T;
2287// int f( int ( T [5] ) ); // see abstract_parameter_declarator
2288// int g( int ( T ( int ) ) ); // see abstract_parameter_declarator
2289// int f( int f1( T a[5] ) ); // see identifier_parameter_declarator
2290// int g( int g1( T g2( int p ) ) ); // see identifier_parameter_declarator
2291//
2292// In essence, a '(' immediately to the left of typedef name, T, is interpreted as starting a parameter type list, and
2293// not as redundant parentheses around a redeclaration of T. Finally, the pattern also precludes declaring an array of
2294// functions versus a pointer to an array of functions, and returning arrays and functions versus pointers to arrays and
2295// functions.
2296
2297type_parameter_redeclarator:
2298 typedef attribute_list_opt
2299 | type_parameter_ptr
2300 | type_parameter_array attribute_list_opt
2301 | type_parameter_function attribute_list_opt
2302 ;
2303
2304typedef:
2305 TYPEDEFname
2306 {
2307 typedefTable.setNextIdentifier( *$1 );
2308 $$ = DeclarationNode::newName( $1 );
2309 }
2310 | TYPEGENname
2311 {
2312 typedefTable.setNextIdentifier( *$1 );
2313 $$ = DeclarationNode::newName( $1 );
2314 }
2315 ;
2316
2317type_parameter_ptr:
2318 '*' type_parameter_redeclarator
2319 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2320 | '*' type_qualifier_list type_parameter_redeclarator
2321 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2322 | '(' type_parameter_ptr ')'
2323 { $$ = $2; }
2324 ;
2325
2326type_parameter_array:
2327 typedef array_parameter_dimension
2328 { $$ = $1->addArray( $2 ); }
2329 | '(' type_parameter_ptr ')' array_parameter_dimension
2330 { $$ = $2->addArray( $4 ); }
2331 ;
2332
2333type_parameter_function:
2334 typedef '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2335 { $$ = $1->addParamList( $4 ); }
2336 | '(' type_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2337 { $$ = $2->addParamList( $6 ); }
2338 ;
2339
2340// This pattern parses a declaration of an abstract variable or function prototype, i.e., there is no identifier to
2341// which the type applies, e.g.:
2342//
2343// sizeof( int );
2344// sizeof( int [10] );
2345//
2346// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2347// and functions versus pointers to arrays and functions.
2348
2349abstract_declarator:
2350 abstract_ptr
2351 | abstract_array attribute_list_opt
2352 | abstract_function attribute_list_opt
2353 ;
2354
2355abstract_ptr:
2356 '*'
2357 { $$ = DeclarationNode::newPointer( 0 ); }
2358 | '*' type_qualifier_list
2359 { $$ = DeclarationNode::newPointer( $2 ); }
2360 | '*' abstract_declarator
2361 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2362 | '*' type_qualifier_list abstract_declarator
2363 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2364 | '(' abstract_ptr ')'
2365 { $$ = $2; }
2366 ;
2367
2368abstract_array:
2369 array_dimension
2370 | '(' abstract_ptr ')' array_dimension
2371 { $$ = $2->addArray( $4 ); }
2372 | '(' abstract_array ')' multi_array_dimension // redundant parenthesis
2373 { $$ = $2->addArray( $4 ); }
2374 | '(' abstract_array ')' // redundant parenthesis
2375 { $$ = $2; }
2376 ;
2377
2378abstract_function:
2379 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2380 { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2381 | '(' abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2382 { $$ = $2->addParamList( $6 ); }
2383 | '(' abstract_function ')' // redundant parenthesis
2384 { $$ = $2; }
2385 ;
2386
2387array_dimension:
2388 // Only the first dimension can be empty.
2389 '[' ']'
2390 { $$ = DeclarationNode::newArray( 0, 0, false ); }
2391 | '[' ']' multi_array_dimension
2392 { $$ = DeclarationNode::newArray( 0, 0, false )->addArray( $3 ); }
2393 | multi_array_dimension
2394 ;
2395
2396multi_array_dimension:
2397 '[' push assignment_expression pop ']'
2398 { $$ = DeclarationNode::newArray( $3, 0, false ); }
2399 | '[' push '*' pop ']' // C99
2400 { $$ = DeclarationNode::newVarArray( 0 ); }
2401 | multi_array_dimension '[' push assignment_expression pop ']'
2402 { $$ = $1->addArray( DeclarationNode::newArray( $4, 0, false ) ); }
2403 | multi_array_dimension '[' push '*' pop ']' // C99
2404 { $$ = $1->addArray( DeclarationNode::newVarArray( 0 ) ); }
2405 ;
2406
2407// This pattern parses a declaration of a parameter abstract variable or function prototype, i.e., there is no
2408// identifier to which the type applies, e.g.:
2409//
2410// int f( int ); // abstract variable parameter; no parameter name specified
2411// int f( int (int) ); // abstract function-prototype parameter; no parameter name specified
2412//
2413// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2414// and functions versus pointers to arrays and functions.
2415
2416abstract_parameter_declarator:
2417 abstract_parameter_ptr
2418 | abstract_parameter_array attribute_list_opt
2419 | abstract_parameter_function attribute_list_opt
2420 ;
2421
2422abstract_parameter_ptr:
2423 '*'
2424 { $$ = DeclarationNode::newPointer( 0 ); }
2425 | '*' type_qualifier_list
2426 { $$ = DeclarationNode::newPointer( $2 ); }
2427 | '*' abstract_parameter_declarator
2428 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2429 | '*' type_qualifier_list abstract_parameter_declarator
2430 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2431 | '(' abstract_parameter_ptr ')'
2432 { $$ = $2; }
2433 ;
2434
2435abstract_parameter_array:
2436 array_parameter_dimension
2437 | '(' abstract_parameter_ptr ')' array_parameter_dimension
2438 { $$ = $2->addArray( $4 ); }
2439 | '(' abstract_parameter_array ')' multi_array_dimension // redundant parenthesis
2440 { $$ = $2->addArray( $4 ); }
2441 | '(' abstract_parameter_array ')' // redundant parenthesis
2442 { $$ = $2; }
2443 ;
2444
2445abstract_parameter_function:
2446 '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2447 { $$ = DeclarationNode::newFunction( 0, 0, $3, 0 ); }
2448 | '(' abstract_parameter_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2449 { $$ = $2->addParamList( $6 ); }
2450 | '(' abstract_parameter_function ')' // redundant parenthesis
2451 { $$ = $2; }
2452 ;
2453
2454array_parameter_dimension:
2455 // Only the first dimension can be empty or have qualifiers.
2456 array_parameter_1st_dimension
2457 | array_parameter_1st_dimension multi_array_dimension
2458 { $$ = $1->addArray( $2 ); }
2459 | multi_array_dimension
2460 ;
2461
2462// The declaration of an array parameter has additional syntax over arrays in normal variable declarations:
2463//
2464// ISO/IEC 9899:1999 Section 6.7.5.2(1) : "The optional type qualifiers and the keyword static shall appear only in
2465// a declaration of a function parameter with an array type, and then only in the outermost array type derivation."
2466
2467array_parameter_1st_dimension:
2468 '[' ']'
2469 { $$ = DeclarationNode::newArray( 0, 0, false ); }
2470 // multi_array_dimension handles the '[' '*' ']' case
2471 | '[' push type_qualifier_list '*' pop ']' // remaining C99
2472 { $$ = DeclarationNode::newVarArray( $3 ); }
2473 | '[' push type_qualifier_list pop ']'
2474 { $$ = DeclarationNode::newArray( 0, $3, false ); }
2475 // multi_array_dimension handles the '[' assignment_expression ']' case
2476 | '[' push type_qualifier_list assignment_expression pop ']'
2477 { $$ = DeclarationNode::newArray( $4, $3, false ); }
2478 | '[' push STATIC type_qualifier_list_opt assignment_expression pop ']'
2479 { $$ = DeclarationNode::newArray( $5, $4, true ); }
2480 | '[' push type_qualifier_list STATIC assignment_expression pop ']'
2481 { $$ = DeclarationNode::newArray( $5, $3, true ); }
2482 ;
2483
2484// This pattern parses a declaration of an abstract variable, i.e., there is no identifier to which the type applies,
2485// e.g.:
2486//
2487// sizeof( int ); // abstract variable; no identifier name specified
2488//
2489// The pattern precludes declaring an array of functions versus a pointer to an array of functions, and returning arrays
2490// and functions versus pointers to arrays and functions.
2491
2492variable_abstract_declarator:
2493 variable_abstract_ptr
2494 | variable_abstract_array attribute_list_opt
2495 | variable_abstract_function attribute_list_opt
2496 ;
2497
2498variable_abstract_ptr:
2499 '*'
2500 { $$ = DeclarationNode::newPointer( 0 ); }
2501 | '*' type_qualifier_list
2502 { $$ = DeclarationNode::newPointer( $2 ); }
2503 | '*' variable_abstract_declarator
2504 { $$ = $2->addPointer( DeclarationNode::newPointer( 0 ) ); }
2505 | '*' type_qualifier_list variable_abstract_declarator
2506 { $$ = $3->addPointer( DeclarationNode::newPointer( $2 ) ); }
2507 | '(' variable_abstract_ptr ')'
2508 { $$ = $2; }
2509 ;
2510
2511variable_abstract_array:
2512 array_dimension
2513 | '(' variable_abstract_ptr ')' array_dimension
2514 { $$ = $2->addArray( $4 ); }
2515 | '(' variable_abstract_array ')' multi_array_dimension // redundant parenthesis
2516 { $$ = $2->addArray( $4 ); }
2517 | '(' variable_abstract_array ')' // redundant parenthesis
2518 { $$ = $2; }
2519 ;
2520
2521variable_abstract_function:
2522 '(' variable_abstract_ptr ')' '(' push parameter_type_list_opt pop ')' // empty parameter list OBSOLESCENT (see 3)
2523 { $$ = $2->addParamList( $6 ); }
2524 | '(' variable_abstract_function ')' // redundant parenthesis
2525 { $$ = $2; }
2526 ;
2527
2528// This pattern parses a new-style declaration for a parameter variable or function prototype that is either an
2529// identifier or typedef name and allows the C99 array options, which can only appear in a parameter list.
2530
2531new_identifier_parameter_declarator_tuple: // CFA
2532 new_identifier_parameter_declarator_no_tuple
2533 | new_abstract_tuple
2534 | type_qualifier_list new_abstract_tuple
2535 { $$ = $2->addQualifiers( $1 ); }
2536 ;
2537
2538new_identifier_parameter_declarator_no_tuple: // CFA
2539 new_identifier_parameter_ptr
2540 | new_identifier_parameter_array
2541 ;
2542
2543new_identifier_parameter_ptr: // CFA
2544 '*' type_specifier
2545 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2546 | type_qualifier_list '*' type_specifier
2547 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2548 | '*' new_abstract_function
2549 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2550 | type_qualifier_list '*' new_abstract_function
2551 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2552 | '*' new_identifier_parameter_declarator_tuple
2553 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2554 | type_qualifier_list '*' new_identifier_parameter_declarator_tuple
2555 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2556 ;
2557
2558new_identifier_parameter_array: // CFA
2559 // Only the first dimension can be empty or have qualifiers. Empty dimension must be factored out due to
2560 // shift/reduce conflict with new-style empty (void) function return type.
2561 '[' ']' type_specifier
2562 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2563 | new_array_parameter_1st_dimension type_specifier
2564 { $$ = $2->addNewArray( $1 ); }
2565 | '[' ']' multi_array_dimension type_specifier
2566 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2567 | new_array_parameter_1st_dimension multi_array_dimension type_specifier
2568 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2569 | multi_array_dimension type_specifier
2570 { $$ = $2->addNewArray( $1 ); }
2571 | '[' ']' new_identifier_parameter_ptr
2572 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2573 | new_array_parameter_1st_dimension new_identifier_parameter_ptr
2574 { $$ = $2->addNewArray( $1 ); }
2575 | '[' ']' multi_array_dimension new_identifier_parameter_ptr
2576 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2577 | new_array_parameter_1st_dimension multi_array_dimension new_identifier_parameter_ptr
2578 { $$ = $3->addNewArray( $2 )->addNewArray( $1 ); }
2579 | multi_array_dimension new_identifier_parameter_ptr
2580 { $$ = $2->addNewArray( $1 ); }
2581 ;
2582
2583new_array_parameter_1st_dimension:
2584 '[' push type_qualifier_list '*' pop ']' // remaining C99
2585 { $$ = DeclarationNode::newVarArray( $3 ); }
2586 | '[' push type_qualifier_list assignment_expression pop ']'
2587 { $$ = DeclarationNode::newArray( $4, $3, false ); }
2588 | '[' push declaration_qualifier_list assignment_expression pop ']'
2589 // declaration_qualifier_list must be used because of shift/reduce conflict with
2590 // assignment_expression, so a semantic check is necessary to preclude them as a type_qualifier cannot
2591 // appear in this context.
2592 { $$ = DeclarationNode::newArray( $4, $3, true ); }
2593 | '[' push declaration_qualifier_list type_qualifier_list assignment_expression pop ']'
2594 { $$ = DeclarationNode::newArray( $5, $4->addQualifiers( $3 ), true ); }
2595 ;
2596
2597// This pattern parses a new-style declaration of an abstract variable or function prototype, i.e., there is no
2598// identifier to which the type applies, e.g.:
2599//
2600// [int] f( int ); // abstract variable parameter; no parameter name specified
2601// [int] f( [int] (int) ); // abstract function-prototype parameter; no parameter name specified
2602//
2603// These rules need LR(3):
2604//
2605// new_abstract_tuple identifier_or_type_name
2606// '[' new_parameter_list ']' identifier_or_type_name '(' new_parameter_type_list_opt ')'
2607//
2608// since a function return type can be syntactically identical to a tuple type:
2609//
2610// [int, int] t;
2611// [int, int] f( int );
2612//
2613// Therefore, it is necessary to look at the token after identifier_or_type_name to know when to reduce
2614// new_abstract_tuple. To make this LR(1), several rules have to be flattened (lengthened) to allow the necessary
2615// lookahead. To accomplish this, new_abstract_declarator has an entry point without tuple, and tuple declarations are
2616// duplicated when appearing with new_function_specifier.
2617
2618new_abstract_declarator_tuple: // CFA
2619 new_abstract_tuple
2620 | type_qualifier_list new_abstract_tuple
2621 { $$ = $2->addQualifiers( $1 ); }
2622 | new_abstract_declarator_no_tuple
2623 ;
2624
2625new_abstract_declarator_no_tuple: // CFA
2626 new_abstract_ptr
2627 | new_abstract_array
2628 ;
2629
2630new_abstract_ptr: // CFA
2631 '*' type_specifier
2632 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2633 | type_qualifier_list '*' type_specifier
2634 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2635 | '*' new_abstract_function
2636 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2637 | type_qualifier_list '*' new_abstract_function
2638 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2639 | '*' new_abstract_declarator_tuple
2640 { $$ = $2->addNewPointer( DeclarationNode::newPointer( 0 ) ); }
2641 | type_qualifier_list '*' new_abstract_declarator_tuple
2642 { $$ = $3->addNewPointer( DeclarationNode::newPointer( $1 ) ); }
2643 ;
2644
2645new_abstract_array: // CFA
2646 // Only the first dimension can be empty. Empty dimension must be factored out due to shift/reduce conflict with
2647 // empty (void) function return type.
2648 '[' ']' type_specifier
2649 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2650 | '[' ']' multi_array_dimension type_specifier
2651 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2652 | multi_array_dimension type_specifier
2653 { $$ = $2->addNewArray( $1 ); }
2654 | '[' ']' new_abstract_ptr
2655 { $$ = $3->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2656 | '[' ']' multi_array_dimension new_abstract_ptr
2657 { $$ = $4->addNewArray( $3 )->addNewArray( DeclarationNode::newArray( 0, 0, false ) ); }
2658 | multi_array_dimension new_abstract_ptr
2659 { $$ = $2->addNewArray( $1 ); }
2660 ;
2661
2662new_abstract_tuple: // CFA
2663 '[' push new_abstract_parameter_list pop ']'
2664 { $$ = DeclarationNode::newTuple( $3 ); }
2665 ;
2666
2667new_abstract_function: // CFA
2668 '[' ']' '(' new_parameter_type_list_opt ')'
2669 { $$ = DeclarationNode::newFunction( 0, DeclarationNode::newTuple( 0 ), $4, 0 ); }
2670 | new_abstract_tuple '(' push new_parameter_type_list_opt pop ')'
2671 { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2672 | new_function_return '(' push new_parameter_type_list_opt pop ')'
2673 { $$ = DeclarationNode::newFunction( 0, $1, $4, 0 ); }
2674 ;
2675
2676// 1) ISO/IEC 9899:1999 Section 6.7.2(2) : "At least one type specifier shall be given in the declaration specifiers in
2677// each declaration, and in the specifier-qualifier list in each structure declaration and type name."
2678//
2679// 2) ISO/IEC 9899:1999 Section 6.11.5(1) : "The placement of a storage-class specifier other than at the beginning of
2680// the declaration specifiers in a declaration is an obsolescent feature."
2681//
2682// 3) ISO/IEC 9899:1999 Section 6.11.6(1) : "The use of function declarators with empty parentheses (not
2683// prototype-format parameter type declarators) is an obsolescent feature."
2684//
2685// 4) ISO/IEC 9899:1999 Section 6.11.7(1) : "The use of function definitions with separate parameter identifier and
2686// declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature.
2687
2688//************************* MISCELLANEOUS ********************************
2689
2690comma_opt: // redundant comma
2691 // empty
2692 | ','
2693 ;
2694
2695assignment_opt:
2696 // empty
2697 { $$ = 0; }
2698 | '=' assignment_expression
2699 { $$ = $2; }
2700 ;
2701
2702%%
2703// ----end of grammar----
2704
2705void yyerror( const char * ) {
2706 std::cout << "Error ";
2707 if ( yyfilename ) {
2708 std::cout << "in file " << yyfilename << " ";
2709 } // if
2710 std::cout << "at line " << yylineno << " reading token \"" << (yytext[0] == '\0' ? "EOF" : yytext) << "\"" << std::endl;
2711}
2712
2713// Local Variables: //
2714// mode: c++ //
2715// tab-width: 4 //
2716// compile-command: "make install" //
2717// End: //
Note: See TracBrowser for help on using the repository browser.