• 初探词法分析实验


    本次实验使用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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144

    运行截图:
    在这里插入图片描述

  • 相关阅读:
    Vue3使用Svgaplayer进行.svga动画的播放
    SpringBoot整合RocketMq
    RK3568 Android11 编译报错
    QT生成ICO文件
    网络安全-典型的恶意代码
    物联网python开发实践
    [C++随想录] 模版进阶
    这是什么代码帮我看看
    上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花板
    【开源】给ChatGLM写个,Java对接的SDK
  • 原文地址:https://blog.csdn.net/m0_67587248/article/details/132717416