source: src/Parser/parser.yy@ a6dd5b0

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 a6dd5b0 was d1625f8, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

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