#include<stdio.h>
#include<string.h>
char prog[80],token[10];
char ch;
int count=0, ni=0;
int syn,p,j=0,n,m,sum=0; //*p是缓冲区prog的指针,m是token的指针
char *rwtab[10]={"begin","if","then","while","do","end","else","char","int"};
void main()
{
void scaner();
p=0;
printf("\n please input string:");
do
{
scanf("%c",&ch);
prog[p++]=ch;
}while (ch!='\n');
prog[p]='\0';
p=0;
printf("\n 各种单词符号对应的种别码 : (种别码,单词符号)\n\n");
do
{
scaner();
switch(syn)
{
case 12:printf(" (%d,%d) ",syn,sum ); break;
case -1: printf(" (错误符号,%s) ",token);break;
default:
printf(" (%d,%s) ",syn,token);
}
ni++;
if(ni%2==0)printf("\n");
}while(syn!=0);
printf("\n");
}
void scaner()
{
int j=0;
for(n=0;n<8;n++)
token[n]=NULL;
ch=prog[p++];
while(ch==' ')
ch=prog[p++];
if(ch>='a'&&ch<='z')
{
count=0;
while((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9'))
{
token[j++]=ch;
ch=prog[p++];
}
token[j++]='\0';
ch=prog[--p];
syn=10;
for(n=0;n<9;n++)
if(strcmp(token,rwtab[n])==0)
{
count=1;
switch(n)
{
case 0: syn=1;break;
case 1: syn=2;break;
case 2: syn=3;break;
case 3: syn=4;break;
case 4: syn=5;break;
case 5: syn=6;break;
case 6: syn=7;break;
case 7: syn=8;break;
case 8: syn=9;break;
case 9:syn=10;break;
}
}
}
else
if(ch>='0'&&ch<='9')
{
count=1;
sum=0;
while((ch-'0')>=0&&(ch-'0')<=9)
{
sum=sum*10+ch-'0';
ch=prog[p++];
}
ch=prog[--p];
syn=11;
}
else
{
count=1;
switch(ch)
{
case '<':m=0;token[m++]=ch;ch=prog[p++];
if(ch=='>')
{
syn=21;
token[m++]=ch;
}
else if(ch=='=')
{
syn=22;
token[m++]=ch;
}
else
{
syn=20;ch=prog[--p];}
break;
case '>': m=0;token[m++]=ch;ch=prog[p++];
if(ch=='=')
{
syn=24;
token[m++]=ch;
}
else
{
syn=23;
ch=prog[--p];}
break;
case ':': m=0;token[m++]=ch;ch=prog[p++];
if(ch=='=')
{
syn=18;
token[m++]=ch;
}
else
{
syn=17;
ch=prog[--p];}
break;
case'+': syn=13;token[0]=ch;break;
case'-': syn=14;token[0]=ch;break;
case'*': syn=15;token[0]=ch;break;
case'/': syn=16;token[0]=ch;break;
case'=': syn=25;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'\n': syn=0;strcpy(token,"\\n");break;
default:syn=-1;token[0]=ch;
}
}
}
int Irparser()
{ int kk=0;
if(syn=1)
{ scaner();
yucu();
if(syn=6)
{ scaner();
if(syn=0 && (kk==0))
输出("success");
}
else{if(kk!=1)输出'缺end'错误;kk=1;}
else{输出'begin'错误;kk=1;}
}
int yucu()
{ statement();
whlie(syn=26)
{ scaner();
statement();
}
return;
}
int statement()
{ if(syn=10)
{ scaner();
if(syn=18)
{ scaner();
expression();
}
else{printf;kk=1;}
}
else{printf(语句错误;kk=1);}
}
int expression()
{ term();
whlie(syn=13or14)
{ scaner();
term();
}
return;
}
int term()
{ factor;
whlie(syn=15 or 16)
{ scaner();
factor();
}
return;
}
int factor()
{ if(syn=10 or 11)
scaner();
else if(syn=27)
{ scaner();
expression();
if(syn=28)
scaner();
else{printf(')'错误;kk=1);}
}
else{printf (kk=1);}
}
#include<string.h>
char prog[80],token[10];
char ch;
int count=0, ni=0;
int syn,p,j=0,n,m,sum=0; //*p是缓冲区prog的指针,m是token的指针
char *rwtab[10]={"begin","if","then","while","do","end","else","char","int"};
void main()
{
void scaner();
p=0;
printf("\n please input string:");
do
{
scanf("%c",&ch);
prog[p++]=ch;
}while (ch!='\n');
prog[p]='\0';
p=0;
printf("\n 各种单词符号对应的种别码 : (种别码,单词符号)\n\n");
do
{
scaner();
switch(syn)
{
case 12:printf(" (%d,%d) ",syn,sum ); break;
case -1: printf(" (错误符号,%s) ",token);break;
default:
printf(" (%d,%s) ",syn,token);
}
ni++;
if(ni%2==0)printf("\n");
}while(syn!=0);
printf("\n");
}
void scaner()
{
int j=0;
for(n=0;n<8;n++)
token[n]=NULL;
ch=prog[p++];
while(ch==' ')
ch=prog[p++];
if(ch>='a'&&ch<='z')
{
count=0;
while((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9'))
{
token[j++]=ch;
ch=prog[p++];
}
token[j++]='\0';
ch=prog[--p];
syn=10;
for(n=0;n<9;n++)
if(strcmp(token,rwtab[n])==0)
{
count=1;
switch(n)
{
case 0: syn=1;break;
case 1: syn=2;break;
case 2: syn=3;break;
case 3: syn=4;break;
case 4: syn=5;break;
case 5: syn=6;break;
case 6: syn=7;break;
case 7: syn=8;break;
case 8: syn=9;break;
case 9:syn=10;break;
}
}
}
else
if(ch>='0'&&ch<='9')
{
count=1;
sum=0;
while((ch-'0')>=0&&(ch-'0')<=9)
{
sum=sum*10+ch-'0';
ch=prog[p++];
}
ch=prog[--p];
syn=11;
}
else
{
count=1;
switch(ch)
{
case '<':m=0;token[m++]=ch;ch=prog[p++];
if(ch=='>')
{
syn=21;
token[m++]=ch;
}
else if(ch=='=')
{
syn=22;
token[m++]=ch;
}
else
{
syn=20;ch=prog[--p];}
break;
case '>': m=0;token[m++]=ch;ch=prog[p++];
if(ch=='=')
{
syn=24;
token[m++]=ch;
}
else
{
syn=23;
ch=prog[--p];}
break;
case ':': m=0;token[m++]=ch;ch=prog[p++];
if(ch=='=')
{
syn=18;
token[m++]=ch;
}
else
{
syn=17;
ch=prog[--p];}
break;
case'+': syn=13;token[0]=ch;break;
case'-': syn=14;token[0]=ch;break;
case'*': syn=15;token[0]=ch;break;
case'/': syn=16;token[0]=ch;break;
case'=': syn=25;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'\n': syn=0;strcpy(token,"\\n");break;
default:syn=-1;token[0]=ch;
}
}
}
int Irparser()
{ int kk=0;
if(syn=1)
{ scaner();
yucu();
if(syn=6)
{ scaner();
if(syn=0 && (kk==0))
输出("success");
}
else{if(kk!=1)输出'缺end'错误;kk=1;}
else{输出'begin'错误;kk=1;}
}
int yucu()
{ statement();
whlie(syn=26)
{ scaner();
statement();
}
return;
}
int statement()
{ if(syn=10)
{ scaner();
if(syn=18)
{ scaner();
expression();
}
else{printf;kk=1;}
}
else{printf(语句错误;kk=1);}
}
int expression()
{ term();
whlie(syn=13or14)
{ scaner();
term();
}
return;
}
int term()
{ factor;
whlie(syn=15 or 16)
{ scaner();
factor();
}
return;
}
int factor()
{ if(syn=10 or 11)
scaner();
else if(syn=27)
{ scaner();
expression();
if(syn=28)
scaner();
else{printf(')'错误;kk=1);}
}
else{printf (kk=1);}
}