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

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 5aa708c was 1db21619, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

add CFA flag, remove -p from cc1, typedef on functions become function declarations, move isInline/isNoreturn to Declaration, cleaned up label code

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