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

📄 cifa.h

📁 编译原理
💻 H
字号:
//#ifndef LEXICAL_ANALYSIS//条件编译指令
//#define LEXICAL_ANALYSIS
//#pragma warning (disable : 4786)
#include<iostream>
#include<fstream>
#include<vector>
#include<map>
#include<math.h>
#include"change.h"
	using namespace std;
	const int WordNum=75;
	void init();//初始化单词表
	void PrintAll(ofstream &);//打印词法分析的结果
							  /*	本程序由于只是一个演示程序,所以仅分析C++语言中所有单词的一部分,关键字、界符、运算符均为
							  *	一符一种的分法;标识符的类型码为1,无符号数,分为整型、实型类型码分别为2、3。
							  *	即如下所示
							  *	界限符如下:
							  *	, . " : ; { } ( ) #  
							  *	 运算符如下:
							  *	+ - * / = % < <= > >= !=  << >>  && ||
	*	关键字如下:且类型码从1到WordNum*/
	string Word[WordNum]={"标识符","整型","实型",",",".","\'","\"",":",";","{","}","(",")",
		"[","]","+","-","#",
		"*","/","=","%","<","<=",">",">=","!=","!","==","<<",">>","&&","||","auto","break","case",
		"char","const","continue","default","do","double","else","enum",
		"extern","float","for","then","if","int","long","return","short",
		"signed","sizeof","static","struct","switch","typedef","union",
		"unsigned","void","while","class","new","delete","public","this",
		"private","protected","thow","template","inline","true","false"};
	map<string,int> WordMap;//存储所有单词和相应的类型码的图
	
	class Token//词法分析过程中每一个单词的分别的种类和类型节点
	{
	public:
		Token(){}
		Token(int type,string token):type(type),token(token){}//构造函数
		Token(string InToken)//构造函数
		{
			token=InToken;
			type=(WordMap.find (token))->second;
		}
		int GetType(){ return type;}
		string GetToken(){return token;}
	private:
		int type;//类型
		string token;//单词
	};
	vector<Token> AllToken;//语法分析的所有结果
	
	int main_lexical_analysis()
	{//建立输入输出文件流
		ifstream Input("input.txt",ios::in);
		if (!Input)
		{
			cerr<<"file not open !"<<endl;
			exit(1);
		}
		ofstream Output("LEXoutput.txt",ios::out);
		if (!Output)
		{
			cerr<<"file not open !"<<endl;
			exit(1);
		}
		//----------------------------------------------------------------------------
		init();//初始化单词表
		char ch;
		while(Input.get(ch))
		{
			if(ch>= 'a'&&ch<='z'||ch>= 'A'&&ch<='Z')//标识符和关键字词法分析子程序
			{
				string temp=toString(ch);
				Input.get(ch);
				while(ch>= 'a'&&ch<='z'||ch>= 'A'&&ch<='Z'||
					ch>='0'&&ch<='9')
				{
					temp= temp+ toString(ch);
					Input.get(ch);
				}
				if(WordMap.find(temp)!=WordMap.end() )//是关键字
				{
					AllToken.push_back(Token(temp ));
				}else//是标识符
				{
					AllToken.push_back(Token(1,temp ));
				}
				Input.putback(ch);//压回输入流		
			}else//----------------------------------------------------------------------------
				if(ch>='0'&&ch<='9')//无符号数词法分析子程序
				{
					long N=0,p=0;
					int j=0,e=1,numtype=2;
					N=fromString<long>(toString(ch));
					Input.get(ch);
					while(ch>='0'&&ch<='9')
					{
						N= N*10+ fromString<long>(toString(ch));
						Input.get(ch);
						numtype=2;//整型量标记
					}
					if(ch=='.')//判断是否为'.'
					{
						Input.get(ch);
						if(ch<'0'||ch>'9')
						{
							cerr<<"error after "<<toString(N)<<endl;
							exit(1);
						}
						else
						{
							do{
								N= N*10+ fromString<long>(toString(ch));
								j++;
								Input.get(ch);
							}while(ch>='0'&&ch<='9');
							numtype=3;//实型量标记
						}
					}
					if(ch=='e')
					{
						Input.get(ch);
						if(ch=='-')
						{
							e=-1;Input.get(ch);
						}
						else
							if(ch=='+')
							{
								Input.get(ch);
							}				
							if(ch<'0'||ch>'9')//其它
							{
								cerr<<"error after "<<toString(N)<<endl;
								exit(1);
							}
							else
							{
								do{
									p= p*10+ fromString<long>(toString(ch));
									Input.get(ch);
								}while(ch>='0'&&ch<='9');
								numtype=3;//实型量标记
							}				
					}
					double t=N*pow(10,e*p-j);
					AllToken.push_back(Token(numtype,toString(t) ));
					Input.putback(ch);
				}else
				{//----------------------------------------------------------------------------
					switch(ch)
					{
					case ' ': case 10: case 9://忽略每次分析前的空格、换行和TAB
						break;
					case ',':  case'.': case ':': case ';':case '\'':case '"':case '{':
					case '}': case '(': case ')':case '[': case ']':case '#'://界限符
						AllToken.push_back(Token(toString(ch) ));
						break;
					case '+': case'-' :case'*' :case'/' : case'%' :  //单个字符的运算符
						AllToken.push_back(Token(toString(ch) ));
						break;
					case '<': case '>':case '!':case '=':case '&':case'|'://运算符词法分析子程序< <= > >= !=
						{
							char temp = ch;
							Input.get(ch);
							if(ch=='=')
								AllToken.push_back( Token( toString(temp)+toString(ch) ) );
							else
								if( ch=='>' && temp=='>' || ch=='<' && temp=='<'||
									ch=='&' && temp=='&' || ch=='|' && temp=='|')
								{
									AllToken.push_back( Token( toString(temp)+toString(ch) ) );					
								}else
								{
									AllToken.push_back(Token(toString(temp) ));
									Input.putback(ch);//压回输入流
								}
								break;
						}
					default:
						cout.put(ch);
					}//switch end
				}//else end
	}
	PrintAll(Output);//打印词法分析的结果
	cout<<endl;
	cout<<"词法分析成功"<<endl<<endl;
	Input.close();
	Output.close();
    //system("PAUSE"); 
	return 0;
}
//----------------------------------------------------------------------------
void PrintAll(ofstream &Output)//打印词法分析的结果
{
	vector<Token>::iterator ibeg = AllToken.begin();
	for(;ibeg != AllToken.end();ibeg++)
	{
		Output<<(*ibeg).GetType()<<"\t,\t"<<(*ibeg).GetToken()<<endl;
	}
}
void init()//初始化单词表
{
	for(int i=0 ; i<WordNum; i++)
	{
		WordMap.insert(pair<string, int>(Word[i], i+1));
	}
}
#endif

⌨️ 快捷键说明

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