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

📄 compile.c

📁 词法分析
💻 C
字号:
#include "compile.h"

void main()
{
	printf("\nPlease input the expression:");
	gets(string);
	current=string;
	printf("\nkind\tinput\tnum");
	printf("\n-------------------------");
	while(1) ana();
}

void ana()
{   *strToken='\0';
	GetChar();GetBC();
	if(IsLetter())
	{
		while(IsLetter()||IsDigit())
		{
			Concat();GetChar();
		}
		Retract();
		code=Reserve();
		if(code==0)
		{
			value=InsertId(strToken);
			printf("\n$ID\t%s\t%d",strToken,value);
		}
		else	printf("\n$RESERV\t%s\t%d",strToken,code);
	}
	else if(IsDigit())
	{
		while(IsDigit())
		{
			Concat();GetChar();
		};
		Retract();
		value=InsertConst(strToken);
		printf("\n$INT\t%s\t%d",strToken,value);
	}
	else if(ch=='=') printf("\n$ASSIGN\t=\t8");
	else if(ch=='+') printf("\n$PLUS\t+\t9");
	else if(ch=='*')
	{	GetChar();
		if(ch=='*')  printf("\n$POWER\t**\t11");
		else{Retract();	 printf("\n$STAR\t*\t10");}
	}
	else if(ch==';') printf("\n$SEMICOLON\t;\t12");
	else if(ch=='(') printf("\n$LPAR\t(\t13");
	else if(ch==')') printf("\n$RPAR\t)\t14");
	else if(ch=='{') printf("\n$LBRACE\t{\t15");
	else if(ch=='}') printf("\n$RBRACE\t}\t16");
	else if(ch=='\0'){printf("\nThe end\nPress anykey to exit...");getch();exit(-1);}
	else {printf("\nWrong input!!!!!!\nPress anykey to exit...");getch();exit(-1);};
	return;
}

void GetChar()
{
	ch=*current;
	current++;
}

void Concat()
{   char *temp;
	temp=strToken+strlen(strToken)*sizeof(char);
	*temp=ch;
	*(temp+1)='\0';
}
int IsLetter()
{	if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) return 1;
	else return 0;
}

int IsDigit()
{
	if(ch>='0'&&ch<='9') return 1;
	else return 0;
}
int Reserve()
{   int i;
	for(i=0;i<TokenNum;i++)
		if(strcmp(strupr(strToken),Token[i])==0) return i;
	return 0;
}
void Retract()
{
	current--;
	ch=' ';
}
int InsertId()
{   int i;
	for(i=0;i<TokenIdNum;i++)
		if(strcmp(strToken,TokenId[i])==0) return i+1;
	strcpy(TokenId[TokenIdNum++],strToken);
	return TokenIdNum;
}

int InsertConst()
{
	int i;
	for(i=0;i<TokenConstNum;i++)
		if(strcmp(TokenConst[i],strToken)==0) return i+1;
	strcpy(TokenConst[TokenConstNum++],strToken);
	return TokenConstNum;
};

void GetBC()
{
	while(ch==' '||ch=='\t') GetChar();
}

⌨️ 快捷键说明

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