栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 实现 词法分析C语言

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 实现 词法分析C语言

代码共106行 短小精悍 但效果强大
大致思路:
①预处理 去除无效字符
②逐个分析 分情况讨论(重点)

#include
#include

using namespace std;

// 关键字表 
string table[48] = 
{
	"#", "main", "if", "then", "while", "do", "static", "int", "double", 
	"struct", "break", "else", "long", "switch", "case", "typedef", "char", "return", 
	"const", "float", "short", "continue", "for", "void", "sizeof", "", "", "+", 
	"-", "*", "/", "**", "==", "<", "<>", "<=", ">", 
	">=", "=", "[", "]", ";", "(", ")", "{", "}", "++", "--"
};

void fun(string s)  //判断 s 是否在关键字表中 
{
	for(int i = 0; i < 48; i ++)       //关键字 
		if(s == table[i])
		{
			cout << "(" << i << "," << s << ")" << endl;
			return ;
		}
	cout << "(25," << s << ")" << endl;  //标识符 
}

int main()
{
	FILE *fp;
	if((fp = fopen("C:/Users/yuyu/Desktop/test.txt","rt")) == NULL)
	{
		cout << "can not open the file!" << endl;
		return 0;
	}
	
	char ch;
	string  s0, s1; // s0为初始串,s1为预处理后的串 
	while((ch = fgetc(fp)) != EOF)
		s0 += ch;
	
	//预处理分为两步	
	//第一步:去注释、换行、回车、tab (转化为空格) 
	for(int i = 0; i < s0.length(); i ++)
	{
		if(s0[i] == '/' && s0[i + 1] == '/')
		{
			do s0[i] = ' ';
			while(s0[++i] != 'n');
		}
		if(s0[i] == 'n' || s0[i] == 't' || s0[i] == 'r')
			s0[i] = ' ';
	}
	
	//第二部:去空格	
	for(int i = 0; i < s0.length(); i ++)
	{
		if(s0[i] == ' ' && s0[i+1] == ' ')	continue;
		else	s1 += s0[i];
	}
	
	cout << "after processing :" << endl << s1 << endl;
	
	int i = 0;
	while(i < s1.length())
	{
		string s2; //储存中间结果的串 
		if(isalpha(s1[i])) // 字母 
		{
			while(isalpha(s1[i]) || isdigit(s1[i]))   //关键字或字母数字串(标识符)
				s2 += s1[i ++];
			fun(s2); 
		}
		else if(s1[i] == ' ')  // 空格 
		{
			i ++;
			continue;
		}
		else if(isdigit(s1[i]))  // 数字 
		{
			while(isdigit(s1[i]))	s2 += s1[i ++];
			if(isalpha(s1[i]))  // 数字  + 字母  非法  -1 
			{
				while(isalpha(s1[i]) || isdigit(s1[i]))
					s2 += s1[i ++];
				cout << "(-1," << s2 << ")" << endl;
			}
			else   // 数字串 
			{
				cout << "(26," << s2 << ")" << endl;
			}
		}
		else   //单个符号 
		{
			s2 += s1[i];
			if(s1[i+1] != ' ' && !isdigit(s1[i+1]) && !isalpha(s1[i+1])) // 两个符号 
			{
				if(s1[i] == s1[i + 1])			s2 += s1[i], i ++; // ++ -- ** ==
				else if(s1[i] == '>' && s1[i + 1] == '=')	s2 = ">=", i ++;
				else if(s1[i] == '<' && s1[i + 1] == '=')	s2 = "<=", i ++;
				else if(s1[i] == '<' && s1[i + 1] == '>')	s2 = "<>", i ++;
			}
			fun(s2);
			i ++;
		}
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/384323.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号