source: src/Parser/parser.yy@ 258eb5c9

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 258eb5c9 was 097e2b0, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

constructor/destructor, more example programs

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