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

📄 scan.cpp

📁 是编译原理的一个小程序
💻 CPP
字号:
#include"Init.h"
int lineno=1;
char ch;
char temp[100];
int j=0;
void scan(ifstream&fin,ofstream&fout)
{
	while(fin.get(ch))
	{
		if('\n'==ch)
			lineno++;
		else if('\t'==ch||' '==ch);
		else if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||'_'==ch)
		{
			while(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||ch>='0'&&ch<='9'||'_'==ch)
			{
				temp[j]=ch;
				j++;
				fin.get(ch);
			}
			temp[j]='\0';
			j=0;
			int value_judge;
			if(value_judge=judge(temp))
				fout<<temp<<"\t\t"<<value_judge<<endl;/*关键字*/
			else
				fout<<temp<<"\t\t\t33"<<endl;/*标识符*/
			fin.seekg(-1,ios::cur);
		}
		else if(ch>='0'&&ch<='9')/*整数*/
		{
			while(ch>='0'&&ch<='9')
			{
				temp[j]=ch;
				j++;
				fin.get(ch);
			}
			temp[j]='\0';
			j=0;
			fout<<temp<<"\t\t\t\t34"<<endl;
			fin.seekg(-1,ios::cur);
		}
		else if(' '==ch||'\t'==ch);/*消除制表*/
		else if('\n'==ch)lineno++;
		else if('/'==ch)/*消除注释*/
		{
			fin.get(ch);
			if(' '==ch&&'*'!=ch&&'='!=ch)
			{
				fout<<"/\t\t\t\t\t35"<<endl;
				fin.seekg(-1,ios::cur);
			}
			else
			{
				if('='==ch)
					fout<<"/=\t\t\t\t\t35"<<endl;
				else
				{
					fin.get(ch);
					int count=0;
					while(2!=count)
					{
						count=0;
						while('*'!=ch)
							fin.get(ch);
						count++;
						fin.get(ch);
						if('/'==ch)
							count++;	
						else	
							fin.get(ch);
					}	
				}
			}
		}
		else if('"'==ch)
		{
			fout<<ch<<"\t\t\t\t\t\t36"<<endl;
			fin.get(ch);
			while('"'!=ch)
				fin.get(ch);
			fout<<ch<<"\t\t\t\t\t\t36"<<endl;
		}
		else if((ch==' ')||('('==ch)||(')'==ch)||('{'==ch)||('}'==ch)||('['==ch)||(']'==ch)||('#'==ch)||(';'==ch)||('.'==ch)||('~'==ch)||(','==ch))
			fout<<ch<<"\t\t\t\t\t\t36"<<endl;
		else if('+'==ch)
		{
			char a=ch;
			fin.get(ch);
			if(' '==ch||'+'!=ch&&'='!=ch)
			{
				fout<<"+\t\t\t\t\t35"<<endl;
				fin.seekg(-1,ios::cur);
			}
			else
				fout<<a<<ch<<"\t\t\t\t\t35"<<endl;
		}
		else if('-'==ch)
		{
			char a=ch;
			fin.get(ch);
			if(' '==ch||'-'!=ch&&'='!=ch)
			{
				fout<<"-\t\t\t\t\t35"<<endl;
				fin.seekg(-1,ios::cur);
			}
			else
				fout<<"-"<<ch<<"\t\t\t\t\t35"<<endl;
		}
		else if('*'==ch)
		{
			fin.get(ch);
			if(' '==ch||'='!=ch)
			{
				fout<<"*\t\t\t\t\t35"<<endl;
				fin.seekg(-1,ios::cur);
			}
			else
				fout<<"*=\t\t\t\t\t35"<<endl;
		}
		else if('='==ch)
		{
			fin.get(ch);
			if('='==ch)
				fout<<"==\t\t\t\t\t35"<<endl;
			else
			{
				fout<<"=\t\t\t\t\t35"<<endl;
				fin.seekg(-1,ios::cur);
			}
		}
		else if('&'==ch)
		{
			fin.get(ch);
			if('&'==ch)
				fout<<"&&\t\t\t\t\t35"<<endl;
			else
			{
				fout<<"&\t\t\t\t\t35"<<endl;
				fin.seekg(-1,ios::cur);
			}
		}
		else if('!'==ch)
		{
			fin.get(ch);
			if('='==ch)
				fout<<"!=\t\t\t\t\t35"<<endl;
			else
			{
				fout<<"!\t\t\t\t\t\t36"<<endl;
				fin.seekg(-1,ios::cur);
			}
		}
		else if('<'==ch)
		{
			fin.get(ch);
			if('<'==ch)
				fout<<" <<\t\t\t\t\t35"<<endl;
			else if('='==ch)
				fout<<" <=\t\t\t\t\t35"<<endl;
			else
			{
				fout<<" <\t\t\t\t\t35"<<endl;
				fin.seekg(-1,ios::cur);
			}
		}
		else if('>'==ch)
		{
			fin.get(ch);
			if('>'==ch)
				fout<<" >\t\t\t\t\t35"<<endl;
			else if('='==ch)
				fout<<" >=\t\t\t\t\t35"<<endl;
			else
			{
				fout<<" >,\t\t\t\t\t35"<<endl;
				fin.seekg(-1,ios::cur);
			}
		}
		else if('|'==ch)
		{
			fin.get(ch);
			if('|'==ch)
				fout<<"||\t\t\t\t\t35"<<endl;
			else
				fin.seekg(-1,ios::cur);
		}
		else if(':'==ch)
		{
			fin.get(ch);
			if(':'==ch)
				fout<<"::\t\t\t\t\t35"<<endl;
			else
				fin.seekg(-1,ios::cur);//倒回到上一行
		}
		else
		{
			fout<<"error\t\t\t\t\t\t\t"<<ch<<" "<<lineno<<endl;
		}
	}
}

⌨️ 快捷键说明

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