c之词法分析程序


一、完成
    1、掌握从源程序文件中读取有效字符的方法和产生源程序的内部表示文件的方法。
    2、掌握词法分析的实现方法。
二、环境:vs2013
#include<stdio.h>
#include<string.h>
#include<Windows.h>

#pragma warning(disable:4996)

//宏函数
#define PRINT(NUM, NAME) printf("(%d,\"%s\")\n",NUM,NAME)
#define len 50


int main()
{
	//以只读模式打开此文件
	FILE *fp = NULL;
	fp = fopen("source.txt", "r");
	char lexi[len];
	int slen = 0;
	int flag = 0;
	int i = 0;
	while (!feof(fp)){
		fscanf(fp, "%s", lexi);//以空格为终结每次读取
		slen = strlen(lexi);
		i = 0;
		char tmp[10] = { '\0' };
		while (i < slen){
			if (lexi[i] == ',' || lexi[i] == ';' || lexi[i] == '{' || \
				lexi[i] == '}' || lexi[i] == '(' || lexi[i] == ')'){
				printf("(5,\"%c\")\n", lexi[i]);
				i++;
				continue;
			}
			else if (lexi[i] == '+' || lexi[i] == '-' || lexi[i] == '*' || \
				lexi[i] == '/' || lexi[i] == '=' || lexi[i] == '>' || \
				lexi[i] == '<' || lexi[i] == '!'){
				i++;
				if (lexi[i + 1] == '='){
					i++;
				}
				flag = 4;
			}
			else if (lexi[i] >= 48 && lexi[i] <= 57){
				flag = 3;
				int j = 0;
				tmp[j] = lexi[i];
				i++; j++;
				while (1){
					if (lexi[i] >= 48 && lexi[i] <= 57){
						tmp[j] = lexi[i];
						i++; j++;
					}
					else{
						break;
					}
				}
				PRINT(flag, tmp);
				continue;
			}
			else if (lexi[i] >= 97 && lexi[i] <= 122){
				if (lexi[i] == 'i'){
					if (lexi[i + 1] == 'f' && lexi[i + 2] == '\0'){
						i += 2;
						flag = 1;
					}
					else if (lexi[i + 1] == 'n' && lexi[i + 2] == 't' && lexi[i + 3] == '\0'){
						i += 3;
						flag = 1;
					}
				}
				else if (lexi[i] == 'f' && lexi[i + 1] == 'o' && \
					lexi[i + 2] == 'r' && lexi[i + 3] && lexi[i + 4] == '\0'){
					i += 3;
					flag = 1;
				}
				else if (lexi[i] == 'd' && lexi[i + 1] == 'o' && \
					lexi[i + 2] == '\0'){
					i += 2;
					flag = 1;
				}
				else if (lexi[i] == 'w' && lexi[i + 1] == 'h' && \
					lexi[i + 2] == 'i' && lexi[i + 3] == 'l' && \
					lexi[i + 4] == 'e' && lexi[i + 5] == '\0'){
					i += 5;
					flag = 1;
				}
				else if (lexi[i] == 'r' && lexi[i + 1] == 'e' && \
					lexi[i + 2] == 't' && lexi[i + 3] == 'u' && \
					lexi[i + 4] == 'r' && lexi[i + 5] == 'n' && \
					lexi[i + 6] == '\0'){
					i += 6;
					flag = 1;
				}
				else if (lexi[i] == 'b' && lexi[i + 1] == 'r' && \
					lexi[i + 2] == 'e' && lexi[i + 3] == 'a' && \
					lexi[i + 4] == 'k'){
					flag = 1;
					i += 5;
					PRINT(flag, "break");
					continue;
				}
				else if (lexi[i] == 'c' && lexi[i + 1] == 'o' && \
					lexi[i + 2] == 'n' && lexi[i + 3] == 't' && \
					lexi[i + 4] == 'i' && lexi[i + 5] == 'n' && \
					lexi[i + 6] == 'u' && lexi[i + 7] == 'e'){
					i += 8;
					flag = 1;
					PRINT(flag, "continue");
					continue;
				}
				else{
					int j = 0;
					while (lexi[i] >= 97 && lexi[i] <= 122){
						tmp[j] = lexi[i];
						i++; j++;
					}
					flag = 2;
					PRINT(flag, tmp);
					continue;
				}
			}
			i++;
			PRINT(flag, lexi);
		}
	}	
	fclose(fp);

	system("pause");
	return 0;
}
读取文件如下:
int main()
{
	int a = 10;
	int b = 20;
	int c = 0;
	while (1){
		if (a > b)
		{
			break;
		}
		a += 1;
		c += a;
	}
}

结果如图:



猜你喜欢

转载自blog.csdn.net/tec_1535/article/details/80080960