0916词法分析
#include<stdio.h>
#include <string.h>
#include<iostream.h>
main(){
char a[100];
char ch,bh;
int i,s;
s=0;
printf("请输入字符(输入#结束):");
do
{
scanf("%c",&ch);
a[s++]=ch;
}while(ch!='#');
for(i=0;i<s;i++)
{
bh=a[i];
if(bh>='a'&&ch<='z')
{
if(a[i]=='b'&&a[i+1]=='e'&&a[i+2]=='g'&&a[i+3]=='i'&&a[i+4]=='n')
{
printf("<1,begin >\n");
i=i+4;
continue;
}
else if(a[i]=='i'&&a[i+1]=='f')
{
printf("<2,if >\n");
i=i+1;
continue;
}
else if(a[i]=='t'&&a[i+1]=='h'&&a[i+2]=='e'&&a[i+3]=='n')
{
printf("<3,then >\n");
i=i+3;
continue;
}
else if(a[i]=='w'&&a[i+1]=='h'&&a[i+2]=='i'&&a[i+3]=='l'&&a[i+4]=='e')
{
printf("<4,while >\n");
i=i+4;
continue;
}
else if(a[i]=='d'&&a[i+1]=='o')
{
printf("<5,do >\n");
i=i+1;
continue;
}
else if(a[i]=='e'&&a[i+1]=='n'&&a[i+2]=='d')
{
printf("<6,end >\n");
i=i+2;
continue;
}
else if(a[i]=='l'&&a[i+1]=='('&&a[i+2]=='l'&&a[i+3]=='|'&&a[i+4]=='d'&&a[i+5]==')'&&a[i+6]=='*')
{
printf("<10,l(l|d)* >\n");
i=i+6;
continue;
}
else if(a[i]=='d'&&a[i+1]=='d'&&a[i+2]=='*')
{
printf("<11,dd* >\n");
i=i+2;
continue;
}
}
if(bh==':')
if(a[i+1]=='='){
printf("<18,:= >\n");
i=i+1;
continue;}
if(bh=='<')
if(a[i+1]=='='){
printf("<21,<= >\n");
i=i+1;
continue;}
if(bh=='<')
if(a[i+1]=='>'){
printf("<22,<> >\n");
i=i+1;
continue;}
if(bh=='>')
if(a[i+1]=='='){
printf("<24,>= >\n");
i=i+1;
continue;}
switch(bh)
{
case '+':
printf("<13,%c >\n",bh);
break;
case '-':
printf("<14,%c >\n",bh);
break;
case '*':
printf("<15,%c >\n",bh);
break;
case '/':
printf("<16,%c >\n",bh);
break;
case ':':
printf("<17,%c >\n",bh);
break;
case '(':
printf("<27,%c >\n",bh);
break;
case ')':
printf("<28,%c >\n",bh);
break;
case '#':
printf("<0,%c >\n",bh);
break;
case '>':
printf("<23,%c >\n",bh);
break;
case '<':
printf("<20,%c >\n",bh);
break;
case ';':
printf("<26,%c >\n",bh);
break;
}
}
}