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

📄 caoyutingdlg.cpp

📁 实现了简单的Simple语言的编辑器,包括词法分析,语法分析,生成四元式,以及生成符号表功能,在符号表中还可以进行查询与删除.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	CString str,st;
	int ErrorCount=0;
//	str.Format("输出格式:二元式序列(类型,值)单词\r\n\r\n");
	m_cOut.AppendText(str.GetBuffer(0));
	theApp.n=s;
	for (int i=0;i<s;i++)
	{   
		
		if (p[i]->type>0)
		{  
			if(p[i]->type>=3&&p[i]->type<16)
            str1="关键字";
			else if(p[i]->type<=37&&p[i]->type>=16)
            str1="运算符";
			else  
            str1="标识符";
	        if(p[i]->type==3) p[i]->type=22;
		    else if (p[i]->type==4) p[i]->type=31;
            else if (p[i]->type==5) p[i]->type=21; 
            else if (p[i]->type==6) p[i]->type=3; 
		    else if (p[i]->type==7) p[i]->type=11; 
		    else if (p[i]->type==8) p[i]->type=14; 
		    else if (p[i]->type==9) p[i]->type=27; 
		    else if (p[i]->type==10) p[i]->type=10; 
		    else if (p[i]->type==11) p[i]->type=21; 
		    else if (p[i]->type==12) p[i]->type=32; 
            else if (p[i]->type==13) p[i]->type=9; 
		    else if (p[i]->type==14) p[i]->type=16; 
		    else if (p[i]->type==15) p[i]->type=24; 
		    else if (p[i]->type==16) p[i]->type=43; 
		    else if (p[i]->type==17) p[i]->type=45; 
		    else if (p[i]->type==18) p[i]->type=41; 
		    else if (p[i]->type==19) p[i]->type=48; 
		    else if (p[i]->type==23) p[i]->type=53; 
		    else if (p[i]->type==24) p[i]->type=54; 
		    else if (p[i]->type==25) p[i]->type=57; 
		    else if (p[i]->type==26) p[i]->type=58; 
		    else if (p[i]->type==27) p[i]->type=56; 
		    else if (p[i]->type==28) p[i]->type=55; 
		    else if (p[i]->type==29) p[i]->type=51; 
		    else if (p[i]->type==30) p[i]->type=52; 
		    else if (p[i]->type==31) p[i]->type=46; 
		    else if (p[i]->type==32) p[i]->type=44; 
		    else if (p[i]->type==33) p[i]->type=39;
		    else if (p[i]->type==34) p[i]->type=40; 
            else if (p[i]->type==35) p[i]->type=49; 
            else if (p[i]->type==36) p[i]->type=42; 
			str.Format("(%d,%d)\t%s\r\n",p[i]->type,p[i]->value,p[i]->text);
			st.Format("%s       %d         %s",str1,p[i]->value,p[i]->text);
            theApp.nam[i]=p[i]->text;
			theApp.str[i]=st;
			m_cOut.AppendText(str.GetBuffer(0));
		}
		else
		{
			ErrorCount++;
			if (p[i]->value==2 && (p[i]->text[0]>126 || p[i]->text[0]<32))
			{
				str.Format("错误号:%d %s 【 无法显示该字符! 】\r\n",
					p[i]->value,ep[p[i]->value]);
				m_cOut.AppendText(str.GetBuffer(0));
			}
			else
			{
				str.Format("错误号:%d %s 【 %s 】\r\n",
					p[i]->value,ep[p[i]->value],p[i]->text);
				m_cOut.AppendText(str.GetBuffer(0));
			}
		}
	}
    UpdateData(true);
	
	str.Format("\r\n 发现 %d 个错误!\r\n",ErrorCount);
	m_cOut.AppendText(str.GetBuffer(0));
	
}

void CCaoyutingDlg::OnMenuitemYufa() 
{
	// TODO: Add your command handler code here
  char buf[30003];
	int sz=m_cIn.GetWindowText(buf,30000);
	buf[sz]=0;
	::strcpy(m_Compiler.GetTable()->SourseBuffer,buf);
	m_Compiler.CiFaFengXi();
	CifaResult * * cp=m_Compiler.GetTable()->LexicalDuad;
	int s=0;
	while ((cp[s]->type)>=0)
		s++;
	char (*ep)[100];
	ep=m_Compiler.GetTable()->ErrorMessageTable;
	for (int i=0;i<s;i++)
		if (cp[i]->type==0)
			if (AfxMessageBox("源程序有词法错误,仍要继续吗?"
				,MB_YESNO|MB_ICONQUESTION)==IDNO)
				return;
			else break;
	m_Compiler.YuFaFengXi();
	//result to : cp,s,ep
	m_cOut.SetWindowText("");
	CString str;
	str.Format("语法分析结果:\r\n\r\n");
	m_cOut.AppendText(str.GetBuffer(0));
	if (m_Compiler.GetTable()->ErrorNumber==0)
	{
		str.Format("源程序在语法上正确!\r\n");
		m_cOut.AppendText(str.GetBuffer(0));
	}
	else
	{
		str.Format("源程序有语法错误!!\r\n");
		m_cOut.AppendText(str.GetBuffer(0));
		m_cIn.SetSel(cp[m_Compiler.GetTable()->ErrorAddress]->address,
			cp[m_Compiler.GetTable()->ErrorAddress]->address
			+::strlen(cp[m_Compiler.GetTable()->ErrorAddress]->text));
		str.Format("\r\n错误号:%d %s\r\n",
			m_Compiler.GetTable()->ErrorNumber,
			ep[m_Compiler.GetTable()->ErrorNumber]);
		m_cOut.AppendText(str.GetBuffer(0));
	}
}

void CCaoyutingDlg::OnMenuitemYuyi() 
{
	// TODO: Add your command handler code here

		m_cIn.GetWindowText(m_Compiler.GetTable()->SourseBuffer,30000);
	m_Compiler.CiFaFengXi();
	m_Compiler.YuYiFengXi();
	m_cOut.SetWindowText("");
	int i=0;CString str;
	while (m_Compiler.GetTable()->LexicalDuad[i]->type>=0)
	{
		if (m_Compiler.GetTable()->LexicalDuad[i]->type==0)
		{
			str.Format("源程序有词法错误:\r\n\r\n");
			m_cOut.AppendText(str.GetBuffer(0));
			str.Format("错误号:%d %s\r\n",
				m_Compiler.GetTable()->LexicalDuad[i]->value,
				m_Compiler.GetTable()->ErrorMessageTable[m_Compiler.GetTable()->LexicalDuad[i]->value]);
			m_cOut.AppendText(str.GetBuffer(0));
			int se=m_Compiler.GetTable()->LexicalDuad[i]->address;
			int sl=::strlen(m_Compiler.GetTable()->LexicalDuad[i]->text);
			m_cIn.SetSel(se,se+sl);
			return;
		}
		i++;
	}
	str.Format("语义分析结果:\r\n\r\n");
	m_cOut.AppendText(str.GetBuffer(0));
	if (m_Compiler.GetTable()->ErrorNumber!=0)
	{
		str.Format("错误号:%d %s\r\n\r\n",
			m_Compiler.GetTable()->ErrorNumber,
			m_Compiler.GetTable()->ErrorMessageTable[m_Compiler.GetTable()->ErrorNumber]);
		m_cOut.AppendText(str.GetBuffer(0));
		int se=m_Compiler.GetTable()->LexicalDuad[m_Compiler.GetTable()->ErrorAddress]->address;
		int sl=::strlen(m_Compiler.GetTable()->LexicalDuad[m_Compiler.GetTable()->ErrorAddress]->text);
		m_cIn.SetSel(se,se+sl);
		if (AfxMessageBox("源程序有错误,要列出四元式吗?"
			,MB_YESNO|MB_ICONQUESTION)==IDNO)
			return;
	}
	/*if (!m_bSysEnable)
	{
		str.Format("输出格式:行号,四元式。\r\n\r\n");
		m_cOut.AppendText(str.GetBuffer(0));
		i=1;
		while (m_Compiler.GetTable()->SemanticQuad[i].op!=0)
		{
			str.Format("%d\t(%s\t,(%d,%d),(%d,%d),(%d,%d))\r\n",i,
				m_Compiler.GetTable()->QuadOpcodeTable[m_Compiler.GetTable()->SemanticQuad[i].op],
				m_Compiler.GetTable()->SemanticQuad[i].arg1.fl,
				m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue,
				m_Compiler.GetTable()->SemanticQuad[i].arg2.fl,
				m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue,
				m_Compiler.GetTable()->SemanticQuad[i].result.fl,
				m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);
			m_cOut.AppendText(str.GetBuffer(0));
			i++;
		}
	}
	else*/
	
	//	str.Format("输出格式:行号,四元式(操作,操作数,操作数,结果)。\r\n\r\n");
	//	m_cOut.AppendText(str.GetBuffer(0));
		i=1;
		while (m_Compiler.GetTable()->SemanticQuad[i].op!=0)
		{
			str.Format("%d\t(%s\t,",i,
				m_Compiler.GetTable()->QuadOpcodeTable[m_Compiler.GetTable()->SemanticQuad[i].op]);
			m_cOut.AppendText(str.GetBuffer(0));
			switch (m_Compiler.GetTable()->SemanticQuad[i].arg1.fl)
			{
			case 0:
				str.Format("__,");break;
			case 1:
				str.Format("%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
			case 2:
				str.Format("I%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
			case 3:
				str.Format("T%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
			case 4:
				str.Format("V%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
			case 5:
				str.Format("VV%d,",m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);break;
			default:
				str.Format("P%d_%d,",
					m_Compiler.GetTable()->SemanticQuad[i].arg1.fl-6,
					m_Compiler.GetTable()->SemanticQuad[i].arg1.flvalue);
			}
			m_cOut.AppendText(str.GetBuffer(0));
			switch (m_Compiler.GetTable()->SemanticQuad[i].arg2.fl)
			{
			case 0:
				str.Format("__,");break;
			case 1:
				str.Format("%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
			case 2:
				str.Format("I%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
			case 3:
				str.Format("T%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
			case 4:
				str.Format("V%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
			case 5:
				str.Format("VV%d,",m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);break;
			default:
				str.Format("P%d_%d,",
					m_Compiler.GetTable()->SemanticQuad[i].arg2.fl-6,
					m_Compiler.GetTable()->SemanticQuad[i].arg2.flvalue);
			}
			m_cOut.AppendText(str.GetBuffer(0));
			switch (m_Compiler.GetTable()->SemanticQuad[i].result.fl)
			{
			case 0:
				str.Format("__)\r\n");break;
			case 1:
				str.Format("%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
			case 2:
				str.Format("I%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
			case 3:
				str.Format("T%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
			case 4:
				str.Format("V%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
			case 5:
				str.Format("VV%d)\r\n",m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);break;
			default:
				str.Format("P%d_%d)\r\n",
					m_Compiler.GetTable()->SemanticQuad[i].result.fl-6,
					m_Compiler.GetTable()->SemanticQuad[i].result.flvalue);
			}
			m_cOut.AppendText(str.GetBuffer(0));
			i++;
		}
	//	str.Format("\r\n四元式说明:\r\n\r\n");
	//	m_cOut.AppendText(str.GetBuffer(0));
	//	str.Format("空:__\r\n常数:###\r\n指令入口:I###\r\n临时变量:T###\r\n直接访问变量:V###\r\n间接访问变量:VV###\r\n第i外层变量:Pi_###\r\n");
	//	m_cOut.AppendText(str.GetBuffer(0));
	//	str.Format("(###为数字)\r\n");
	//	m_cOut.AppendText(str.GetBuffer(0));
	
}

void CCaoyutingDlg::OnMenuitemXianshi() 
{
	// TODO: Add your command handler code here
	   CDialog1 dialog1;
	   dialog1.DoModal();
}

void CCaoyutingDlg::OnMenuitemSaveas() 
{
	// TODO: Add your command handler code here
	CFileDialog fdlg(FALSE,"pl0",NULL,OFN_OVERWRITEPROMPT,
		"源程序文件(*.txt)|*.txt|所有文件(*.*)|*.*||",this);
	fdlg.m_ofn.lpstrTitle="保存源程序文件";
	if (fdlg.DoModal()==IDOK)
	{
		m_strCurFile=fdlg.GetPathName();
		char buf[30001];
		int sz=m_cIn.GetWindowText(buf,30000);
		CFile f;
	    f.Open(m_strCurFile,CFile::modeWrite|CFile::modeCreate);
	    f.Write(buf,sz);
	    f.Close();
		m_cIn.SetModify(FALSE);
		char a[255];
		//::strcpy(a,"编译器  当前源文件:");
		::strcat(a,m_strCurFile.GetBuffer(0));
		SetWindowText(a);
	}
}

void CAboutDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

⌨️ 快捷键说明

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