source: src/Parser/parser.yy@ 5ae36ed

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 5ae36ed was 2f22cc4, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

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