⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 词法分析.cpp

📁 实现编译原理词法分析的c++程序源代码 实现编译原理词法分析的c++程序源代码
💻 CPP
字号:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
string tab,strToken,key[100];
char ch;
int index,dex;
void GetChar()
{
	ch=tab[index];
	index++;
}
void GetBC()
{
	if(ch==' ')
		while(tab[index]==' ')index++;	
}
void Concat()
{
	strToken[dex]=ch;
	dex++;
}
int type()
{
	if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')return 1;
	else if(ch>=1&&ch<=9)return 2;
	else return 0;
	
}
bool Reserve()
{
	int i=0;
	while(i<=41)
	{
		if(strToken==key[i])
		{
			i=-1;
			break;
		}
		i++;
	}
	if(i==-1) return true;
	return false;
}
void Retract()
{
	index--;
	ch=' ';
}
int main()
{
	fstream fin("F:\\s.txt");
	int i=0;
	while(fin>>key[i])i++;
	fstream din("F:\\d.txt");
	din>>tab;
	index=0;
	while(1)
	{

		while(1)
		{
			
			cout<<"Y\\N?"<<endl;
			if('N'==getchar())break;
			getchar();
			strToken="\0";
			dex=0;
			GetChar();
			if(type()==1)
			{
				int rgt=0;
				
				while(type()==1||type()==2)
				{
					
					strToken+=ch;
					dex++;
					GetChar();
					if(Reserve())
					{
						GetBC();
						if(ch==';')
						{
							cout<<"关键字:"<<strToken<<endl;
							rgt=1;
						}
						Retract();
						break;
					}	
				}
				if(rgt)break;
				cout<<"标志符: "<<strToken<<endl;
				Retract();
				continue;
			}
			else if(type()==2)
			{
				while(type()==2)
				{
					strToken[dex]=ch;
					dex++;
					GetChar();
				}
				cout<<"整数:"<<strToken<<endl;
				Retract();
				continue;
			}
			else
			{
				strToken+=ch;
				cout<<"其他字符:"<<strToken<<endl;
			}
		}
		din>>tab;
		index=0;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -