#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <math.h>
char prog[300], token[200] ;
char ch ;
int syn, p, m, n ;
int num1 =1;
double sum=0,sum1=0;
int sum2=0;
int s1;
int k=0;
int w=0;
int np;
char *rwtab[9]={ "main","int","float","double","char","if","else","do","while"};
int number[100];
void e();
void f();
void t();
bool letter(char a)
{
if ((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
{
return true;
}
else
return false;
}
bool digit(char b)
{
if (b>='0'&&b<='9')
{
return true;
}
return false;
}
void scaner()
{ m=0;
for ( n=0; n<200; n++ ) token[n]=NULL;
ch=prog[p++] ;
while (ch==' ' ) ch=prog[p++] ;
while(ch==32) ch=prog[p++];
while (ch==13) ch=prog[p++];
while(ch==10) ch=prog[p++];
while(ch==9) ch=prog[p++];
if ( letter(ch))
{ while (letter(ch)||digit(ch))
{ token[m++]=ch ;
ch=prog[p++] ;
}
token[m++]='\0' ;
p-- ;
syn=10 ;
for (n=0 ; n<9 ; n++ )
if ( strcmp( token, rwtab[n])==0)
{ syn=n+1;
break ;
}
}
else if (digit(ch))
{
while(digit(ch))
{
sum=sum*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
syn=20;
if(ch=='.')
{
token[m++]=ch ;
ch=prog[p++];
int k=1;
if (digit(ch))
{
while(digit(ch))
{
sum1=sum1+pow(0.1,k)*(ch-'0');
k++;
token[m++]=ch ;
ch=prog[p++];
}
}
else
syn=-1;
}
sum=sum+sum1;
if (ch=='e')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else if (ch=='-')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum/pow(10.0,sum2);
syn=20;
}
else
syn=-1;
}
else if (ch=='+')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else
syn=-1; }
else
syn=-1;
}
p--; }
else
switch(ch)
{
case '<':
m=0; token[m++]=ch;
if (ch=='=')
{ syn=35;
token[m++]=ch;
}
else { syn=34;
p--;
}
break;
case '>':
m=0; token[m++]=ch;
ch=prog[p++];
if (ch=='=')
{syn=33;
token[m++]=ch;
}
else
{ syn=32 ;
p--;
}
break;
case '=':
m=0; token[m++]=ch;
ch=prog[p++];
if (ch=='=')
{ syn=36;
token[m++]=ch ;
}
else
{ syn=21 ;
p--;
}
break;
case '!':
m=0;token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=37;
token[m++]=ch;
}
else
{syn=-1;
p--;}
break;
case '+':syn=22; token[0]=ch;
token[m++]=ch ;
ch=prog[p++];
s1=p-3;
if ((p-1)==1)
{
}
else if ((prog[s1]!='+'&&prog[s1]!='-'&&prog[s1]!='*'&&prog[s1]!='/'&&prog[s1]!='=')&&prog[s1]!='(')
{
p--;
break;
}
if (digit(ch))
{
while(digit(ch))
{
sum=sum*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
syn=20;
if(ch=='.')
{
token[m++]=ch ;
ch=prog[p++];
int k=1;
if (digit(ch))
{
while(digit(ch))
{
sum1=sum1+pow(0.1,k)*(ch-'0');
k++;
token[m++]=ch ;
ch=prog[p++];
}
}
else
syn=-1;
}
sum=sum+sum1;
if (ch=='e')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else if (ch=='-')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum/pow(10.0,sum2);
syn=20;
}
else
syn=-1;
}
else if (ch=='+')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else
syn=-1; }
else
syn=-1;
}
p--; }
else
p--;
break;
case '-':syn=23; token[0]=ch;
token[m++]=ch ;
ch=prog[p++];
s1=p-3;
if ((p-1)==1)
{
}
else if ((prog[s1]!='+'&&prog[s1]!='-'&&prog[s1]!='*'&&prog[s1]!='/'&&prog[s1]!='=')&&prog[s1]!='(')
{
p--;
break;
}
if (digit(ch))
{
while(digit(ch))
{
sum=sum*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
syn=20;
if(ch=='.')
{
token[m++]=ch ;
ch=prog[p++];
int k=1;
if (digit(ch))
{
while(digit(ch))
{
sum1=sum1+pow(0.1,k)*(ch-'0');
k++;
token[m++]=ch ;
ch=prog[p++];
}
}
else
syn=-1;
}
sum=(sum+sum1)*(-1);
if (ch=='e')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else if (ch=='-')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum/pow(10.0,sum2);
syn=20;
}
else
syn=-1;
}
else if (ch=='+')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else
syn=-1; }
else
syn=-1;
}
p--; }
else
p--;
break;
case '*':syn=24; token[0]=ch; break;
case '/':ch=prog[p++];
if(ch=='*')
{
do
{
ch=prog[p++];;
} while (ch!='*'&&prog[p]!='/');
ch=prog[p];
p++;
syn=-2;
}
else
{
p--;
p--;
ch=prog[p++];syn=16;token[0]=ch;
}
break;
case '(':syn=26; token[0]=ch; break;
case ')':syn=27; token[0]=ch; break;
case '{':syn=28; token[0]=ch; break;
case '}':syn=29; token[0]=ch; break;
case ',':syn=30; token[0]=ch; break;
case ';':syn=31; token[0]=ch; break;
case '#':syn=0; token[0]=ch; break;
default:
syn= -1;
}
}
int zz()
{
int num=0;
p=0 ;
printf( "\n please input string : \n" ) ;
do {
ch=getchar( );
prog[p++]=ch ;
}while( ch!='#');
p=0;
do{
scaner();
switch(syn)
{
case 20: printf ("(%2d,%g)\n",syn,sum);
number[num++]=syn;
sum=0;
sum1=0;
sum2=0;
break;
case -1: printf("( error,%8s)\n",token);
number[num++]=-1;
sum=0;
sum1=0;
sum2=0;
break;
case -2:break;
default: printf ("(%2d,%8s)\n",syn,token);
number[num++]=syn;
sum=0;
sum1=0;
sum2=0;
break;
}
}while(syn!=0);
getch();
return num;
}
void main()
{
do{
zz();
k=1;
w=0;
np=number[w++];
if(np==20||np==10|np==26)
{
e();}
else
k=0;
if((k==1)&&(np==0))
{
printf("sucess");
}
else
printf("error");
getch();
}while(1);
}
void e( )
{ t();
while(np==22||np==23)
{
np=number[w++];
t();
}
}
void t()
{ f();
while(np==24||np==25)
{
np=number[w++];
f();
}
}
void f()
{ if (np==10||np==20)
np=number[w++];
else if (np==26)
{
np=number[w++];
e();
if (np==27 )
{
np=number[w++];
}
else k=0;
}
elsek=0;
}
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include <math.h>
char prog[300], token[200] ;
char ch ;
int syn, p, m, n ;
int num1 =1;
double sum=0,sum1=0;
int sum2=0;
int s1;
int k=0;
int w=0;
int np;
char *rwtab[9]={ "main","int","float","double","char","if","else","do","while"};
int number[100];
void e();
void f();
void t();
bool letter(char a)
{
if ((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
{
return true;
}
else
return false;
}
bool digit(char b)
{
if (b>='0'&&b<='9')
{
return true;
}
return false;
}
void scaner()
{ m=0;
for ( n=0; n<200; n++ ) token[n]=NULL;
ch=prog[p++] ;
while (ch==' ' ) ch=prog[p++] ;
while(ch==32) ch=prog[p++];
while (ch==13) ch=prog[p++];
while(ch==10) ch=prog[p++];
while(ch==9) ch=prog[p++];
if ( letter(ch))
{ while (letter(ch)||digit(ch))
{ token[m++]=ch ;
ch=prog[p++] ;
}
token[m++]='\0' ;
p-- ;
syn=10 ;
for (n=0 ; n<9 ; n++ )
if ( strcmp( token, rwtab[n])==0)
{ syn=n+1;
break ;
}
}
else if (digit(ch))
{
while(digit(ch))
{
sum=sum*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
syn=20;
if(ch=='.')
{
token[m++]=ch ;
ch=prog[p++];
int k=1;
if (digit(ch))
{
while(digit(ch))
{
sum1=sum1+pow(0.1,k)*(ch-'0');
k++;
token[m++]=ch ;
ch=prog[p++];
}
}
else
syn=-1;
}
sum=sum+sum1;
if (ch=='e')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else if (ch=='-')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum/pow(10.0,sum2);
syn=20;
}
else
syn=-1;
}
else if (ch=='+')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else
syn=-1; }
else
syn=-1;
}
p--; }
else
switch(ch)
{
case '<':
m=0; token[m++]=ch;
if (ch=='=')
{ syn=35;
token[m++]=ch;
}
else { syn=34;
p--;
}
break;
case '>':
m=0; token[m++]=ch;
ch=prog[p++];
if (ch=='=')
{syn=33;
token[m++]=ch;
}
else
{ syn=32 ;
p--;
}
break;
case '=':
m=0; token[m++]=ch;
ch=prog[p++];
if (ch=='=')
{ syn=36;
token[m++]=ch ;
}
else
{ syn=21 ;
p--;
}
break;
case '!':
m=0;token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=37;
token[m++]=ch;
}
else
{syn=-1;
p--;}
break;
case '+':syn=22; token[0]=ch;
token[m++]=ch ;
ch=prog[p++];
s1=p-3;
if ((p-1)==1)
{
}
else if ((prog[s1]!='+'&&prog[s1]!='-'&&prog[s1]!='*'&&prog[s1]!='/'&&prog[s1]!='=')&&prog[s1]!='(')
{
p--;
break;
}
if (digit(ch))
{
while(digit(ch))
{
sum=sum*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
syn=20;
if(ch=='.')
{
token[m++]=ch ;
ch=prog[p++];
int k=1;
if (digit(ch))
{
while(digit(ch))
{
sum1=sum1+pow(0.1,k)*(ch-'0');
k++;
token[m++]=ch ;
ch=prog[p++];
}
}
else
syn=-1;
}
sum=sum+sum1;
if (ch=='e')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else if (ch=='-')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum/pow(10.0,sum2);
syn=20;
}
else
syn=-1;
}
else if (ch=='+')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else
syn=-1; }
else
syn=-1;
}
p--; }
else
p--;
break;
case '-':syn=23; token[0]=ch;
token[m++]=ch ;
ch=prog[p++];
s1=p-3;
if ((p-1)==1)
{
}
else if ((prog[s1]!='+'&&prog[s1]!='-'&&prog[s1]!='*'&&prog[s1]!='/'&&prog[s1]!='=')&&prog[s1]!='(')
{
p--;
break;
}
if (digit(ch))
{
while(digit(ch))
{
sum=sum*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
syn=20;
if(ch=='.')
{
token[m++]=ch ;
ch=prog[p++];
int k=1;
if (digit(ch))
{
while(digit(ch))
{
sum1=sum1+pow(0.1,k)*(ch-'0');
k++;
token[m++]=ch ;
ch=prog[p++];
}
}
else
syn=-1;
}
sum=(sum+sum1)*(-1);
if (ch=='e')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else if (ch=='-')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum/pow(10.0,sum2);
syn=20;
}
else
syn=-1;
}
else if (ch=='+')
{
token[m++]=ch ;
ch=prog[p++];
if (digit(ch))
{
while(digit(ch))
{
sum2=sum2*10+ch-'0';
token[m++]=ch ;
ch=prog[p++];
}
sum=sum*pow(10.0,sum2);
syn=20;
}
else
syn=-1; }
else
syn=-1;
}
p--; }
else
p--;
break;
case '*':syn=24; token[0]=ch; break;
case '/':ch=prog[p++];
if(ch=='*')
{
do
{
ch=prog[p++];;
} while (ch!='*'&&prog[p]!='/');
ch=prog[p];
p++;
syn=-2;
}
else
{
p--;
p--;
ch=prog[p++];syn=16;token[0]=ch;
}
break;
case '(':syn=26; token[0]=ch; break;
case ')':syn=27; token[0]=ch; break;
case '{':syn=28; token[0]=ch; break;
case '}':syn=29; token[0]=ch; break;
case ',':syn=30; token[0]=ch; break;
case ';':syn=31; token[0]=ch; break;
case '#':syn=0; token[0]=ch; break;
default:
syn= -1;
}
}
int zz()
{
int num=0;
p=0 ;
printf( "\n please input string : \n" ) ;
do {
ch=getchar( );
prog[p++]=ch ;
}while( ch!='#');
p=0;
do{
scaner();
switch(syn)
{
case 20: printf ("(%2d,%g)\n",syn,sum);
number[num++]=syn;
sum=0;
sum1=0;
sum2=0;
break;
case -1: printf("( error,%8s)\n",token);
number[num++]=-1;
sum=0;
sum1=0;
sum2=0;
break;
case -2:break;
default: printf ("(%2d,%8s)\n",syn,token);
number[num++]=syn;
sum=0;
sum1=0;
sum2=0;
break;
}
}while(syn!=0);
getch();
return num;
}
void main()
{
do{
zz();
k=1;
w=0;
np=number[w++];
if(np==20||np==10|np==26)
{
e();}
else
k=0;
if((k==1)&&(np==0))
{
printf("sucess");
}
else
printf("error");
getch();
}while(1);
}
void e( )
{ t();
while(np==22||np==23)
{
np=number[w++];
t();
}
}
void t()
{ f();
while(np==24||np==25)
{
np=number[w++];
f();
}
}
void f()
{ if (np==10||np==20)
np=number[w++];
else if (np==26)
{
np=number[w++];
e();
if (np==27 )
{
np=number[w++];
}
else k=0;
}
elsek=0;
}