source: src/Parser/parser.yy@ ae42f2a

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 with_gc
Last change on this file since ae42f2a was d63eeb0, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/GenPoly/Box.cc
src/Makefile.in
src/Parser/ParseNode.h
src/Parser/parser.cc
src/Parser/parser.yy
src/SymTab/Validate.cc
src/SynTree/Initializer.h
src/SynTree/ObjectDecl.cc
src/SynTree/Visitor.h
src/main.cc

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