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

📄 parse.y.bak

📁 完成一个简化的C语言编译程序
💻 BAK
📖 第 1 页 / 共 2 页
字号:

while_stmt         : WHILE LPAREN expression RPAREN statement
                   {
                      $$=newStmtNode(WhileK);
                      $$->child[0]=$3;
                      $$->child[1]=$5;
                   }
                   ;

return_stmt        : RETURN SEMI
                   {
                      $$=newStmtNode(ReturnK);
                   }
                   | RETURN expression SEMI
                   {
                      $$=newStmtNode(ReturnK);
                      $$->child[0]=$2;
                      
                   } 
                   ;

assign_stmt        : var ASSIGN expression   SEMI
                   {
                      $$=newStmtNode(AssignK);
                      $$->child[0]=$1;
                      $$->child[1]=$3;
                   }
                   ; 

break_stmt         : BREAK SEMI
                   {
                      $$=newStmtNode(BreakK);
                   }   
                   ;
                   
var                : id
                   {
                      $$=newExpNode(IdK);
                      $$->attr.name=$1->attr.name;
                   }
                   |  id LBRACKET expression RBRACKET
                   {
                      $$=newExpNode(ArrayK);
                      $$->attr.name=$1->attr.name;
                      $$->child[0]=$3;
                   } 
                   ;

expression         : or_expression
                   {
                      $$=$1;
                   }                                                                          
                   ;
                   
or_expression      : or_expression OR and_expression
                   {
                      $$ = newExpNode(OpK);
                      $$->child[0] = $1;
                      $$->child[1] = $3;
                      $$->attr.op = OR;
                   }
                   | and_expression
                   {
                          $$=$1;
                   } 
                   ;

and_expression     : and_expression AND simple_expression
                   {
                        $$=newExpNode(OpK);
                        $$->child[0]=$1;
                        $$->child[1]=$3;
                        $$->attr.op=AND;
                   }
                   |simple_expression
                   {
                           $$=$1;
                   } 
                   ;

simple_expression  : additive_expression GE additive_expression
                   {
                        $$=newExpNode(OpK);
                        $$->child[0]=$1;
                        $$->child[1]=$3;
                        $$->attr.op=GE;  
                   }
                   | additive_expression GT additive_expression
                   {
                        $$=newExpNode(OpK);
                        $$->child[0]=$1;
                        $$->child[1]=$3;
                        $$->attr.op=GT;  
                   }
                   | additive_expression LE additive_expression
                   {
                        $$=newExpNode(OpK);
                        $$->child[0]=$1;
                        $$->child[1]=$3;
                        $$->attr.op=LE;  
                   }
                    | additive_expression LT additive_expression
                   {
                        $$=newExpNode(OpK);
                        $$->child[0]=$1;
                        $$->child[1]=$3;
                        $$->attr.op=LT;  
                   }
                    | additive_expression EQ additive_expression
                   {
                        $$=newExpNode(OpK);
                        $$->child[0]=$1;
                        $$->child[1]=$3;
                        $$->attr.op=EQ;  
                   }
                    | additive_expression NE additive_expression
                   {
                        $$=newExpNode(OpK);
                        $$->child[0]=$1;
                        $$->child[1]=$3;
                        $$->attr.op=NE;  
                   }
                   | additive_expression
                   {
                        $$=$1;
                   }                   
                   ;

additive_expression :additive_expression PLUS term
                    {
                        $$=newExpNode(OpK);
                        $$->child[0]=$1;
                        $$->child[1]=$3;
                        $$->attr.op=PLUS;
                    } 
                    |additive_expression MINUS term
                    {
                        $$=newExpNode(OpK);
                        $$->child[0]=$1;
                        $$->child[1]=$3;
                        $$->attr.op=MINUS;
                    }                     
                    | term
                    {
                        $$=$1;
                    }
                    ;
                    
                    
term                : term TIMES factor
                      {
                        $$ = newExpNode(OpK);
                        $$->child[0] = $1;
                        $$->child[1] = $3;
                        $$->attr.op = TIMES;
                      }
                    | term OVER factor
                      {
                        $$ = newExpNode(OpK);
                        $$->child[0] = $1;
                        $$->child[1] = $3;
                        $$->attr.op = OVER;
                      }
                    | factor
                      { $$ = $1; }
                    ; 
                    
factor              : LPAREN expression RPAREN 
                      { $$ = $2; }
                    | var
                      { $$ = $1; }
                    | call
                      { $$ = $1; }
                    | NUM
                      { 
                        $$ = newExpNode(ConstK);
                        $$->attr.vali = atoi(tokenString); 
                        $$->type = Integer;
                      }
                    | FLOATNUM
                      {
                        $$ = newExpNode(ConstK);
                        $$->attr.valf = (float)atof(tokenString); 
                        $$->type = Float;
                      }
                    | CONSTCHAR
                    	{
                    	$$ = newExpNode(ConstK);
                        $$->attr.valc = tokenString[0];
                        $$->type = Char;
                    	}
                    | NOT factor
                      { 
                        $$ = newExpNode(OpK);
                        $$->attr.op=NOT;
                        $$->child[0] = $2;
                      }
                    ;   
                    
call                : id LPAREN args RPAREN
                      {
                        $$ = newExpNode(CallK);
                        $$->attr.name=$1->attr.name;
                        $$->child[0] = $3;
                      }
                    | id LPAREN RPAREN
                      {
                        $$ = newExpNode(CallK);
                        $$->attr.name=$1->attr.name;
                        $$->child[0]=0;
                      }
                    ;
                         
args                : arg_list 
                      {
                        $$=$1;
                      }
                      ;

arg_list            : expression COMMA arg_list
                      {
                        $$ = $1;
                        $$->sibling = $3;
                      }
                    | expression
                      {
                        $$ = $1;
                      }
                    ;
%%                  
            
/*int yyerror(char* message)
{
   fprintf(listing,"Syntax error at line %d: %s \n",lineno,message);
   printToken(yychar,tokenString);
   Error=TRUE;
   return 0;
} */

#ifdef YYPROTOTYPE
int YYCDECL yygettoken(void)
#else
int YYCDECL yygettoken()
#endif
{
	return yylex();
}

/*int yygettoken(void)
{
  return getToken();
}
*/
static int yylex(void)
{ return getToken(); }

TreeNode* parse(void)
{
    yyparse();
    return savedTree;
}

⌨️ 快捷键说明

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