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

📄 analyzerdlg.cpp

📁 编译原理课程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			getChar();
			if(c_char=='<')
			{
				getChar();
				if(c_char=='=')
				{
					str2.Format("( <<=,-)");
					str+=str2;
				}
				else
				{
					retract();
					str2.Format("( <<,-)");
					str+=str2;
				}
			}
			else if(c_char=='=')
			{
				str2.Format("( <=,-)");
				str+=str2;
			}
			else
			{
				retract();
				str2.Format("( <,-)");
				str+=str2;
			}
		}
		else if(c_char=='>')
		{
			getChar();
			if(c_char=='>')
			{
				getChar();
				if(c_char=='=')
				{
					str2.Format("( >>=,-)");
					str+=str2;
				}
				else
				{
					retract();
					str2.Format("( >>,-)");
					str+=str2;
				}
			}
			else if(c_char=='=')
			{
				str2.Format("( <=,-)");
				str+=str2;
			}
			else
			{
				retract();
				str2.Format("( >,-)");
				str+=str2;
			}
		}
		else if(c_char=='=')
		{
			getChar();
			if(c_char=='=')
			{
				str2.Format("( ==,-)");
				str+=str2;
			}
			else
			{
				retract();
				str2.Format("( =,-)");
				str+=str2;
			}
		}
		else if(c_char=='^')
		{
			getChar();
			if(c_char=='=')
			{
				str2.Format("( ^=,-)");
				str+=str2;
			}
			else
			{
				retract();
				str2.Format("( ^,-)");
				str+=str2;
			}
		}
		else if(c_char=='|')
		{
			getChar();
			if(c_char=='|')
			{
				str2.Format("( ||,-)");
				str+=str2;
			}
			else if(c_char=='=')
			{
				str2.Format("( |=,-)");
				str+=str2;
			}
			else
			{
				retract();
				str2.Format("( |,-)");
				str+=str2;
			}
		}
		else if(c_char=='?')
		{
			str2.Format("( ?,-)");
			str+=str2;
		}
		else if(c_char==',')
		{
			str2.Format("( ,,-)");
			str+=str2;
		}
		else if(c_char=='#')
		{
			str2.Format("( #,-)");
			str+=str2;
		}
		else if(c_char=='"')
		{
			str2.Format("( \",-)");
			str+=str2;
		}
		else if(c_char==';')
		{
			str2.Format("( ;,-)");
			str+=str2;
		}
		else if(c_char=='\'')
		{
			str2.Format("( \',-)");
			str+=str2;
		}
		else if(c_char=='_')
		{
			str2.Format("( _,-)");
			str+=str2;
		}
		else
		{

			str2.Format("(error,%c )",c_char);
			str+=str2;
		}
				
	}


	int l=str.GetLength();
	int i,j=0;
	for(i=0;i<l;i++)
	{
		if(str[i]==')')
		{
			j++;
			m_Edit3+=str[i];
			while(j%10!=0)
			{
				j++;
				m_Edit3+=" ";
			}
			if(j%70==0)
			{
				m_Edit3+="\r\n";
			}

		}
		else
		{
			j++;
			m_Edit3+=str[i];
		}
	}

	UpdateData(false);
	str=s_str;
	s_str=s_str2;
	s_str2=str;
}



void CAnalyzerDlg::getChar()
{
	if(n_position<s_str.GetLength())
	c_char=s_str[n_position++];
}

/*消除本字符后面的所有间隔符 */
void CAnalyzerDlg::getBC()
{
	getChar();
	while(c_char==' ' || c_char== '\n' || c_char=='\t' )getChar();
	retract();
}

void CAnalyzerDlg::createTable()
{
	int i=1;
	keyWordTable[i].note="asm";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="auto";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="break";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="case";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="catch";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="char";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="class";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="const";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="continue";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="default";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="delete";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="do";
	keyWordTable[i++].value="-";

	keyWordTable[i].note="double";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="else";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="enum";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="extern";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="float";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="for";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="friend";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="goto";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="if";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="inline";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="int";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="long";
	keyWordTable[i++].value="-";

	keyWordTable[i].note="new";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="operator";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="private";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="protected";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="public";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="return";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="register";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="short";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="signed";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="sizeof";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="static";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="struck";
	keyWordTable[i++].value="-";

	keyWordTable[i].note="switch";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="template";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="this";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="throw";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="try";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="typedef";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="union";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="unsigned";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="virtual";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="void";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="volatile";
	keyWordTable[i++].value="-";
	keyWordTable[i].note="while";
	keyWordTable[i++].value="-";

	keyWordTable[i].note="ID";
	keyWordTable[i++].value="-";

	keyWordTable[i].note="CONST";
	keyWordTable[i++].value="-";



}

//将c_char中字符连接到strToken之后。
void CAnalyzerDlg::concat()
{
	strToken+=c_char;
}

bool CAnalyzerDlg::isLetter()
{
	if((c_char>='a'&&c_char<='z')||(c_char>='A'&&c_char<='Z'))
		return true;
	else return false;
}

bool CAnalyzerDlg::isDigit()
{
	if(c_char>='0'&&c_char<='9')
		return true;
	else return false;
}

int CAnalyzerDlg::isKey()
{
	int i;
	for(i=1;i<=48;i++)
	{
		if(strToken==keyWordTable[i].note)
			break;
	}
	if(i<=48)return i;
	else return 0;
}

void CAnalyzerDlg::retract()
{
	n_position--;
	c_char=0;
}

int CAnalyzerDlg::insertID()
{
	int i=0;
	while(i<IDTP)
	{
		if(IDTable[i].note!=strToken)
			i++;
		else
			break;
	}
	if(i<IDTP)
	{
		return i;
	}
	else
	{
		IDTable[IDTP].note=strToken;
		return IDTP++;
	}
}

int CAnalyzerDlg::insertConst()
{
	int tt=0;
	char ch;
	int i;
	int l=strToken.GetLength();
	for(i=0;i<l;i++)
	{
		ch=strToken[i];
		tt=tt*10+(ch-'0');
	}
	i=0;
	while(i<cTP)
	{
		if(	constTable[i]!=tt)
			i++;
		else
			break;
	}
	if(i<cTP)
		return i-1;
	else
	{
		constTable[cTP]=tt;
		return cTP++;
	}
}

void CAnalyzerDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	if(flag==0)
	{
		AfxMessageBox("尚未打开文件!");
		return;
	}
	if(flag==1)
	{
		AfxMessageBox("尚未预处理!");
		return;
	}
	if(flag==2)
	{
		AfxMessageBox("尚未进行词法分析!");
		return;
	}
	int result=createDlg();
}

int CAnalyzerDlg::createDlg()
{	
	return nDlg.DoModal();
}

CString CAnalyzerDlg::showKeyWord()
{
	CString str="";
	CString str2="";
	int i,j;
	char ch;
	for(i=1;i<=50;i++)
	{
		str2.Format("(%d,%s)",i,keyWordTable[i].note);
		str+=str2;
	}

	str2=str;
	str="";
	j=0;
	for(i=0;i<str2.GetLength();i++)
	{
		ch=str2[i];
		j++;
		str+=ch;
		if(ch==')')
		{
			while(j%20!=0)
			{
				j++;
				str+=" ";
			}
			if(j%100==0)
			{
				str+="\r\n";
			}
		}
	}
	return str;
}

CString CAnalyzerDlg::showID()
{
	int i=0;
	CString str,str2;
	str="";
	str2="";
	while(IDTable[i].note.GetLength()!=0&&i<200)
	{
		str2.Format("(%d,%s)",i,IDTable[i].note);
		str+=str2;
		i++;
	}
	str2=str;
	str="";
	int j=0;
	char ch;
	for(i=0;i<str2.GetLength();i++)
	{
		ch=str2[i];
		j++;
		str+=ch;
		if(ch==')')
		{
			while(j%20!=0)
			{
				j++;
				str+=" ";
			}
			if(j%100==0)
			{
				str+="\r\n";
			}
		}
	}
	return str;

}

CString CAnalyzerDlg::showConst()
{
	int i=0;
	CString str,str2;
	str="";
	str2="";
	while(constTable[i]!=-1&&i<300)
	{
		str2.Format("(%d,%d)",i,constTable[i]);
		str+=str2;
		i++;
	}
	str2=str;
	str="";
	int j=0;
	char ch;
	for(i=0;i<str2.GetLength();i++)
	{
		ch=str2[i];
		j++;
		str+=ch;
		if(ch==')')
		{
			while(j%20!=0)
			{
				j++;
				str+=" ";
			}
			if(j%100==0)
			{
				str+="\r\n";
			}
		}
	}
	return str;
}

void CAnalyzerDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
	if(flag==0)
	{
		AfxMessageBox("尚未打开文件!");
		return;
	}
	if(flag==1)
	{
		AfxMessageBox("尚未预处理!");
		return;
	}
	if(flag==2)
	{
		AfxMessageBox("尚未进行词法分析!");
		return;
	}
	CFileDialog dlg(FALSE, ".txt", "词法分析结果",
		OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,
		"*.txt|*.txt|所有文件|*.*||"); 
	CString FilePathName;
	if(dlg.DoModal()==IDOK)
	FilePathName=dlg.GetPathName();

	CString str;
	str = "";
	CStdioFile mFile;
	if(!mFile.Open(FilePathName,CStdioFile::modeCreate|CStdioFile::modeWrite,NULL))
	{   
    
		AfxMessageBox("保存文件失败!");   
    
		return;   
    
	}   
	str+="关键字表:\r\n";
	str+=showKeyWord();
	str+="\r\n\r\n\r\n标志符表:\r\n";
	str+=showID();
	str+="\r\n\r\n\r\n常数表:\r\n";
	str+=showConst();
	str+="\r\n\r\n\r\n词法分析结果:\r\n";
	str+=m_Edit3;
	mFile.WriteString(str);
	mFile.Close();
}

⌨️ 快捷键说明

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