📄 parser.y
字号:
;
/***************************/
/* Bitwise Shift Operators */
/***************************/
shift_expression: additive_expression
| shift_expression LL_SY additive_expression
{ $$ = NewBinaryOperator(Cg->tokenLoc, SHL_OP, LL_SY, $1, $3, 1); }
| shift_expression GG_SY additive_expression
{ $$ = NewBinaryOperator(Cg->tokenLoc, SHR_OP, GG_SY, $1, $3, 1); }
;
/************************/
/* Relational Operators */
/************************/
relational_expression: shift_expression
| relational_expression '<' shift_expression
{ $$ = NewBinaryComparisonOperator(Cg->tokenLoc, LT_OP, '<', $1, $3); }
| relational_expression '>' shift_expression
{ $$ = NewBinaryComparisonOperator(Cg->tokenLoc, GT_OP, '>', $1, $3); }
| relational_expression LE_SY shift_expression
{ $$ = NewBinaryComparisonOperator(Cg->tokenLoc, LE_OP, LE_SY, $1, $3); }
| relational_expression GE_SY shift_expression
{ $$ = NewBinaryComparisonOperator(Cg->tokenLoc, GE_OP, GE_SY, $1, $3); }
;
/**********************/
/* Equality Operators */
/**********************/
equality_expression: relational_expression
| equality_expression EQ_SY relational_expression
{ $$ = NewBinaryComparisonOperator(Cg->tokenLoc, EQ_OP, EQ_SY, $1, $3); }
| equality_expression NE_SY relational_expression
{ $$ = NewBinaryComparisonOperator(Cg->tokenLoc, NE_OP, NE_SY, $1, $3); }
;
/************************/
/* Bitwise AND Operator */
/************************/
AND_expression: equality_expression
| AND_expression '&' equality_expression
{ $$ = NewBinaryOperator(Cg->tokenLoc, AND_OP, '&', $1, $3, 1); }
;
/*********************************/
/* Bitwise Exclusive OR Operator */
/*********************************/
exclusive_OR_expression: AND_expression
| exclusive_OR_expression '^' AND_expression
{ $$ = NewBinaryOperator(Cg->tokenLoc, XOR_OP, '^', $1, $3, 1); }
;
/*********************************/
/* Bitwise Inclusive OR Operator */
/*********************************/
inclusive_OR_expression: exclusive_OR_expression
| inclusive_OR_expression '|' exclusive_OR_expression
{ $$ = NewBinaryOperator(Cg->tokenLoc, OR_OP, '|', $1, $3, 1); }
;
/************************/
/* Logical AND Operator */
/************************/
logical_AND_expression: inclusive_OR_expression
| logical_AND_expression AND_SY inclusive_OR_expression
{ $$ = NewBinaryBooleanOperator(Cg->tokenLoc, BAND_OP, AND_SY, $1, $3); }
;
/***********************/
/* Logical OR Operator */
/***********************/
logical_OR_expression: logical_AND_expression
| logical_OR_expression OR_SY logical_AND_expression
{ $$ = NewBinaryBooleanOperator(Cg->tokenLoc, BOR_OP, OR_SY, $1, $3); }
;
/************************/
/* Conditional Operator */
/************************/
conditional_expression: logical_OR_expression
| conditional_test '?' expression ':' conditional_expression
{ $$ = NewConditionalOperator(Cg->tokenLoc, $1, $3, $5); }
;
conditional_test: logical_OR_expression
{ $$ = CheckBooleanExpr(Cg->tokenLoc, $1, 1); }
;
/***********************/
/* Assignment operator */
/***********************/
expression: conditional_expression
/***
| basic_variable '=' expression
{ $$ = (expr *) NewBinopNode(ASSIGN_OP, $1, $3); }
***/
;
/***********************/
/* Function Definition */
/***********************/
function_definition: function_definition_header block_item_list '}'
{ DefineFunction(Cg->tokenLoc, CurrentScope, $1, $2); PopScope(); }
| function_definition_header '}'
{ DefineFunction(Cg->tokenLoc, CurrentScope, $1, NULL); PopScope(); }
;
function_definition_header: declaration_specifiers declarator '{'
{ $$ = Function_Definition_Header(Cg->tokenLoc, $2); }
;
/*************/
/* Statement */
/*************/
statement: balanced_statement
| dangling_statement
;
balanced_statement: compound_statement
| discard_statement
| expression_statement
| iteration_statement
| if_statement
| return_statement
;
dangling_statement: dangling_if
| dangling_iteration
;
/*********************/
/* Default Statement */
/*********************/
discard_statement: DISCARD_SY ';'
{ $$ = (stmt *) NewDiscardStmt(Cg->tokenLoc, NULL); }
| DISCARD_SY expression ';'
{ $$ = (stmt *) NewDiscardStmt(Cg->tokenLoc, CheckBooleanExpr(Cg->tokenLoc, $2, 1)); }
;
/****************/
/* If Statement */
/****************/
if_statement: if_header balanced_statement ELSE_SY balanced_statement
{ $$ = (stmt *) SetThenElseStmts(Cg->tokenLoc, $1, $2, $4); }
;
dangling_if: if_header statement
{ $$ = (stmt *) SetThenElseStmts(Cg->tokenLoc, $1, $2, NULL); }
| if_header balanced_statement ELSE_SY dangling_statement
{ $$ = (stmt *) SetThenElseStmts(Cg->tokenLoc, $1, $2, $4); }
;
if_header: IF_SY '(' boolean_scalar_expression ')'
{ $$ = (stmt *) NewIfStmt(Cg->tokenLoc, $3, NULL, NULL); ; }
;
/**********************/
/* Compound Statement */
/**********************/
compound_statement: compound_header block_item_list compound_tail
{ $$ = (stmt *) NewBlockStmt(Cg->tokenLoc, $2); }
| compound_header compound_tail
{ $$ = NULL; }
;
compound_header: '{'
{ PushScope(NewScope()); CurrentScope->funindex = NextFunctionIndex; }
;
compound_tail: '}'
{
if (Cg->options.DumpParseTree)
PrintScopeDeclarations();
PopScope();
}
;
block_item_list: block_item
| block_item_list block_item
{ $$ = AddStmt($1, $2); }
;
block_item: declaration
| statement
{ $$ = CheckStmt($1); }
;
/************************/
/* Expression Stetement */
/************************/
expression_statement: expression_statement2 ';'
| ';'
{ $$ = NULL; }
;
expression_statement2: postfix_expression /* basic_variable */ '=' expression
{ $$ = NewSimpleAssignmentStmt(Cg->tokenLoc, $1, $3, 0); }
| expression
{ $$ = (stmt *) NewExprStmt(Cg->tokenLoc, $1); }
| postfix_expression ASSIGNMINUS_SY expression
{ $$ = NewCompoundAssignmentStmt(Cg->tokenLoc, ASSIGNMINUS_OP, $1, $3); }
| postfix_expression ASSIGNMOD_SY expression
{ $$ = NewCompoundAssignmentStmt(Cg->tokenLoc, ASSIGNMOD_OP, $1, $3); }
| postfix_expression ASSIGNPLUS_SY expression
{ $$ = NewCompoundAssignmentStmt(Cg->tokenLoc, ASSIGNPLUS_OP, $1, $3); }
| postfix_expression ASSIGNSLASH_SY expression
{ $$ = NewCompoundAssignmentStmt(Cg->tokenLoc, ASSIGNSLASH_OP, $1, $3); }
| postfix_expression ASSIGNSTAR_SY expression
{ $$ = NewCompoundAssignmentStmt(Cg->tokenLoc, ASSIGNSTAR_OP, $1, $3); }
;
/***********************/
/* Iteration Statement */
/***********************/
iteration_statement: WHILE_SY '(' boolean_scalar_expression ')' balanced_statement
{ $$ = (stmt *) NewWhileStmt(Cg->tokenLoc, WHILE_STMT, $3, $5); }
| DO_SY statement WHILE_SY '(' boolean_scalar_expression ')' ';'
{ $$ = (stmt *) NewWhileStmt(Cg->tokenLoc, DO_STMT, $5, $2); }
| FOR_SY '(' for_expression_opt ';' boolean_expression_opt ';' for_expression_opt ')' balanced_statement
{ $$ = (stmt *) NewForStmt(Cg->tokenLoc, $3, $5, $7, $9); }
;
dangling_iteration: WHILE_SY '(' boolean_scalar_expression ')' dangling_statement
{ $$ = (stmt *) NewWhileStmt(Cg->tokenLoc, WHILE_STMT, $3, $5); }
| FOR_SY '(' for_expression_opt ';' boolean_expression_opt ';' for_expression_opt ')' dangling_statement
{ $$ = (stmt *) NewForStmt(Cg->tokenLoc, $3, $5, $7, $9); }
;
boolean_scalar_expression:
expression
{ $$ = CheckBooleanExpr(Cg->tokenLoc, $1, 0); }
;
for_expression_opt: for_expression
| /* empty */
{ $$ = NULL; }
;
for_expression: expression_statement2
| for_expression ',' expression_statement2
{
stmt *lstmt = $1;
if (lstmt) {
while (lstmt->exprst.next)
lstmt = lstmt->exprst.next;
lstmt->exprst.next = $3;
$$ = $1;
} else {
$$ = $3;
}
}
;
boolean_expression_opt: boolean_scalar_expression
| /* empty */
{ $$ = NULL; }
;
/*******************/
/*Return Statement */
/*******************/
return_statement: RETURN_SY expression ';'
{ $$ = (stmt *) NewReturnStmt(Cg->tokenLoc, CurrentScope, $2); }
| RETURN_SY ';'
{ $$ = (stmt *) NewReturnStmt(Cg->tokenLoc, CurrentScope, NULL); }
;
/*********/
/* Misc. */
/*********/
member_identifier: identifier
;
scope_identifier: identifier
;
semantics_identifier: identifier
;
type_identifier: TYPEIDENT_SY
;
variable_identifier: identifier
;
identifier: IDENT_SY
;
constant: INTCONST_SY /* Temporary! */
{ $$ = (expr *) NewIConstNode(ICONST_OP, $1, TYPE_BASE_CINT); }
| CFLOATCONST_SY /* Temporary! */
{ int base = Cg->theHAL->GetFloatSuffixBase(Cg->tokenLoc, ' ');
$$ = (expr *) NewFConstNode(FCONST_OP, $1, base);
}
| FLOATCONST_SY /* Temporary! */
{ int base = Cg->theHAL->GetFloatSuffixBase(Cg->tokenLoc, 'f');
$$ = (expr *) NewFConstNode(FCONST_OP, $1, base);
}
| FLOATHCONST_SY /* Temporary! */
{ int base = Cg->theHAL->GetFloatSuffixBase(Cg->tokenLoc, 'h');
$$ = (expr *) NewFConstNode(FCONST_OP, $1, base);
}
| FLOATXCONST_SY /* Temporary! */
{int base = Cg->theHAL->GetFloatSuffixBase(Cg->tokenLoc, 'x');
$$ = (expr *) NewFConstNode(FCONST_OP, $1, base);
}
;
/***
integer_constant: INTCONST_SY
{ $$ = $1; }
;
***/
/***
constant_expression: expression
;
***/
%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -