source: src/Parser/parser.yy@ 4789f44

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 4789f44 was 5721a6d, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

correctly set type for complex constants, consolidate function name tables, add offsetof, refactor printing complex constants to use basic types

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