⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paofan.c

📁 词法分析 从字符串表示的源程序中识别单词符号
💻 C
字号:
#include<stdio.h>
#include<string.h>
char prog[80],token[8];
char ch;
int syn,p,m,n,sum;
char *rwtab[6]={"begin","if","then","while","do","end"};
scaner()
{for(n=0;n<8;n++) 
 token[n]=0;
 ch=prog[p++];
 while(ch==' '||ch=='\n')
	ch=prog[p++];
 if(ch>='a'&&ch<='z')
 {   m=0;
 while((ch>='a'&&ch<='z')||(ch<='9'&&ch>='0')){
	 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)
	{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;
	 }
	}
 }
else if(ch<='9'&&ch>='0')
{ while(ch<='9'&&ch>='0'){
	sum=sum*10+ch-'0';
   ch=prog[p++];
}
ch=prog[--p];
syn=11;
}
else
   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'#':syn=0;token[0]=ch;break;
  default:syn=-1;
}
}




main()
{ p=0;		
  printf("\n please input string:\n");
  do{ch=getchar();
     prog[p++]=ch;
  } while(ch!='#');
p=0;
do
{ scaner();
  switch(syn)
  { case 11:printf("\n (%d,%d)",syn,sum);break;
    case -1:printf("error");break;
    default:printf("\n (%d,%s)",syn,token);
  }
}while(syn!=0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -