source: src/Parser/parser.yy@ 8c84ebd

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 8c84ebd was 7f5566b, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

asm statement, memory leaks

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