0% found this document useful (0 votes)
29 views4 pages

Expression Evaluation (Compiler Construction)

This is a program to analyze the syntax of expression and to evaluate it.

Uploaded by

Aqua Geniuss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Expression Evaluation (Compiler Construction)

This is a program to analyze the syntax of expression and to evaluate it.

Uploaded by

Aqua Geniuss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//--------------------------------------------------------------------------#include <iostream.

h>
#include <conio.h>
#pragma hdrstop
struct token
{
bool isOperand;
int Operand;
char Operator;
};
token L[100];
int TokenCount = 0;
//--------------------------------------------------------------------------void tokenize(char *S)
{
unsigned int i=0;
while (i<strlen(S))
{
if(S[i]=='+' || S[i]=='-' || S[i]=='*' || S[i]=='/' || S[i]=='(' || S[i]
==')')
{
L[TokenCount].isOperand=false;
L[TokenCount].Operator = S[i];
TokenCount++;
i++;
}
else if(S[i]>='0' && S[i]<='9')
{
int N=0;
while (i<strlen(S) && S[i]>='0' && S[i]<='9')
{
N = N*10 + S[i] - 48;
i++;
}
L[TokenCount].isOperand=true;
L[TokenCount].Operand = N;
TokenCount++;
}
else
{
cout << "invalid symbol '"<<S[i]<<"' in string.";
getch();
exit(0);
}
}
}
bool
bool
bool
bool
bool

E(int A, int
Edash(int A,
T(int A, int
Tdash(int A,
F(int A, int

&Z);
int &Z);
&Z);
int &Z);
&Z);

//--------------------------------------------------------------------------bool match_operator(char op, int A, int &Z)


{
if (L[A].isOperand==false && L[A].Operator==op)
{
Z=A+1;
return true;
}
else
{
Z=A;
return false;
}
}
//--------------------------------------------------------------------------bool E(int A, int &Z)
{
int B;
if ( T(A,B) && Edash(B,Z) )
{
return true;
}
else
{
Z=A;
return false;
}
}
//--------------------------------------------------------------------------bool Edash(int A, int &Z)
{
int B,C;
if ( match_operator('+',A,B) && T(B,C) && Edash(C,Z) )
{
return true;
}
else if ( match_operator('-',A,B) && T(B,C) && Edash(C,Z) )
{
return true;
}
else
{
Z=A;
return true;
}
}
//--------------------------------------------------------------------------bool T(int A, int &Z)
{
int B;
if ( F(A,B) && Tdash(B,Z) )
{
return true;
}

else
{
Z=A;
return false;
}
}
//--------------------------------------------------------------------------bool Tdash(int A, int &Z)
{
int B,C;
if ( match_operator('*',A,B) && F(B,C) && Tdash(C,Z) )
{
return true;
}
else if ( match_operator('/',A,B) && F(B,C) && Tdash(C,Z) )
{
return true;
}
else
{
Z=A;
return true;
}
}
//--------------------------------------------------------------------------bool F(int A, int &Z)
{
int B,C;
if (L[A].isOperand == true)
{
Z=A+1;
return true;
}
else if( match_operator('(',A,B) && E(B,C) && match_operator(')',C,Z))
{
return true;
}
else
{
Z=A;
return false;
}
}
//--------------------------------------------------------------------------#pragma argsused
int main(int argc, char* argv[])
{
char S[256];
cin.getline(S, 255);
tokenize(S);
cout << "\n======================Token List:" << endl;

int i;
for (i=0; i<TokenCount; i++)
{
cout << "L[" << i << "]=\t";
if (L[i].isOperand==true)
{
cout << L[i].Operand << endl;
}
else
{
cout << L[i].Operator << endl;
}
}
cout << "\n=================================" << endl;
int Z;
if (E(0,Z))
{
cout << "matched upto "<< Z;
}
else
{
cout << "not matched";
}

getch();
return 0;
}
//---------------------------------------------------------------------------

You might also like