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

📄 scanner.cpp

📁 生成类汇编代码的编译器
💻 CPP
字号:
#include<fstream.h>
#include<iostream.h>
#include<string.h>
#include<stdio.h>

#define PLUS		0
#define MINUS		1
#define STAR		2	
#define SLASH		3
#define LT	     	4
#define LTEQ		5
#define GT	    	6
#define GTEQ		7
#define EQ	    	8
#define NEQ		    9
#define ASSIGN		10
#define SEMI		11
#define COMMA		12
#define COLON		13
#define DOT	    	14
#define WAVE		15
#define LPAREN		16
#define RPAREN		17
#define LSQUAR		18
#define RSQUAR		19
#define LBRACE		20
#define RBRACE		21
#define LCOMMENT	22
#define RCOMMENT	23
#define ID	    	24
#define NUMBER		25

#define ERROR	100
#define BUFFERSIZE	20

char * operaterType[] = {"PLUS","MINUS","STAR","SLASH","LT","LTEQ","GT","GTEQ","EQ",
		     "NEQ","ASSIGN","SEMI","COMMA","COLON","DOT","WAVE","LPAREN",
		     "RPAREN","LSQUAR","RSQUAR","LBRACE","RBRACE","LCOMMENT","RCOMMENT",
		     "ID","NUMBER"};
char * operater[]={"+","-","*","/","<","<=",">",">=","==","!=","=",";",",",":",".","~",
                  "(",")","[","]","{","}","/*","*/"};
char * keyWordType[] = {"EXP","THROW","CATCH","TRY","VIRTUAL","THIS","DELETE",
		        "NEW","PROTECTED","PRIVATE","PUBLIC","CLASS","RETURN",
		        "WHILE","IF","ELSE","INT","VOID"};

char * keyWord[] = {"exp","throw","catch","try","virtual","this","delete",
		    "new","protected","private","public","class","return",
		    "while","if","else","int","void"};


ifstream in;
ofstream out;
int line;
int error;
char buffa[BUFFERSIZE];
char buffb[BUFFERSIZE];
int sp,bp,state,used;
char  GetChar()
{
	if(sp!=BUFFERSIZE && state==0)
	{
		sp++;
		return(buffa[sp-1]);
	}
	else if(sp!=BUFFERSIZE && state==1)
	{
		sp++;
		return(buffb[sp-1]);
	}
	else if(sp==BUFFERSIZE && state==0)
	{
		state=1;
		sp=1;
		if(used!=1)
			in.read((unsigned char *)&buffb,BUFFERSIZE);
		used=0;
		return(buffb[0]);
	}
	else if(sp==BUFFERSIZE && state==1)
	{
		state=0;
		sp=1;
		if(used!=1)
			in.read((unsigned char *)&buffa,BUFFERSIZE);
		used=0;
		return(buffa[0]);
	}
}
void  GetBC(char * ch)
{
	while(*ch=='\n' || *ch=='\t' ||*ch==' ')
	{
		if(*ch=='\n')
			line++;
		*ch=GetChar();
	}
}
int  Isletter(char  ch)
{
	if((ch<='z'&& ch>='a') || (ch<='Z'&&ch>='A'))
		return 1;
	else return 0;
}
int IsDigit(char ch)
{
	if(ch<='9' && ch>='0')
		return 1;
	else return 0;
}
int Reserve(char *strToken)
{
	int i;
	for(i=0;i<18;i++)
	{
		if(!strcmp(strToken,keyWord[i]))
		{
			return i+26;
			break;
		}
	}
	return ID;
}
void Retract()
{
	if(sp!=0 && state==0)
		sp--;
	else if(sp!=0 && state==1)
		sp--;
	else if(sp==0 && state==0)
	{
		sp=BUFFERSIZE;
		state=1;
		used=1;
	}
	else if(sp==0 && state==1)
	{
		sp=BUFFERSIZE;
        state=0;
		used=1;
	}
}
void Concat(char* strToken,char ch)
{
	int i=0;
	while(strToken[i])
		i++;
	strToken[i]=ch;
}
void writexml(int type,char*strToken)
{
	char* str1="\t<token line=\"";
	char* str2="\" type=\"";
	char* str3="\"string=\"";
	char* str4="\" />\n";
	if(type==ERROR)
	{
		error++;
		cout<<"error"<<error<<"  in line"<<line<<"    about  \""<<strToken<<'\"'<<endl; 
	}
	out<<str1;
	out<<line;
	out<<str2;
	if(type<26)
		out<<operaterType[type];
	else out<<keyWordType[type-26];
	out<<str3;
	if(type<24)
		out<<operater[type];
	else if(type>25)
		out<<keyWord[type-26];
	else out<<strToken;
	    out<<str4;
}


void scaner()
{
	char strToken[BUFFERSIZE*2];
	char ch;
	int type,temp1,temp2;
	for(int i=0;i<2*BUFFERSIZE;i++)
			strToken[i]='\0';
	ch=GetChar();
	bp=sp;
	GetBC(& ch);
	while(ch!='$')
	{
	if(Isletter(ch))
	{
		bp=sp;
		while(Isletter(ch)||IsDigit(ch))
		{
			Concat(strToken,ch);
			ch=GetChar();
		}
		Retract();
        type=Reserve(strToken);
		writexml(type,strToken);
		for(int i=0;i<2*BUFFERSIZE;i++)
			strToken[i]='\0';
	}
	else if(IsDigit(ch))
	{
		while(IsDigit(ch))
		{
			Concat(strToken,ch);
			ch=GetChar();
		}
		Retract();
		type=NUMBER;
		writexml(type,strToken);
		for(int i=0;i<2*BUFFERSIZE;i++)
			strToken[i]='\0';
	}
	else if(ch=='+')
	{
		type=PLUS;
		writexml(type,strToken);
	}
	else if(ch=='-')
	{
		type=MINUS;
		writexml(type,strToken);
	}
	else if(ch=='*')
	{
		type=STAR;
		writexml(type,strToken);
	}
	else if(ch=='\\')
	{
		type=SLASH;
		writexml(type,strToken);
	}
	else if(ch=='<')
	{
		ch=GetChar();
		if(ch=='=')
		{
			type=LTEQ;
			writexml(type,strToken);
		}
		else 
		{
			Retract();
			type=LT;
			writexml(type,strToken);
		}
	}
	else if(ch=='>')
	{
		ch=GetChar();
		if(ch=='=')
		{
			type=GTEQ;
			writexml(type,strToken);
		}
		else
		{
			Retract();
			type=GT;
			writexml(type,strToken);
		}
	}
	else if(ch=='=')
	{
		ch=GetChar();
		if(ch=='=')
		{
			type=EQ;
			writexml(type,strToken);
		}
		else
		{
			Retract();
			type=ASSIGN;
			writexml(type,strToken);
		}
	}
	else if(ch=='!')
	{
		ch=GetChar();
		if(ch=='=')
		{
			type=NEQ;
			writexml(type,strToken);
		}
		else
		{
			Retract();
			type=ERROR;
			writexml(type,strToken);
		}
	}
	else if(ch==';')
	{
		type=SEMI;
		writexml(type,strToken);
	}
	else if(ch==',')
	{
		type=COMMA;
		writexml(type,strToken);
	}
	else if(ch==':')
	{
		type=COLON;
		writexml(type,strToken);
	}
	else if(ch=='.')
	{
		type=DOT;
		writexml(type,strToken);
	}
	else if(ch=='~')
	{
		type=WAVE;
		writexml(type,strToken);
	}
	else if(ch=='(')
	{
		type=LPAREN;
		writexml(type,strToken);
	}
	else if(ch==')')
	{
		type=RPAREN;
		writexml(type,strToken);
	}
	else if(ch=='[')
	{
		type=LSQUAR;
		writexml(type,strToken);
	}
	else if(ch==']')
	{
		type=RSQUAR;
		writexml(type,strToken);
	}
	else if(ch=='{')
	{
		type=LBRACE;
		writexml(type,strToken);
	}
	else if(ch=='}')
	{
		type=RBRACE;
		writexml(type,strToken);
	}
	else if(ch=='/')
	{
		ch=GetChar();
		if(ch=='*')
		{
			type=LCOMMENT;
			writexml(type,strToken);
			ch=GetChar();
			temp1=0;
			temp2=0;
			while(temp2==0)
			{
				temp1=0;
				while(ch!='*')
				{
					ch=GetChar();
					if(ch=='$')
					{
						type=ERROR;
						writexml(type,strToken);
						temp1=1;
					}
				}
				if(temp1!=1)
				{
					ch=GetChar();
					if(ch=='/')
					{
						type=RCOMMENT;
						writexml(type,strToken);
					    temp2=1;
					}
				}
			}
		}
		else
		{
			Retract();
			type=ERROR;
			writexml(type,strToken);
		}
	}
	else
	{
		type=ERROR;
		writexml(type,strToken);
	}
	ch=GetChar();
	GetBC(& ch);
	}
}
void main()
{
	char fileName1[20],fileName2[20];	
	char head[] = "<?xml version=\"1.0\"?>\n<root>\n";
	char tail[] = "</root>";
	sp=0;
	bp=0;
	state=0;
	used=0;
	line = 1;
	error=0;
	gets(fileName1);
//	strcpy(fileName1,"G:\\exam2class");		
	strcpy(fileName2,fileName1);
	in.open (strcat(fileName1,".cm"),ios::nocreate);
	if(!in)
	{
		cout<<"can't find file!!"<<endl;
		return;
	}
	out.open(strcat(fileName2,".xml"));	
	in.read((unsigned char *)&buffa,BUFFERSIZE);	
	out<<head;
	scaner();
    out<<tail;
	in.close ();
	out.close ();
}


	








		




						












⌨️ 快捷键说明

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