Search code examples
c++parsingc++11string-parsing

Error parsing an expression in C++


Can someone please point out the reason for the segmentation fault in my code. I am trying to convert an arithmetic expression with precedence decided by '()' into postfix form and then solve the expression.

#include<iostream>
#include<string>
#include<stack>

using namespace std;

string post(string exp)
{
   cout<<"reached post";
   stack<char> s2;
   string new_exp="";
   int length=exp.length();
   for(int i=0; i<length; i++)
   {
       if(exp[i]=='(')
       {
          s2.push(exp[i]);
       }
       else if(exp[i]=='+'|| exp[i]=='-' || exp[i]=='*' || exp[i]=='/')
       {
          new_exp+=' ';
          s2.push(exp[i]);
       }
       else if(exp[i]>='0'&& exp[i]<='9')
       {
          new_exp+=exp[i];
       }
       else if(exp[i]==')')
       {
          new_exp+=' ';
          new_exp+=s2.top();
          s2.pop();
          s2.pop();
       }
    }
    if(!s2.empty())
    {
       while(!s2.empty())
       {
         new_exp+=' ';
         new_exp+=s2.top();
         s2.pop();
       }
    }

  return(new_exp);
}

int operation(char op, char op1, char op2)
{
  if(op == '+') return(op1+op2);
  else if(op=='-') return(op1-op2);
  else if(op=='*') return(op1*op2);
  else if(op=='/') return(op1/op2);
}

int solve(string expression)
{
  cout<<"\nreached solve";
  string postfix=post(expression);
  stack<char> s;
  int res;
  int length=postfix.length();
  for(int i=0; i<length; i++)
  {
      if(postfix[i]==' ')
      {
          continue;
      }
      else if(postfix[i]=='+'|| postfix[i]=='-' || postfix[i]=='*' || postfix[i]=='/')
      {
          char op2=s.top();
          s.pop();
          char op1=s.top();
          s.pop();
          res=operation(postfix[i],op1,op2);
          s.push(res);
      }
     else if(postfix[i]>='0' && postfix[i]<=9)
      {
          int operand=0;
          while(postfix[i]!=' ' || i!=length)
          {
              operand=(operand*10)+(postfix[i]-'0');
              i++;
          }
          i--;
          s.push(operand);
      }
    }
  return(res);
}

int main(void)
{
  string exp;
  int result;
  cout<<"Enter expression: ";
  getline(cin,exp);
  result=solve(exp);
  cout<<"\nResult= "<<result;
  return 0;
}

I get the following error message:

cav@cav-VirtualBox:~/src/cpp$ ./infix_postfix
Enter expression: 10+3

Segmentation fault (core dumped)

Solution

  • I can see at least two mistakes. First,

    else if(postfix[i]>='0' && postfix[i]<=9)
    

    You need to compare character '9', not integer 9 as you have a string here. It should be:

    else if(postfix[i]>='0' && postfix[i]<='9')
                                           ^ ^
    

    Second problem is here:

    while(postfix[i]!=' ' || i!=length)
    

    You meant and operation && here, not or ||. When it's a || it is basically true for all characters except i runs out of the length. Also i != length should be tested before postfix[i] != ' ' since when i == length postfix[i] will be out of bound. This line should be:

    while(i!=length && postfix[i]!=' ')
    

    Due to these two mistakes you are not pushing values to your stack correctly, getting erronous values at different times which is leading to segmentation fault.