source: src/Parser/parser.yy@ ed9ecda

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

changes for switch and choose statements

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