新手小白 求教大神c语言写词法分析的代码的功能的实现
#include<stdio.h>#include<string.h>
char prog[80],token[8];
char ch;
int syn,p,m,n,sum,e;
char * rwtab[6]={"function","if","then","while","do","endfunc"};
scaner();
main()
{
p=0;
printf("\n please input string:\n");
do{
scanf("%c",&ch);
prog[++p]=ch;
}
while(ch!='#');
p=0;
do
{
scaner();
switch(syn)
{
case 11:printf("\n(%d,%d)",syn,sum);
break;
case-1:printf("\n erron");
break;
default:printf("\n(%d,%s)",syn,token);
}
}
while(syn!=0);
return 0;
}
scaner()
{
for(n=0;n<8;n++)token[n]=NULL;
ch=prog[++p];
while(ch!='#')ch=prog[++p];
if(ch>='a'&&ch<='z')
{
m=0;
while(ch>='a'&&ch<='z'||ch>='0'&&ch<='9')
{
token[m++]=ch;
ch=prog[++p];
}
token[m]='\0';
ch=prog[--p];
syn=10;
for(n=0;n<6;n++)
if(strcmp(token,rwtab[n])==0)
{
syn=n+1;
break;
}
}
else
if(ch>='0'&&ch<='9')
{
sum=0;
while(ch>='0'&&ch<='9')
{
sum=sum*10+ch-'0';
ch=prog[++p];
}
ch=prog[--p];
syn=11;
}
if(ch=='.') //有小数点
{
e=-1;
ch=prog[p++];
if(ch>='0'&&ch<='9')
{
while(ch>='0'&&ch<='9')
{
sum=sum+(ch-'0')*pow(10,e--);
ch=prog[p++];
}
}
}
else
switch(ch)
{
case'<':m=0;
token[m++]=ch;
ch=prog[++p];
if(ch=='=')
{
syn=22;
token[m+1]=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=25;
token[m++]=ch;
}
else
{
syn=18;
ch=prog[--p];
}
break;
case'!':m=0;token[m++]=ch;
ch=prog[++p];
if(ch=='=')
{
syn=22;token[m++]=ch;
}
else
syn=-1;
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=26;token[0]=ch;break;
case'(':syn=27;token[0]=ch;break;
case')':syn=28;token[0]=ch;break;
case'#':syn=0;token[0]=ch;break;
default:syn=-1;
}
return 0;
}
————————————————————————————————————————
这个代码我们老师要求改成能够识别小数和实数的科学计数部分 大于1位的整数不能以0开头 且诸如1.或.1的形式是错误的 请问我该怎么改这个代码啊 求教大神讲解一下 万分感谢