贴一个刚写的 词法解析程序吧(C语言),很简陋,呵呵。。。

/**
 * 2012年6月25日 中午
 * 
 * 山东科技大学信息学院  版权所有
 * 
 * 联系邮箱:[email protected]
 * 
 * Copyright @2012, sdust, All Rights Reserved
 * 
 * author 王昌帅
 *
 */
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>

#define MAX 1024
#define MAX_LENGTH 10
#define NUM_RESERVED_WORD 40
#define NUM_OPERATOR 6
#define NUM_SEPARATOR 16

//函数声明
//用于输入的函数
void input(char*);
//用于输出的函数
void output();
//判断单词的是哪种标识符
int judge(char*);
//将输入的字符串解析成一个一个的单词
void parse2Array(char*);
//判断字符是否是字母
int isChar(char c);
//判断字符是否是操作符
int isOperator(char c);
//判断字符是否是分隔符
int isSeparator(char c);
//判断字符是否是数字
int isNum(char c);
//判断是否是基本保留字(参数:字符串)
int isReservedWord_str(char* str);
//判断是否是操作符(参数:字符串)
int isOperator_str(char* str);
//判断是否是操作符(参数:字符串)
int isSeparator_str(char* str);
//判断是否是数字串(参数:字符串)
int isNum_str(char* str);
//判断是否是标识符(参数:字符串)
int isIndentifier_str(char* str);


//定义标识符
char sign_reserved_word[][MAX_LENGTH] = {"auto","break","continue","char","case","const","class","default","double","do","define","extern"
              ,"enum","else","friend","float","for","goto","include","int","if","long","namespace","protected",
			  "public","private","register","return","static","short","signed","switch","sizeof","struct","typedef"
			  ,"unsigned","union","using","void","while"};
char sign_operator[][MAX_LENGTH] = {"+","-","*","/","%","="};
char sign_separator[][MAX_LENGTH] = {"\n"," ","\"","\t",",",";",":","'","<",">","(",")","{","}","[","]"};

//单词总数(包括运算符、分隔符等)
int wordNum = 0;
//解析好的单词
char array[MAX][20];

//主函数
int main()
{
	char str[MAX],c;
	while(1)
	{
		system("cls");
		input(str);
		parse2Array(str);
		output();

		printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");

		printf("按Esc退出,按其他任意建继续...\n");
		if((c=getch())==27)
		{break;}
	}
}


//用于输入的函数
void input(char* str)
{
	int i = 0,j = 0;
	char c;
	str[0] = '\0';
	printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	printf("请输入要解析的语句(按@结束输入,按esc退出):\n\n");
	for(i=0;(c=getch())!='@';i++)
	{
		if(c==27)
			exit(0);
		if(c==8)
		{
			i-=2;
			if(i<-1)
				i=-1;
			system("cls");
			printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
			printf("请输入要解析的语句(按@结束输入,按esc退出):\n\n");
			for(j=0;j<=i;j++)
			{
				printf("%c",str[j]);
			}
			continue;
		}
		else if(c==13)
		{
			str[i] = '\n';
			printf("\n");
		}
		else
		{
			str[i] = c;
			printf("%c",c);
		}
		str[i+1] = '\0';
	}
}



//将输入的字符串解析成一个一个的单词
void parse2Array(char* str)
{
	int count; 
	int i,temp;
	wordNum = 0;
	count = strlen(str);
	for(i=0;i<count;i++)
	{
		temp = i;
		if(isOperator(str[i]))
		{//如果是运算符
			array[wordNum][0]=str[i];
			array[wordNum][1]='\0';
			wordNum++;
		}
		else if(isSeparator(str[i]))
		{//如果是分隔符
			array[wordNum][0]=str[i];
			array[wordNum][1]='\0';
			wordNum++;
		}
		else
		{
			for(;str[i]!='\0'&&(!isOperator(str[i]))&&(!isSeparator(str[i]));i++)
			{
				array[wordNum][i-temp]=str[i];
			}
			array[wordNum][i-temp]='\0';
			wordNum++;
			i--;
		}
	}
}

//输出信息
void output()
{
	int i ;

	printf("\n");
	printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
	for(i = 0;i<wordNum;i++)
	{
		int flag = judge(array[i]);
		switch(flag)
		{
		case 1:
			printf(" 1、基本保留字:\t%s\n",array[i]);
			break;
		case 2:
			printf(" 2、标识符    :\t%s\n",array[i]);
			break;
		case 3:
			printf(" 3、常数      :\t%s\n",array[i]);
			break;
		case 4:
			printf(" 4、运算符    :\t%s\n",array[i]);
			break;
		case 5:
			if(strcmp(array[i],"\n")==0)
			{
				//printf(" 5、分隔符    :\t\\n\n",array[i]);
			}
			else if(strcmp(array[i]," ")==0)
			{
				//printf(" 5、分隔符    :\t空格\n",array[i]);
			}
			else if(strcmp(array[i],"\t")==0)
			{
				//printf(" 5、分隔符    :\t\\t\n",array[i]);
			}
			else
			{
				printf(" 5、分隔符    :\t%s\n",array[i]);
			}
			break;
		case -1:
			printf    ("-1、Error     :\t%s\n",array[i]);
			break;
		default:
			printf("-1、Error     :\t%s\n",array[i]);
			break;
		}
	}
}

//判断是哪类字符(1、基本保留字 2、标识符 3、常数 4、运算符 5、分隔符 -1、不能识别(Error))
int judge(char * word)
{
	if(isSeparator_str(word))
	{
		return 5;
	}
	else if(isOperator_str(word))
	{
		return 4;
	}
	else if(isReservedWord_str(word))
	{
		return 1;
	}
	else if(isNum_str(word))
	{
		return 3;
	}
	else if(isIndentifier_str(word))
	{
		return 2;
	}
	else
	{
		return -1;
	}
}

//判断是否是字符
int isChar(char c)
{
	if(c>='A'&&c<='Z'||c>='a'&&c<='z')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//判断是否是数字(单个字符)
int isNum(char c)
{
	if(c>='0'&&c<='9')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//判断是否是操作符(单个字符)
int isOperator(char c)
{
	int i ;
	char sign[] = {'+','-','*','/','%','='};
	for(i = 0;i<NUM_OPERATOR;i++)
	{
		if(c==sign[i])
		{
			return 1;
		}
	}
	return 0;
}

//判断是否是分隔符(单个字符)
int isSeparator(char c)
{
	int i ;
	char sign[] = {'\n',' ','\"','\t',',',';',':','\'','<','>','(',')','{','}','[',']'};
	for(i = 0;i<NUM_SEPARATOR;i++)
	{
		if(c==sign[i])
		{
			return 1;
		}
	}
	return 0;
}

//判断是否是基本保留字(参数:字符串)
int isReservedWord_str(char* str)
{
	int i ;
	for(i = 0;i<NUM_RESERVED_WORD;i++)
	{
		if(strcmp(str,sign_reserved_word[i])==0)
		{
			return 1;
		}
	}
	return 0;
}

//判断是否是操作符(参数:字符串)
int isOperator_str(char* str)
{
	int i ;
	for(i = 0;i<NUM_OPERATOR;i++)
	{
		if(strcmp(str,sign_operator[i])==0)
		{
			return 1;
		}
	}
	return 0;
}

//判断是否是分隔符(参数:字符串)
int isSeparator_str(char* str)
{
	int i ;
	for(i = 0;i<NUM_SEPARATOR;i++)
	{
		if(strcmp(str,sign_separator[i])==0)
		{
			return 1;
		}
	}
	return 0;
}

//判断是否是数字串(参数:字符串)
int isNum_str(char* str)
{
	int i , count;
	count = strlen(str);
	for(i = 0;i<count;i++)
	{
		if(str[i]<'0'||str[i]>'9')
		{
			return 0;
		}
	}
	return 1;
}

//判断是否是标识符(参数:字符串)
int isIndentifier_str(char* str)
{
	int i , count;
	count = strlen(str);
	if(str[0]>='0'&&str[0]<='9')
	{
		return 0;
	}
	for(i = 0;i<count;i++)
	{
		if(!isChar(str[i])&&!isNum(str[i])&&(str[i]!='_'))
		{
			return 0;
		}
	}
	return 1;
}


 

效果图如下:

贴一个刚写的 词法解析程序吧(C语言),很简陋,呵呵。。。

你可能感兴趣的:(C语言)