include <stdio.h>
#include <string.h>
char prog[80],token[8],ch;
int syn,p,m,n,sum;
char *rwtab[6]={"begin","if","then","while","do","end"};
void scaner(void);
main()
{
p=0;
printf("\n Please input the source code:\n");
do{
scanf("%c",&ch);
prog[p++]=ch;
}while(ch!='#');
p=0;
do{
scaner();
switch(syn)
{
case 11:
printf("( %d,%d )\n",syn,sum);
break;
case -1:
printf("you have input a wrong string\n");
return 0;
break;
default:
printf("( %d,%s )\n",syn,token);
break;
}
}while(syn!=0);
printf(" Over.\n");
}
void scaner(void)
{
sum=0;
for(m=0;m<8;m++)
token[m++]= NULL;
ch=prog[p++];
m=0;
while((ch==' ')||(ch=='\n'))
ch=prog[p++];
if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
{
while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))
{
token[m++]=ch;
ch=prog[p++];
}
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'))
{
while((ch>='0')&&(ch<='9'))
{
sum=sum*10+ch-'0';
ch=prog[p++];
}
p--;
syn=11;
}
else
{
switch(ch)
{
case '<':
token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=22;
token[m++]=ch;
}
else
{
syn=20;
p--;
}
break;
case '>':
token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=24;
token[m++]=ch;
}
else
{
syn=23;
p--;
}
break;
case '+':
token[m++]=ch;
ch=prog[p++];
if(ch=='+')
{
syn=17;
token[m++]=ch;
}
else
{
syn=13;
p--;
}
break;
case '-':
token[m++]=ch;
ch=prog[p++];
if(ch=='-')
{
syn=29;
token[m++]=ch;
}
else
{
syn=14;
p--;
}
break;
case '=':
token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=25;
token[m++]=ch;
}
else
{
syn=18;
p--;
}
break;
case '*': syn=15;token[m++]=ch; break;
case '/': syn=16;token[m++]=ch; break;
case '(': syn=27;token[m++]=ch; break;
case ')': syn=28;token[m++]=ch; break;
case ';': syn=26;token[m++]=ch; break;
case '#': syn=0;token[m++]=ch; break;
case ':': syn=17;token[m++]=ch; break;
default: syn=-1; break;
}
}
token[m++]='\0';
}
1022-词法分析程序
推荐阅读
- firefox浏览器不能使用window.close的解决方案
- C#给其他程序发消息
- Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given,
- ERROR 2003 (HY000): Can't connect to MySQL server on "192.168.xxx.xxx" (111)
- js跨越请求的2中实现 JSONP /后端接口设置运行跨越header
- myql root用户远程访问
- Server sent passive reply with unroutable address. Using server address instead
- iPhone 6 屏幕揭秘
- 响应式布局的钥匙之 图片液态化
- 理解Angular中的$apply()以及$digest()