source: src/Parser/parser.yy @ 861799c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 861799c was 861799c, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

fix 0,1 member access, fix autogenerated routines missing size for sized generic types

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