eval_parser (Part 1).
By SalimMeghani
- 259 reads
// Reverse Polish Parser.
double eval_parser(char* newyk, int *error)
{
double answer=0;
switc=newyk[x];
if ((switc=='%') && (x==0)) // In the beginning we push the % terminator
// onto the texas stack
{
previous=switc;
pushtexas(switc);
}
if ((previous=='%') && (((switc>='0') && (switc<='9')) || switc=='.'))
{
calif[c]='P';
c++;
calif[c]=',';
c++;
}
else if (switc=='-')
{
calif[c]='M';
c++;
calif[c]=',';
c++;
x++;
previous='-';
switc=newyk[x];
}
else if (switc=='+')
{
calif[c]='P';
c++;
calif[c]=',';
c++;
x++;
previous='+';
switc=newyk[x];
}
do
{
if ((previous=='+' || previous=='-' || previous=='/' || previous=='*' || previous=='(') && (switc=='+' || switc=='-'))
{
if (switc=='+')
{
previous='+';
x++;
switc=newyk[x];
}
else
{
if (previous!='-')
{
putcalif('M');
calif[c]=',';
c++;
if (switc=='-') previous='-';
}
else
{
putcalif('M');
calif[c]=',';
c++;
}
}
goto done;
}
else
if (switc=='(')
{
calif[c]=switc;
c++;
}
if ((switc=='E') || (switc=='e'))
{
putcalif(switc);
if (switc=='-')
putcalif('-');
else
putcalif(switc);
previous=switc;
notrue=1;
}
while (((switc>='0') && (switc<='9')) || switc=='.')
{
previous=switc;
putcalif(switc);
notrue=1;
}
if (notrue==1) goto done;
if ((switc=='%' || switc=='+' || switc=='-' || switc==')') && (texas[s-1]=='+' || texas[s-1]=='*' || texas[s-1]=='-' || texas[s-1]=='/'))
{
poptexas(s-1);
goto done;
}
if ((switc=='*' || switc=='/') && (texas[s-1]=='*' || texas[s-1]=='/'))
{
poptexas(s-1);
goto done;
}
if (switc=='%' && texas[s-1]=='(')
{
fperror(error);
break;
goto done;
}
if ((switc=='+' || switc=='-' || switc=='*' || switc=='/' || switc=='(') && (texas[s-1]=='(' || texas[s-1]=='%'))
{
previous=switc;
pushtexas(switc);
goto done;
}
if ((switc=='*' || switc=='/') && (texas[s-1]=='+' || texas[s-1]=='-'))
{
previous=switc;
pushtexas(switc);
goto done;
}
if ((switc=='(') && (texas[s-1]=='+' || texas[s-1]=='*' || texas[s-1]=='-' || texas[s-1]=='/' || texas[s-1]=='('))
{
previous=switc;
pushtexas(switc);
goto done;
}
if (switc==')' && texas[s-1]=='%')
{
fperror(error);
break;
goto done;
}
if (switc==')' && texas[s-1]=='(')
{
calif[c]=switc;
c++;
calif[c]=',';
c++;
fpdel();
previous=')';
goto done;
}
done:;
if (notrue==1)
{
calif[c]=',';
c++;
notrue=0;
}
if (switc=='%' && texas[s-1]=='%')
- Log in to post comments