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

📄 lword.c

📁 自己开发的c0文法编译器
💻 C
字号:
#include "world.h"
int lineno = 1;
char * int_to_string(int );

int tokenval = NONE;

char buffer[BSIZE] = "";//初始化缓冲区


char keyword[keyWordNum][keyWordMaxLen] = {"main","const","void", "int","if","else","while","scanf","printf","return"};//关键字表

int lookup_in_keyword(char *);





int lookup_in_keyword(char *s)
{
	int i = 0;
	for(i=0;i<keyWordNum;i++)
	{
		if(strcmp(strlwr(s),strlwr(keyword[i]))==0)
			return i;
	}
	return i; 
}

/*
int gsm()
{
	char ch1;						
	int k,i,j;
    ch1=fgetc(FIN);
	
	while(isspace(ch1))
	{
		putc(ch1,FOUT);
		if(ch1=='\n')
		{
			linenumber++;
			printf("%d:\t",linenumber);
		}
		ch1=fgetc(FIN);
	}

	i=0;					
	Word[i]=ch1;
	i++;
	if(isdigit(ch1))
	{
		putc(ch1,FOUT);

		while(isdigit((ch1=fgetc(FIN))))
		{
			putc(ch1,FOUT);
			Word[i++]=ch1;
			if(i==MW)
			{
				erline[ernum]=linenumber;
				error[ernum++]=1;
				Word[--i]='\0';
				ch1=fgetc(FIN);
				while(isdigit(ch1))
				{
					putc(ch1,FOUT);
				}
				num=atoi(Word);
				ungetc(ch1,FIN);
				return 2;
			}

		}
		ungetc(ch1,FIN);
		Word[i]='\0';
		num=atoi(Word);
		return 2;
	}

    if(isalpha(ch1))
	{
		putc(ch1,FOUT);
		ch1=fgetc(FIN);
		while(isalpha(ch1)||isdigit(ch1))
		{
			Word[i]=ch1;
			i++;

			if(i==MW)
			{
				erline[ernum]=linenumber;
				error[ernum++]=1;
				i--;
				Word[i]='\0';
				putc(ch1,FOUT);
				ch1=fgetc(FIN);

				while(isalpha(ch1)||isdigit(ch1))
				{
					putc(ch1,FOUT);
				}
				ungetc(ch1,FIN);
				return 1;
			}
			putc(ch1,FOUT);
			ch1=fgetc(FIN);	
		}
		ungetc(ch1,FIN);
		Word[i]='\0';

		for(k=0;k<UU;k++)
		{
			if(!(strcmp(Keyword[k],Word)))
			{
				return k+10;
			}
		}
		if(k==UU){
			return 1;
		}
	}
	
	i=34;
	putc(ch1,FOUT);
	switch(ch1){
	case'"':
			j=0;
			while((ch1=fgetc(FIN))!='"')
			{
				putc(ch1,FOUT);
				bbsstring[j++]=ch1;

				if(j==ML)
				{
					printf("warning!字符串过长\n");
					bbsstring[--j]='\0';
					putc(ch1,FOUT);

					ch1=fgetc(FIN);
					while(ch1!='"')
					{
						putc(ch1,FOUT);
						j++;
						if(ch1==EOF||j>100)
						{
							erline[ernum]=linenumber;
							error[ernum++]=5;
							return -1;
						}
					}
					return 5;
				}
			}
			putc(ch1,FOUT);
			bbsstring[j]='\0';
			return 5;
    
    case'\'':
			ch1=fgetc(FIN);
			if(ch1=='+'||ch1=='-'||ch1=='*'||ch1=='\''||isalpha(ch1)) 
			{
				baschar=ch1;
				putc(ch1,FOUT);
				if ((ch1=fgetc(FIN))=='\'')
				{
					putc(ch1,FOUT);
					return 4;
				}
				else 
				{
					putc(ch1,FOUT);
					erline[ernum]=linenumber;
					error[ernum++]=3;
					return -1;
				}
			}  
    	
	case'/':
		if((ch1=getc(FIN))=='/')
		{
			putc(ch1,FOUT);
			while((ch1=fgetc(FIN))!='\n')
				putc(ch1,FOUT);
			putc(ch1,FOUT);
			linenumber++;
			printf("%d:\t",linenumber);
			return (gsm());
		}
}
*/




int Word()
{
	
int ch;
	int temp;
	int index = 0;
	while(1)
	{
		ch = getchar();
		if(ch==' '||ch=='\t') ;//忽略空格和制表符
		else if(ch=='\n')
		{
			lineno++;
		}
		else if(isdigit(ch)||ch=='+'||ch=='-')
		{
			if(isdigit(ch))
			{	ungetc(ch,stdin);
				scanf("%d",&temp);//numbers
				strcpy(buffer,int_to_string(temp));
				return NUM;
			}
			else if(ch=='-')
			{
				buffer[0]=ch;
				for(temp=1;;temp++)
				{
					ch=getchar();
					buffer[temp]=ch;
					if(!isdigit(ch))
						break;
				}
				ungetc(ch,stdin);
				buffer[temp]='\0';
				if(temp!=1)
					return NUM;
				else if(buffer[0]=='-')
				{
					buffer[0]='\0';
					return minus;
				}

			}
			else if(ch=='+')
			{
				buffer[0]=ch;
				for(temp=1;;temp++)
				{
					ch=getchar();
					buffer[temp]=ch;
					if(!isdigit(ch))
						break;
				}
				ungetc(ch,stdin);
				buffer[temp]='\0';
				if(temp!=1)
					return NUM;
				else if(buffer[0]=='+')
				{
					buffer[0]='\0';
					return plus;
				}

			}
		}
		else if(isalpha(ch)||ch=='_')
		{
			index = 0;
			while(isalnum(ch)||ch=='_')
			{
				buffer[index++] = ch;
				ch = getchar();
			}
			ungetc(ch,stdin);
			buffer[index]='\0';
			return lookup_in_keyword(buffer);
		}
	//	else if(ch=='+')
	//		return plus;
	//	else if(ch=='-')
	//	{
	//		return minus;
	//	}
		else if(ch=='*')
			return multiply;
		else if(ch=='/')
			return divide;
		else if(ch=='<')
		{
			ch = getchar();
			if(ch=='=')
			{
				return LEQ;
			}
			else
			{
				ungetc(ch,stdin);
				return LT;
			}
		}
		else if(ch=='>')
		{
			ch = getchar();
			if(ch=='=')
				return GEQ;
			else
			{
				ungetc(ch,stdin);
				return GT;
			}
		}
		else if(ch=='!')
		{
			ch = getchar();
			if(ch=='=')
			{
				return NEQ;
			}
			else
			{
					index = 0;
					while(ch!='\n')//出错 跳过该行
					{
						buffer[index++] = ch;
						ch = getchar();
					}
					buffer[index]='\0';
					existerror(buffer,lineno);
					lineno++;
					return -1;//出错
			}
		}
		else if(ch=='=')
		{
			ch = getchar();
			if(ch=='=')
				return IS_EQ;
			else
			{
				ungetc(ch,stdin);
				return EQ;
			}
		}
		else if(ch=='(')
		{
			return LK;
		}
		else if(ch==')')
		{
			return RK;
		}
		else if(ch=='{')
		{
			return LB;
		}
		else if(ch=='}')
		{
			return RB;
		}
		else if(ch==';')
		{
			return fenghao;
		}
		else if(ch==',')
		{
			return douhao;
		}
		else if(ch=='"')
		{
			return maohao;
		}
		else if(ch==EOF)
		{
			//printf("jieshu");
			return OVER;
		}
		else
		{
			index = 0;
					while(ch!='\n')//出错 跳过该行
					{
						buffer[index++] = ch;
						ch = getchar();
					}
					buffer[index]='\0';
					existerror(buffer,lineno);
					lineno++;
					return -1;//出错

		}
	}
}




int isnumber(char *s)
{
	int i=0;
	int len = strlen(s);
	if(s[i]=='-')
	{
		i++;
		sign=-1;
	}
	else if(s[i]=='+')
		i++;

	while(i<len)
	{
		if(!isdigit(s[i]))
			return 0;
		i++;
	}
	return 1;
}



char * int_to_string(int t)
{
	char s0[20];
	char *s1 = (char *)malloc(20);
	int temp,i=0;
	int len;
	while(t>0)
	{
		temp = t%10;
		s0[i++] = temp + '0';
		t /= 10;
	}
	if(i==0)
	{
		s0[i++] ='0';
	}
	len = i;

	for(i=0;i<len;i++)
	{
		s1[i] = s0[len-1-i];
	}
	s1[len] = '\0';
	return s1;

}

⌨️ 快捷键说明

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