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

📄 wordanalysis.h

📁 一个pascal子集编译器。输入pascal原程序
💻 H
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NMAX 100
#define LMAX 20
#define SIGN_NONE 0
#define SIGN_STR 1//文本常数
#define SIGN_INTEGER 2//整型常数
#define SIGN_REAL 3//浮点常数
#define SIGN_KEYWORD 40//关键字
#define SIGN_SIGN 10//运算符和界符
#define SIGN_NAME 4//标示符 
#define SIGN_ERROR -1//错误的符号

#define SIGN_EVAL 10	 // :=
#define SIGN_ADD 11		 // +
#define SIGN_SUBTRACT 12 // -
#define SIGN_MULTIPLY 13 // *
#define SIGN_DEVIDE 14	 // /
#define SIGN_LEFTBRA 15	 // (
#define SIGN_RIGHTBRA 16 // )
#define SIGN_EQUAL 17	 // =
#define SIGN_LESS 18	 // <
#define SIGN_LESSEQUAL 19// <=
#define SIGN_MORE 20	 // >
#define SIGN_MOREEQUAL 21// >=
#define SIGN_NOTEQUAL 22 // <>
#define SIGN_COMMA 23    // ,
#define SIGN_SEMI 24     // ;
#define SIGN_DECIMAL 25  // .
#define SIGN_OVER 26     // #

#define SIGN_PROGRAM 40  // program
#define SIGN_CONST 41    // const
#define SIGN_VAR 42      // var
#define SIGN_PROCEDURE 43// procedure
#define SIGN_BEGIN 44    // begin
#define SIGN_END 45      // end
#define SIGN_IF 46       // if
#define SIGN_THEN 47     // then
#define SIGN_ELSE 48     // else
#define SIGN_WHILE 49    // while
#define SIGN_DO 50       // do
#define SIGN_CALL 51     // call
#define SIGN_READ 52     // read
#define SIGN_WRITE 53    // write
#define SIGN_ODD 54		 // odd

typedef struct arraystr
{
	int count;
	char str[NMAX][LMAX];
}arraystr;

typedef struct opwords
{
	int type;//单词类型
	int line;//单词位于源程序的第几行
	char data[100];//单词
//	opwords *next;
}opwords;

class WordAnalysis
{
private:
	int signchnum;
	int signnum;
	int n2line;
	int keywordsnum;
	char constkeywords[15][20];
	char constsignch[14];
	char constsign[17][5];
	char input[NMAX*LMAX];
//	opwords *outwordsfirst,*outwordsnow;
	int outwordsnum;
	int totlelength;
	int begin,nowline,count;
	char last;
	char text[1000];
	int tn;
/*	FILE *fp;*/
public:
	WordAnalysis()
	{
		int i=0;
		n2line=0;
		//定义关键字
		char strkeywords[15][20]={"program","const","var","procedure","begin","end","if",
"then","else","while","do","call","read","write","odd"};
		//定义单符号,包括运算符和算符
		char strsignch[14]={'+','-','*','/','(',')','<','>','=',':',',',';','.','#'};
		//定义符号,包括运算符和算符
		char strsign[17][5]={":=","+","-","*","/","(",")","="
		,"<","<=",">",">=","<>",",",";",".","#"};

		keywordsnum=15;
		signchnum=14;
		signnum=17;
		for(i=0;i<keywordsnum;i++)
			strcpy(constkeywords[i],strkeywords[i]);
		for(i=0;i<signnum;i++)
			strcpy(constsign[i],strsign[i]);
		for(i=0;i<signchnum;i++)
			constsignch[i]=strsignch[i];

/*		fp=fopen("in.txt","r");*/
		nowline=1;
		begin=0;
		count=0;
	}

	void SetText(char *str)
	{
		strcpy(text,str);
		tn=0;
	}

	int StrToInt(char *str)
	{	//将字符串转换成整数
		int ans=0,i;
		for(i=0;str[i]!='\0';i++)
			ans=ans*10+str[i]-'0';
		return ans;
	}

	double StrToReal(char *str)
	{	//将字符串转换成浮点数
		double ansp=0,anst=0;
		int i;
		for(i=0;str[i]!='.';i++)
			ansp=ansp*10+str[i]-'0';
		while(str[i]!='\0') i++;
		i--;
		while(str[i]!='.')
		{
			anst=0.1*anst+str[i]-'0';
			i--;
		}
		anst*=0.1;
		return ansp+anst;
	}
/*
	void InsertWords(opwords **first,opwords **now,int ctype,char *cdata,int cline)
	{	//往链表里插入一个单词二元组
		opwords *temp;
		if(*first==NULL)
		{
			temp=(opwords *)malloc(sizeof(opwords));
			strcpy(temp->data.str,"");
			temp->line=0;
			temp->type=NONE;
			temp->next=NULL;
			*first=temp;
			*now=*first;
		}
		temp=(opwords *)malloc(sizeof(opwords));
		temp->type=ctype;
		if(ctype==INTEGER)
			temp->data.integer=StrToInt(cdata);
		else if(ctype==REAL)
			temp->data.real=StrToReal(cdata);
		else if(ctype==SIGN || ctype==STR || ctype==ERROR || ctype==NAME || ctype==KEYWORD)
			strcpy(temp->data.str,cdata);
		temp->line=cline;
		temp->next=NULL;
		(*now)->next=temp;
		*now=temp;
	}
*//*
	opwords *GetWordsLink()
	{
		return outwordsfirst;
	}
*/

	void PrintWords(opwords temp)
	{
		if(temp.type==SIGN_INTEGER) printf("(%s,%d) ",temp.data,temp.type);
		else if(temp.type==SIGN_REAL) printf("(%s,%d) ",temp.data,temp.type);
		else if(temp.type==SIGN_STR) printf("(%s,%d) ",temp.data,temp.type);
		else if(temp.type==SIGN_NAME) printf("(%s,%d) ",temp.data,temp.type);
		else if(temp.type==SIGN_ERROR) printf("(%s,%d) ",temp.data,temp.type);
		else if(temp.type>=SIGN_KEYWORD) printf("(%s,%d) ",temp.data,temp.type);
		else printf("(%s,%d) ",temp.data,temp.type);
	}
		/*
			switch(temp.type)
			{
			case STR:
				printf("(%s,%d) ",temp.data.str,temp.type);
				break;
			case NAME:
				printf("(%s,%d) ",temp.data.str,temp.type);
				break;
			case INTEGER:
				printf("(%d,%d) ",temp.data.integer,temp.type);
				break;
			case REAL:
				printf("(%.3lf,%d) ",temp.data.real,temp.type);
				break;
			case KEYWORD:
					printf("(%s,%d) ",temp.data.str,temp.type);
				break;
			case SIGN:
					printf("(%s,%d) ",temp.data.str,temp.type);
				break;
			case ERROR:
				printf("(%s,%d) ",temp.data.str,temp.type);
				break;
			default:break;
			}
			*/

	bool IsAlpha(char ch)
	{
		if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
			return true;
		else return false;
	}

	bool IsNumber(char ch)
	{
		if(ch>='0' && ch<='9') return true;
		else return false;
	}

	int IsKeywords(char *ch)
	{
		int i;
		for(i=0;i<keywordsnum;i++)
		{
			if(!(strcmp(ch,constkeywords[i]))) return i;
		}
		return -1;
	}

	int IsSign(char *ch)
	{
		int i;
		for(i=0;i<signnum;i++)
		{
			if(!(strcmp(ch,constsign[i]))) return i;
		}
		return -1;		
	}

	int IsSignch(char ch)
	{
		int i;
		for(i=0;i<signchnum;i++)
		{
			if(ch==constsignch[i]) return i;
		}
		return -1;		
	}
	
	opwords GetOneWord()
	{
		int i,k,temptype;
		char strtemp[100],ch;
		opwords result;
		i=0;
		if(1==n2line) nowline++;
		n2line=0;
			if(text[tn]=='\0') 
			{
				strcpy(result.data,"");
				result.type=SIGN_NONE;
				result.line=nowline;
				return result;
			}
			k=begin;
			if(begin==1) {strtemp[0]=last;ch=last;}
			else {ch=text[tn++];strtemp[0]=ch;k++;}
			temptype=SIGN_INTEGER;
			if(IsNumber(ch))
			{//首字符是数字
				ch=text[tn++];
				while(text[tn-1]!='\0' && ' '!=ch && '\n'!=ch && '\t'!=ch && (ch=='.' || IsSignch(ch)<0))//这时切词
				{
					if(ch=='\r') n2line=1;
					if(ch=='.' && temptype!=SIGN_ERROR) temptype=SIGN_REAL;//浮点数
					else if(IsAlpha(ch) || (temptype==SIGN_REAL && ch=='.') ) temptype=SIGN_ERROR;
					//数字后接字母、两个".",都属于错误情况
					strtemp[k++]=ch;
					ch=text[tn++];
				}
				if(ch=='\r') n2line=0;
				strtemp[k]='\0';
				if(temptype==SIGN_INTEGER) 
				{
					strcpy(result.data,strtemp);
					result.type=SIGN_INTEGER;
				}
				else if(temptype==SIGN_REAL)
				{
					strcpy(result.data,strtemp);
					result.type=SIGN_REAL;
				}
				else 
				{
					strcpy(result.data,strtemp);
					result.type=SIGN_ERROR;
				}
	//			InsertWords(&outwordsfirst,&outwordsnow,temptype,strtemp,*nowline);
				begin=0;
			}
			else if(IsSignch(ch)>=0)
			{//首字符是符号
				ch=text[tn++];
				if(text[tn-1]!='\0') 
				{
					strtemp[k++]=ch;
				}
				strtemp[k]='\0';
				if(IsSign(strtemp)<0) 
				{//下一个的字符构不成长度为2的符号信息,要回退
					begin=1;
					strtemp[k-1]='\0';
				}
				else if(text[tn-1]!='\0') {ch=text[tn++];begin=0;}
				strcpy(result.data,strtemp);
				result.type=IsSign(strtemp)+SIGN_SIGN;
//				InsertWords(&outwordsfirst,&outwordsnow,SIGN,strtemp,*nowline);
			}
			else if(ch=='\'')
			{//首字符是',说明以下是字符常量
				k--;
				ch=text[tn++];
//				strtemp[k++]=ch;
				while(text[tn-1]!='\0' && '\''!=ch) {strtemp[k++]=ch;ch=text[tn++];}
				strtemp[k]='\0';
				result.type=SIGN_STR;
				strcpy(result.data,strtemp);
//				InsertWords(&outwordsfirst,&outwordsnow,STR,strtemp,*nowline);
				ch=text[tn++];
			}
			else if(IsAlpha(ch))
			{//首字符是字母
				ch=text[tn++];
				while(text[tn-1]!='\0' && '\n'!=ch && ' '!=ch  && '\t'!=ch && '\r'!=ch && IsSignch(ch)<0)
				{
					strtemp[k++]=ch;
					ch=text[tn++];
				}
				strtemp[k]='\0';
				if(IsKeywords(strtemp)>=0) 
				{
					strcpy(result.data,strtemp);
					result.type=IsKeywords(strtemp)+SIGN_KEYWORD;
//					InsertWords(&outwordsfirst,&outwordsnow,KEYWORD,strtemp,*nowline);
				}
				else
				{
					strcpy(result.data,strtemp);
					result.type=SIGN_NAME;
//					InsertWords(&outwordsfirst,&outwordsnow,NAME,strtemp,*nowline);
				}
			}
			else if(ch=='\n' || ch=='\t' || ch==' ' || ch==13) 
			{
				//滤去多余的空格符、换行符
				do
				{
					if(ch=='\r') nowline++;//下一行
					ch=text[tn++];
				}while(ch=='\n' || ch=='\t' || ch==' ');
				begin=1;
				last=ch;
				return GetOneWord();
			}
			begin=1;
			last=ch;
			result.line=nowline;
			return result;
	}

	void Action()
	{
		opwords every;
		do
		{
			count++;
			if(count==5) {printf("\n");count=0;}
			every=GetOneWord();
			if(every.type!=SIGN_NONE) PrintWords(every);
		}
		while(every.type!=SIGN_NONE);
	}
};

⌨️ 快捷键说明

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