⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.y

📁 cg编译器
💻 Y
📖 第 1 页 / 共 3 页
字号:
                              { $$ = LookUpTypeSymbol(NULL, $1); }
                        | error
                              { 
                                SemanticParseError(Cg->tokenLoc, ERROR_S_TYPE_NAME_EXPECTED,
                                                   GetAtomString(atable, Cg->mostRecentToken /* yychar */));
                                $$ = UndefinedType;
                              }
;

/*******************/
/* Type Qualifiers */
/*******************/

type_qualifier:           CONST_SY
                              { $$ = TYPE_QUALIFIER_CONST; }
;

/****************/
/* Type Domains */
/****************/

type_domain:              UNIFORM_SY
                              { $$ = TYPE_DOMAIN_UNIFORM; }
;

/*******************/
/* Storage Classes */
/*******************/

storage_class:            STATIC_SY
                              { $$ = (int) SC_STATIC; }
                        | EXTERN_SY
                              { $$ = (int) SC_EXTERN; }
;

/**********************/
/* Function Specifier */
/**********************/

function_specifier:       INLINE_SY
                              { $$ = TYPE_MISC_INLINE; }
                        | INTERNAL_SY
                              { $$ = TYPE_MISC_INTERNAL; }
;

/**********/
/* In Out */
/**********/

in_out:                   IN_SY
                              { $$ = TYPE_QUALIFIER_IN; }
                        | OUT_SY
                              { $$ = TYPE_QUALIFIER_OUT; }
                        | INOUT_SY
                              { $$ = TYPE_QUALIFIER_INOUT; }
;

/****************/
/* Struct Types */
/****************/

struct_or_connector_specifier:
                          struct_or_connector_header struct_compound_header struct_declaration_list '}'
                              { $$ = SetStructMembers(Cg->tokenLoc, $1, PopScope()); }
                        | untagged_struct_header struct_compound_header struct_declaration_list '}'
                              { $$ = SetStructMembers(Cg->tokenLoc, $1, PopScope()); }
                        | struct_or_connector_header
                              { $$ = $1; }
;

struct_compound_header:   compound_header
                              { CurrentScope->IsStructScope = 1; $$ = $1; }
;

struct_or_connector_header:
                          STRUCT_SY struct_identifier
                              { $$ = StructHeader(Cg->tokenLoc, CurrentScope, 0, $2); }
                        | STRUCT_SY struct_identifier ':' semantics_identifier
                              { $$ = StructHeader(Cg->tokenLoc, CurrentScope, $4, $2); }
;

struct_identifier:        identifier
                        | type_identifier
;

untagged_struct_header:   STRUCT_SY
                              { $$ = StructHeader(Cg->tokenLoc, CurrentScope, 0, 0); }
;

struct_declaration_list:  struct_declaration
                        | struct_declaration_list struct_declaration
;

struct_declaration:       declaration
                            { $$ = $1; }
;

/**************/
/* Type Names */
/**************/

/*** Not used -- Use abstract_declaration" instead ***
type_name:                type_specifier
                        | type_qualifier type_name
                              { $$ = $2; }
;
***/

/***************/
/* Annotations */
/***************/

annotation:               '<' { PushScope(NewScope()); } annotation_decl_list '>'
                              { $$ = $3; PopScope(); }
;

annotation_decl_list:     /* empty */
                              { $$ = 0; }
                        | annotation_decl_list declaration
;

/***************/
/* Declarators */
/***************/

declarator:               semantic_declarator
                              { $$ = $1; }
                        | semantic_declarator annotation
                              { $$ = $1; }
;

semantic_declarator:      basic_declarator
                              { $$ = Declarator(Cg->tokenLoc, $1, 0); }
                        | basic_declarator ':' semantics_identifier
                              { $$ = Declarator(Cg->tokenLoc, $1, $3); }
;

basic_declarator:         identifier
                              { $$ = NewDeclNode(Cg->tokenLoc, $1, &CurrentDeclTypeSpecs); }
                        | basic_declarator '[' INTCONST_SY /* constant_expression */ ']'
                              { $$ = Array_Declarator(Cg->tokenLoc, $1, $3, 0); }
                        | basic_declarator '[' ']'
                              { $$ = Array_Declarator(Cg->tokenLoc, $1, 0 , 1); }
                        | function_decl_header parameter_list ')'
                              { $$ = SetFunTypeParams(CurrentScope, $1, $2, $2); }
                        | function_decl_header abstract_parameter_list ')'
                              { $$ = SetFunTypeParams(CurrentScope, $1, $2, NULL); }
;

function_decl_header:     basic_declarator '('
                              { $$ = FunctionDeclHeader(&$1->loc, CurrentScope, $1); }
;

abstract_declarator:      /* empty */
                              { $$ = NewDeclNode(Cg->tokenLoc, 0, &CurrentDeclTypeSpecs); }
                        | abstract_declarator '[' INTCONST_SY /* constant_expression */  ']'
                              { $$ = Array_Declarator(Cg->tokenLoc, $1, $3, 0); }
                        | abstract_declarator '[' ']'
                              { $$ = Array_Declarator(Cg->tokenLoc, $1, 0 , 1); }
/***
 *** This rule causes a major shift reduce conflict with:
 ***
 ***      primary_expression  :;=  type_specifier '(' expression_list ')'
 ***
 *** Cannot be easily factored.  Would force: "( expr -list )" to be merged with "( abstract-param-list )"
 ***
 *** Matches other shading languages' syntax.
 *** Will disallow abstract literal function parameter declarations should we ever defide to
 ***      support function parameters in the future.
 ***
                        | abstract_declarator '(' abstract_parameter_list ')'
***/
;

parameter_list:           parameter_declaration
                              { $$ = $1; }
                        | parameter_list ',' parameter_declaration
                              { $$ = AddDecl($1, $3); }
;

parameter_declaration:    declaration_specifiers declarator
                              { $$ = Param_Init_Declarator(Cg->tokenLoc, CurrentScope, $2, NULL); }
                        | declaration_specifiers declarator '=' initializer
                              { $$ = Param_Init_Declarator(Cg->tokenLoc, CurrentScope, $2, $4); }
;

abstract_parameter_list:  /* empty */
                              { $$ = NULL; }
                        | non_empty_abstract_parameter_list
;

non_empty_abstract_parameter_list:  abstract_declaration
                              {
                                if (IsVoid(&$1->type.type))
                                    CurrentScope->HasVoidParameter = 1;
                                $$ = $1;
                              }
                        | non_empty_abstract_parameter_list ',' abstract_declaration
                              {
                                if (CurrentScope->HasVoidParameter || IsVoid(&$1->type.type)) {
                                    SemanticError(Cg->tokenLoc, ERROR___VOID_NOT_ONLY_PARAM);
                                }
                                $$ = AddDecl($1, $3);
                              }
;

/******************/
/* Initialization */
/******************/

initializer:              expression
                              { $$ = Initializer(Cg->tokenLoc, $1); }
                        | '{' initializer_list '}'
                              { $$ = Initializer(Cg->tokenLoc, $2); }
                        | '{' initializer_list ',' '}'
                              { $$ = Initializer(Cg->tokenLoc, $2); }
;

initializer_list:         initializer
                              { $$ = InitializerList(Cg->tokenLoc, $1, NULL); }
                        | initializer_list ',' initializer
                              { $$ = InitializerList(Cg->tokenLoc, $1, $3); }
;

/***************/
/* EXPRESSIONS */
/***************/

/************/
/* Variable */
/************/

variable:                 basic_variable
                              { $$ = $1; }
                        | scope_identifier COLONCOLON_SY basic_variable
                              { $$ = $3; }
;

basic_variable:           variable_identifier
                              { $$ = BasicVariable(Cg->tokenLoc, $1); }
;

/**********************/
/* Primary Expression */
/**********************/

primary_expression:       variable
                        | constant
                        | '(' expression ')'
                              { $$ = $2; }
                        | type_specifier '(' expression_list ')'
                              { $$ = NewVectorConstructor(Cg->tokenLoc, $1, $3); }
;

/*********************/
/* Postfix Operators */
/*********************/

postfix_expression:       primary_expression
                        | postfix_expression PLUSPLUS_SY
                              { $$ = (expr *) NewUnopNode(POSTINC_OP, $1); }
                        | postfix_expression MINUSMINUS_SY
                              { $$ = (expr *) NewUnopNode(POSTDEC_OP, $1); }
                        | postfix_expression '.' member_identifier
                              { $$ = NewMemberSelectorOrSwizzleOrWriteMaskOperator(Cg->tokenLoc, $1, $3); }
                        | postfix_expression '[' expression ']'
                              { $$ = NewIndexOperator(Cg->tokenLoc, $1, $3); }
                        | postfix_expression '(' actual_argument_list ')'
                              { $$ = NewFunctionCallOperator(Cg->tokenLoc, $1, $3); }
;

actual_argument_list:     /* empty */
                                { $$ = NULL; }
                        | non_empty_argument_list
;

non_empty_argument_list:  expression
                              { $$ = ArgumentList(Cg->tokenLoc, NULL, $1); }
                        | non_empty_argument_list ',' expression
                              { $$ = ArgumentList(Cg->tokenLoc, $1, $3); }
;

expression_list:          expression
                              { $$ = ExpressionList(Cg->tokenLoc, NULL, $1); }
                        | expression_list ',' expression
                              { $$ = ExpressionList(Cg->tokenLoc, $1, $3); }
;

/*******************/
/* Unary Operators */
/*******************/

unary_expression:         postfix_expression
                        | PLUSPLUS_SY unary_expression
                              { $$ = (expr *) NewUnopNode(PREINC_OP, $2); }
                        | MINUSMINUS_SY unary_expression
                              { $$ = (expr *) NewUnopNode(PREDEC_OP, $2); }
                        | '+' unary_expression
                              { $$ = NewUnaryOperator(Cg->tokenLoc, POS_OP, '+', $2, 0); }
                        | '-' unary_expression
                              { $$ = NewUnaryOperator(Cg->tokenLoc, NEG_OP, '-', $2, 0); }
                        | '!' unary_expression
                              { $$ = NewUnaryOperator(Cg->tokenLoc, BNOT_OP, '!', $2, 0); }
                        | '~' unary_expression
                              { $$ = NewUnaryOperator(Cg->tokenLoc, NOT_OP, '~', $2, 1); }
;

/*****************/
/* Cast Operator */
/*****************/

cast_expression:          unary_expression
/* *** reduce/reduce conflict: (var-ident) (type-ident) ***
                        | '(' type_name ')' cast_expression
*/
                        | '(' abstract_declaration ')' cast_expression
                              { $$ = NewCastOperator(Cg->tokenLoc, $4, GetTypePointer(&$2->loc, &$2->type)); }
;

/****************************/
/* Multiplicative Operators */
/****************************/

multiplicative_expression: cast_expression
                        | multiplicative_expression '*' cast_expression
                              { $$ = NewBinaryOperator(Cg->tokenLoc, MUL_OP, '*', $1, $3, 0); }
                        | multiplicative_expression '/' cast_expression
                              { $$ = NewBinaryOperator(Cg->tokenLoc, DIV_OP, '/', $1, $3, 0); }
                        | multiplicative_expression '%' cast_expression
                              { $$ = NewBinaryOperator(Cg->tokenLoc, MOD_OP, '%', $1, $3, 1); }
;

/**********************/
/* Addative Operators */
/**********************/

additive_expression:      multiplicative_expression
                        | additive_expression '+' multiplicative_expression
                              { $$ = NewBinaryOperator(Cg->tokenLoc, ADD_OP, '+', $1, $3, 0); }
                        | additive_expression '-' multiplicative_expression
                              { $$ = NewBinaryOperator(Cg->tokenLoc, SUB_OP, '-', $1, $3, 0); }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -