词法分析

#include<stdio.h>
#include<string.h>
void main()
{ 
	int  i=0,j,k=0,state=1,f=0,linenum=1;
    char a[11][10]={"const","var","call","begin","if","while","do","odd","end","then","procedure"}; 
    char b,d[40]={"\0"};
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	b=getchar();
	while(b!=EOF)/*判断所输入字符是否为结束符*/
    {
      if(b==' '||b=='\n'||b=='\t')/*滤过空格、换行等分隔符号*/
	  {
		if(b='\n') linenum++;
		b=getchar();
	  }
	else if((b>='a'&&b<='z')||(b>='A'&&b<='Z'))/*识别标识符以及保留字*/
	{
		d[i++]=b;
		b=getchar(); 
		while((b>='a'&&b<='z')||(b>='A'&&b<='Z')||(b>='0'&&b<='9'))
		{
			d[i++]=b;
			b=getchar(); 
		}
		for(j=0;j<11;j++)/*查询保留字表确定该单词是否是保留字*/ 
		{
			if(strcmp(d,a[j])==0)
			{
				printf("1,%s\n",d);
				k=1;
				break; 

			}

		}
		if(k==0)/*在保留字表中没有查到该单词,是标识符*/ 
		printf("3,%s\n",d);
		for(j=0;j<=i;j++) 
		d[j]='\0'; 
		i=0; 
		k=0; 

	}
	   else if(b>='0'&&b<='9')/*识别常数*/ 
	   {
	    d[i++]=b; 
		b=getchar();
		while(f!=1){ 
		switch (state)
		case 1: 
		if(b>='0'&&b<='9') { 
		state=1;
		d[i++]=b;
		b=getchar();
		}
		else if(b=='.') 
		{ state=2;d[i++]=b;b=getchar();}
		else if(b=='E')
		{ state=4;d[i++]=b;b=getchar();}
		else state=7;
		break; 

   case 2:
        if(b>='0'&&b<='9') { 
			state=3; 
			d[i++]=b;
            b=getchar();} 
			else state=8; 
            break;




	   }






	}















}

  

posted on 2016-09-30 22:12  155林俊彪  阅读(108)  评论(0编辑  收藏  举报

导航