ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 인터프리터...32
    일지 2021. 1. 11. 11:37

    C 방식 식 분석 프로그램

     

    MiniCalculator_ver.C.h

    #pragma region 실행 처리
    int varArr[26];
    
    // 계산 처리
    void Operate(TokenKind op)
    {
    	int num2{ Pop() }, num1{ Pop() };
    
    	switch (op)
    	{
    	case TokenKind::Plus:
    		Push(num1 + num2);
    		break;
    	case TokenKind::Minus:
    		Push(num1 - num2);
    		break;
    	case TokenKind::Multiply:
    		Push(num1 * num2);
    		break;
    	case TokenKind::Divide:
    		if (num2 == 0)
    		{
    			std::cout << "0으로 나눌 수 없습니다.\n";
    			exit(1);
    		}
    		Push(num1 / num2);
    		break;
    	case TokenKind::Modular:
    		if (num2 == 0)
    		{
    			std::cout << "0으로 나눌 수 없습니다.\n";
    			exit(1);
    		}
    		Push(num1 % num2);
    		break;
    	}
    }
    
    // And 처리
    void AndExpression()
    {
    	EqualsExpression();
    
    	TokenKind op;
    	while (token.tokenKind == TokenKind::And)
    	{
    		op = token.tokenKind;
    		token = NextToken();
    		EqualsExpression();
    		Operate(op);
    	}
    }
    
    // Equals 처리
    void EqualsExpression()
    {
    	RelationExpression();
    
    	TokenKind op;
    	while (token.tokenKind == TokenKind::Equal
    		|| token.tokenKind == TokenKind::NotEqual)
    	{
    		op = token.tokenKind;
    		token = NextToken();
    		RelationExpression();
    		Operate(op);
    	}
    }
    
    // Relation 처리
    void RelationExpression()
    {
    	AddExpression();
    
    	TokenKind op;
    	while (token.tokenKind == TokenKind::Less
    		|| token.tokenKind == TokenKind::LessEqual
    		|| token.tokenKind == TokenKind::Grater
    		|| token.tokenKind == TokenKind::GraterEqual)
    	{
    		op = token.tokenKind;
    		token = NextToken();
    		AddExpression();
    		Operate(op);
    	}
    }
    
    // +, - 처리
    void AddExpression()
    {
    	MultiplyExpression();
    
    	TokenKind op;
    	while (token.tokenKind == TokenKind::Plus
    		|| token.tokenKind == TokenKind::Minus)
    	{
    		op = token.tokenKind;
    		token = NextToken();
    		MultiplyExpression();
    		Operate(op);
    	}
    }
    
    // *, /, % 처리
    void MultiplyExpression()
    {
    	Factor();
    
    	TokenKind op;
    	while (token.tokenKind == TokenKind::Multiply
    		|| token.tokenKind == TokenKind::Divide
    		|| token.tokenKind == TokenKind::Modular)
    	{
    		op = token.tokenKind;
    		token = NextToken();
    		Factor();
    		Operate(op);
    	}
    }
    
    // 인자 처리
    void Factor()
    {
    	TokenKind op;
    	switch (token.tokenKind)
    	{
    	case TokenKind::Plus:
    	case TokenKind::Minus:
    	case TokenKind::Not:
    		op = token.tokenKind;
    		token = NextToken();
    		Factor();
    
    		if (op == TokenKind::Minus)
    		{
    			Push(-Pop());
    		}
    		else if (op == TokenKind::Not)
    		{
    			Push(!Pop());
    		}
    		break;
    	case TokenKind::VariableName:
    		Push(varArr[token.intValue]);
    		break;
    	case TokenKind::Int:
    		Push(token.intValue);
    		break;
    	case TokenKind::LeftParenthesis:
    		token = NextToken();
    		Expression();
    		CheckToken(TokenKind::RightParenthesis);
    		break;
    	default:
    		std::cout << "에러 발생!\n";
    		exit(1);
    	}
    	token = NextToken();
    }
    
    // 식 처리
    void Expression()
    {
    	TokenKind op;
    
    	AndExpression();
    	while (token.tokenKind == TokenKind::Or)
    	{
    		op = token.tokenKind;
    		token = NextToken();
    		AndExpression();
    		Operate(op);
    	}
    }
    
    // 문 처리
    void Statement()
    {
    	int variableNumber;
    
    	switch (token.tokenKind)
    	{
    	case TokenKind::VariableName:
    		variableNumber = token.intValue;
    		token = NextToken();
    		CheckToken(TokenKind::Assign);
    
    		token = NextToken();
    		Expression();
    		varArr[variableNumber] = Pop();
    		break;
    	case TokenKind::Print:
    		token = NextToken();
    		Expression();
    		CheckToken(TokenKind::EndOfToken);
    
    		std::cout << "   " << Pop() << '\n';
    		return;
    	default:
    		std::cout << "에러 발생!\n";
    		exit(1);
    	}
    
    	CheckToken(TokenKind::EndOfToken);
    }
    #pragma endregion

     

    댓글

Designed by Tistory.