初探词法分析实验

本次实验使用C++对编译过程中的分词进行初步探究,以下是实验代码,输入文件需要在main函数中自己填写文本所在地址

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include<string>
#define M 20 
using namespace std;
string keyword[9] = { "main","if","int","for","while","do","return","break","continue" };
char arithmetic[4] = { '+','-','*','/' }; 
char relation[6] = { '=','>','<','>=','<=','!=' };
char border[6] = { ';',',','{','}','(',')' };

bool IsKeyword(string word) {
	for (int i = 0; i < 9; i++) {
		if (keyword[i] == word) {
			return true;
		}
	}
	return false;
}

bool IsArithmetic(char ch) {
	for (int i = 0; i < 4; i++) {
		if (arithmetic[i] == ch) {
			return true;
		}
	}
	return false;
}

bool IsRelation(char ch) {
	for (int i = 0; i < 6; i++) {
		if (relation[i] == ch) {
			return true;
		}
	}
	return false;
}

bool IsBorder(char ch) {
	for (int i = 0; i < 6; i++) {
		if (border[i] == ch) {
			return true;
		}
	}
	return false;
}

bool IsLetter(char ch) {
	if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') return true;
	return false;
}

bool IsDigit(char ch) {
	if (ch >= '0' && ch <= '9') return true;
	return false;
}

char DigitProcess(char ch, FILE* fp)
{
	int i = -1;
	char digit[M];
	while ((IsDigit(ch)))
	{
		digit[++i] = ch;
		ch = fgetc(fp);
	}
	digit[i + 1] = '\0';
	cout << "(3 , " << digit << " )" << "  常数" << endl;
	return(ch);
}

char AlphaProcess(char ch, FILE* fp)
{
	int i = -1;
	char alpha[M];
	while (IsLetter(ch) || (IsDigit(ch)))
	{
		alpha[++i] = ch;
		ch = fgetc(fp);
	}
	alpha[i + 1] = '\0';
	if (IsKeyword(alpha))
		cout << "(1 , " << alpha << " )" << "  关键字" << endl;
	else
		cout << "(2 , " << alpha << " )" << "  标识符" << endl;

	return(ch);
}

char OtherProcess(char ch, FILE* fp)
{
	int i = -1;
	char othertp[M];
	othertp[0] = ch;
	othertp[1] = '\0';
	if (IsArithmetic(ch))
	{
		cout << "(4 , " << othertp << " )" << "  运算符" << endl;
		ch = fgetc(fp);
	}
	if (IsRelation(ch))
	{
		ch = fgetc(fp);
		othertp[1] = ch;
		othertp[2] = '\0';
		if (IsRelation(ch))
			cout << "(4 , " << othertp << " )" << "  运算符" << endl;
		else
		{
			othertp[1] = '\0';
			cout << "(4 , " << othertp << " )" << "  运算符" << endl;
		}
	}
	if (IsBorder(ch))
	{
		cout << "(5 , " << othertp << " )" << "  分隔符" << endl;
		ch = fgetc(fp);
	}
	ch = fgetc(fp);
	return(ch);
}
int main()
{
	char ch;
	FILE* fp;
	if (fopen_s(&fp, "E:\\编译原理\\code\\main( ).txt", "r") != NULL) 
		cout << " 文件不存在 " << endl;
	else
	{
		ch = fgetc(fp);
		while (ch != EOF) 
		{
			if (IsLetter(ch)) 
				ch = AlphaProcess(ch, fp);
			else if (IsDigit(ch)) 
				ch = DigitProcess(ch, fp);
			else
				ch = OtherProcess(ch, fp);
		}
		cout << "end" << endl;
		getchar();
	}
	return 0;
}

运行截图:
初探词法分析实验_第1张图片

你可能感兴趣的:(编译原理,算法,编辑器,c++,汇编)