0917 实验一 词法分析程序

#include <stdio.h>
#include <string.h>
#include <iostream.h>
#define BEGIN 1
#define IF 2 
#define THEN 3
#define WHILE 4

#define  DO 5
#define  END 6
#define  INT 11
#define   LT 20
#define   LE 21
#define  EQ 24
#define  NE 22
#define  GT 12
#define  GE 24
#define  IS 18
#define   PL 13
#define   MI 14
#define  MU 15
#define  DI 16
#define SS 17
#define ID 10
#define  AI  26
#define  BI 27
#define  HI 58
#define  JI  0

char *keyword[8]={"begin","end","if","then","do","while"};
int i=0,j=0,k=0,t=0;
char ch,strtoken[20];
char * chr_form[100];
char * int_form[100];
char form[1000];
int q=0,temp;
void GetChar()
{
	ch=form[k];
	k++;
}
void getbc()
{
	while(ch==' ')
	{
		
		GetChar();
	}
}
void concat()
{
	strtoken[i]=ch;
	i++;
}
bool isletter()
{
	if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
		return(1);
	else
		return(0);
}
bool isdigit()
{
if(((ch)<='9')&&((ch)>='0'))
return (1);
else
return (0);
}
int reserve()
{
	for(int q=0;q<5;q++)
	{
		if(strcmp(keyword[q],strtoken)==0)
			return q;
		if(q==4)
			return -1;
	}
}
void retract()
{
	k--;
	ch=NULL;
}
char *insertld()
{
	chr_form[j]=strtoken;
	j++;
	return chr_form[0];
}
char * insertconst()
{
	int_form[t]=strtoken;
	t++;
	return int_form[0];
}
int code;
void output(int a,char *p1,char *p2)
{
	cout<<"\t种别码:"<<a<<"\t 单词值:";
		while(p1<=p2)
		{
			printf("%s",*p1);
			p1++;
		}
		cout<<endl;
}

void analyze()
{
	GetChar();
	getbc();
	if(isletter())
	{
		while (isletter()||isdigit())
		{
			concat();
		GetChar();
		}
		retract();
		code=reserve();
		switch(code)
		{
		case 0:cout<<"单词:" <<strtoken<<"  种别码为: "<<BEGIN<<endl;break;

		case 1:cout<<"单词:" <<strtoken<<"  种别码为: "<<END<<endl;break;
	
        case 2:cout<<"单词:" <<strtoken<<"  种别码为: "<<IF<<endl;break;

        case 3:cout<<"单词:" <<strtoken<<"  种别码为: "<<THEN<<endl;break;
        case 4:cout<<"单词:" <<strtoken<<"  种别码为: "<<DO<<endl;break;
        case 5:cout<<"单词:" <<strtoken<<"  种别码为: "<<WHILE<<endl;break;
		
	  default:
		  cout<<"单词:" <<strtoken<<"  种别码为: "<<ID<<endl;break;


	}
	}
	else
	{
		if( isdigit())
		{
          while(isdigit()||ch=='.')
		  {
			  concat();
			  GetChar();
		  }
		  retract();
		  cout<<"单词:"<<strtoken<<"  种别码为:"<<INT<<endl;
		}
		else
		{
			switch(ch)
			{
			case'+': cout<<"单词:+  种别码为: "<<PL<<endl;break;
            case'-': cout<<"单词:-  种别码为: "<<MI<<endl;break;
		    case'*': cout<<"单词:*  种别码为: "<<MU<<endl;break;
			case'/': cout<<"单词:/  种别码为: "<<DI<<endl;break;
			case';': cout<<"单词:;  种别码为: "<<AI<<endl;break;
			case'(': cout<<"单词:(  种别码为: "<<BI<<endl;break;
			case')': cout<<"单词:)  种别码为: "<<HI<<endl;break;
			case'#': cout<<"单词:#  种别码为: "<<JI<<endl;break;
		
			case':':GetChar();
				if(ch=='=')
				{
					cout<<"单词::=  种别码为: "<<IS<<endl;break;
				}
				else
				{
					retract();
					cout<<"单词::   种别码为: "<<SS<<endl;break;
				}
			case'=':cout<<"单词:=  种别码:"<<EQ<<endl; break;
			case'>':GetChar();switch(ch)
					{
			case'=':cout<<"单词:>=  种别码:"<<GE<<endl;break;
			default: retract;
				cout<<"单词:=> 种别码:"<<GT<<endl;break;
					}
			case'<':GetChar();
				switch(ch)
				{
				case'=':cout<<"单词:<=  种别码:"<<LE<<endl;break;
                case'>':cout<<"单词:<>  种别码:"<<NE<<endl;break;
               default: retract();
					cout<<"单词:<  种别码:"<<LT<<endl;break;
			}
		}
	}
	}

	while(k<q)
	{
		for(int p=0;p<50;p++)
			strtoken[p]='\0';
		i=0;
		analyze();
	}
}

void main ()
{
	printf("输入一段程序,以!结束:");
	form[0]=cin.get();
	for(q=1;form[q-1]!='!!';q++)
	{
		form[q]=cin.get();
		if(form[q]=='!')
		{
			printf("你输入的程序段为:\n");
			cout.write(form,q);
			break;
		}
	}
	cout<<endl;
	analyze();

}

 

posted @ 2015-09-21 13:33  39赖文亮1  阅读(153)  评论(0编辑  收藏  举报