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

📄 projectdlg.cpp

📁 编译器包括词法分析器,语法分析器,最后生成四元式
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{

	if(!strcmp(token,"not"))
	{
	token=getnexttoken();
	bf(token);
	}
	else
		if(!strcmp(token,"("))
		{
			token=getnexttoken();
			bexp(token);
			next--;
			token=getnexttoken();
			if(token[0]!=')')
					{AfxMessageBox("错误:缺少 () !");}
		}
		else
		{
			fourslist[resent_fl].AG1=token;
			fourslist[resent_fl].no=resent_fl;
			saexpr(token);
			char *rop=getnexttoken();
			if(!strcmp(rop,">")
			 ||!strcmp(rop,">=")
			 ||!strcmp(rop,"<")
			 ||!strcmp(rop,"<=")
			 ||!strcmp(rop,"=")
			 ||!strcmp(rop,"<>"))
			{   
				if(fourslist[resent_fl-1].op!="")////////////解决 b>3 类型的简单判断式
                resent_fl++;
				fourslist[resent_fl].op=rop;
				CString temp;
				temp.Format("%d",resent_fl-1);
				fourslist[resent_fl].AG1="temp"+temp;
				aexpr(token);
			}
		}
}
////////////////////////////////计算算术表达式包括()
char *CProjectDlg::cal(char *token)
{
	if(isidentifier(token)
	 ||(token[0]>='0'&&token[0]<='9')
	 ||token[0]=='+'
	 ||token[0]=='-'
	 ||token[0]=='*'
	 ||token[0]=='/'
	 ||token[0]=='('
	 ||token[0]==')'
	 )
	{
		if(isidentifier(token)||(token[0]>='0'&&token[0]<='9'))
		fourslist[resent_fl].AG1=token;


		token=getnexttoken();
		if(token[0]=='+'
		||token[0]=='-'
		||token[0]=='*'
		||token[0]=='/')
		fourslist[resent_fl].op=token;

		if(tokenfile_char[next][0]=='('
		||tokenfile_char[next+1][0]=='+'
		||tokenfile_char[next+1][0]=='-'
		||tokenfile_char[next+1][0]=='*'
		||tokenfile_char[next+1][0]=='/')
		{
		CString temp;
		temp.Format("%d",resent_fl+1);
		fourslist[resent_fl].AG2="temp"+temp;
		if(fourslist[resent_fl].RESULT=="")
		{
        temp.Format("%d",resent_fl);
		fourslist[resent_fl].RESULT="temp"+temp;
		}
        fourslist[resent_fl].no=resent_fl;
		resent_fl++;
		}
		token=tokenfile_char[next-1];
		token=cal(token);
	}
	else
		return token;
}
////////////////////////////////算术运算判定
char *CProjectDlg::aexpr(char *token)
{
	if(isidentifier(token)
	 ||(token[0]>='0'&&token[0]<='9')
	 ||token[0]=='+'
	 ||token[0]=='-'
	 ||token[0]=='*'
	 ||token[0]=='/'
	 )
	{
		token=getnexttoken();
		if(token[0]!=')')
		if(fourslist[resent_fl].op==">"||fourslist[resent_fl].op==">="||fourslist[resent_fl].op=="<"||fourslist[resent_fl].op=="<="||fourslist[resent_fl].op=="<>")
		{
			CString temp;
			temp.Format("%d",resent_fl-1);
			fourslist[resent_fl-1].RESULT="temp"+temp;
		}
		if(isidentifier(token)||(token[0]>='0'&&token[0]<='9'))
			////////////////////////// 解决 if a+3 > d 中 >  符号的放在AG2的问题
		    if(fourslist[resent_fl].op==""||fourslist[resent_fl].op==">"||fourslist[resent_fl].op==">="||fourslist[resent_fl].op=="<"||fourslist[resent_fl].op=="<="||fourslist[resent_fl].op=="<>")
			{fourslist[resent_fl].AG2=token;fourslist[resent_fl].no=resent_fl;}
			else
			fourslist[resent_fl].AG1=token;
		if(token[0]==')'||token[0]==';')
		{   CString temp;
		    temp.Format("%d",resent_fl); 
			fourslist[resent_fl].RESULT="temp"+temp;
            resent_fl++;
		}
		if(token[0]=='+'||token[0]=='-'||token[0]=='*'||token[0]=='/')
		{   
			fourslist[resent_fl].op=token;
			///////////////////////////    判断 后面是否有 运算符
		    if(tokenfile_char[next+1][0]=='+'||tokenfile_char[next+1][0]=='-'||tokenfile_char[next+1][0]=='*'||tokenfile_char[next+1][0]=='/')
			{
                CString temp;
		        temp.Format("%d",resent_fl); 
				fourslist[resent_fl].RESULT="temp"+temp;
		        temp.Format("%d",resent_fl+1); 
                fourslist[resent_fl].AG2="temp"+temp;
                fourslist[resent_fl].op=token;
				resent_fl++;
                fourslist[resent_fl].RESULT="temp"+temp;
            }
		}
		aexpr(token);
	}
	else
		return token;
}
///////////////////////////////////////////
void CProjectDlg::saexpr(char *token)  //将特殊的算术运算表达式退格
{
    aexpr(token);
	next--;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void CProjectDlg::Onread() 
{
	char word[7]="\0";
	 UpdateData(true);
	 m_content="";
	 UpdateData(false);
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Text Files (*.txt;*.src)|*.txt;*.src");//设置读取的文件类型
	if(dlg.DoModal() == IDOK)
	{
		m_strFileNamePath = dlg.GetPathName();  //取文件的路径
		m_FileInput.open((char*)m_strFileNamePath.GetBuffer(0),ios::nocreate);//得到文件的带路径的文件名

		{
			char *OpenFileOut="\r\n";
			m_strTargetFileContent = OpenFileOut;


			char str[41] = "\0",str1[20]="";
            int k=0;
			//读入一行,或者最大30个字符
			while(!m_FileInput.eof()) //没有结束继续读
			{   
				m_FileInput.getline(str,40,'\n');
                while(k<40)
				{
					if(str[k]=='/'&&str[k+1]=='*')
					{
                        if(str[k]!='*'&&str[k+1]!='/')
							 str[k]=0;
					    else
						{
							 str[k]=0;
						     str[k+1]=0;
							 k=0;
						     break;
						}
					}
					k++;
				}
			    k=0;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////当前行读入 str/////////////////////////////////////////////////////////////////////
          int j=0,FLAG=0;
	      int row=0,cloumm=0;
		  int token,strlenth=strlen(str);
		  while(cloumm<strlenth)
			{strlenth=strlen(str);////////////////////////计算当前串长度
		       while(str[cloumm]==32)
			   {cloumm++;}///////////////////////////跳过空格
			   while(!(FLAG=isdelimeter(CString(str[cloumm]),delimeter))&&str[cloumm]!=32&&str[cloumm]!=0)/////while_2
			   {
				   if(j>6)
				   {
				   j=0; break;	 
				   }
                   word[j]=str[cloumm];/////////////////从句子中取单词
				   j++;
				   cloumm++;
			   }//for while_2
				word[j]='\0';
				if(str[cloumm]==32||str[cloumm]==0)
					cloumm++;
				j=0;
				
//////////////////////////////////////////// 标识符 和 关键字 识别//////////////////////////////////////////////////////
			    if(word[0]>='A'&&word[0]<='Z'||word[0]>='a'&&word[0]<='z')
					if(token=charsort(word))
					{tokenfile+=word;tokenfile+="\r\n";///////////////加入TOKEN文件
					 if(token>33&&token<38)
                     {                 
					  char *temp=transvert(token);
				      char tem[10];
				      for(int i=0;*(temp+i)!='\0';i++)
					        tem[i]=*(temp+i);
				             tem[i]='\0';           /////////////加入符号表
					 charfile+="(";charfile+=tem;charfile+=",";charfile+=word;charfile+=")";charfile+="\r\n";
					 }// for if(token>33&&token<38)
					}// for if(token=charsort(word))
///////////////////////////////////////////// 数值 类型 识别 ////////////////////////////////////////////////////////////
				if(word[0]>='0'&&word[0]<='9')
				{tokenfile+=word;tokenfile+="\r\n";///////////////加入TOKEN文件
                    char *temp=transvert(digitsort(word));
				    char tem[10];
				    for(int i=0;*(temp+i)!='\0';i++)
					        tem[i]=*(temp+i);
				    tem[i]='\0';        /////////////加入符号表
				    charfile+="(";charfile+=tem;charfile+=",";charfile+=word;charfile+=")";charfile+="\r\n";
				}// for if(word[0]>='0'&&word[0]<='9')
                if(FLAG)
				{
				if(str[cloumm]!=39)/////////////////识别 ' 符号
				{
				   if(str[cloumm+1]=='='||str[cloumm+1]=='>')////////////双界符识别
				   {
				   tokenfile+=CString(str[cloumm]);tokenfile+=CString(str[cloumm+1]);tokenfile+="\r\n";cloumm+=2;
				   }
				   else/////////////单界符识别
                   {tokenfile+=CString(str[cloumm]);tokenfile+="\r\n";cloumm++;}
				   if(str[cloumm+1]=='*'||str[cloumm+1]=='/')
				   {}
				}//for if(str[i]!=39)
				else////////////////// 字符常量识别
				{
				  char sentence[30]="\0";
				  for(j=cloumm+1;str[j]!=39&&str[j]!=0;j++)
                      sentence[j-cloumm-1]=str[j];
				  if(str[j]==0)
					  AfxMessageBox(" ' 符号不匹配");
				  else
				  {
				  sentence[j]='\0';
				  tokenfile+=sentence;tokenfile+="\r\n";
				  cloumm=j+1;
				  }
				}
				}// for if(FLAG)
		  }//for while(i<strlenth)
//////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////
				UpdateData(true);
		        m_content +=str;
				m_content +="\r\n";
				UpdateData(false);
				row++;
				//读入的内容加到对应源文件的变量最后
			}	
		}
	}
}

void CProjectDlg::OnWordAnalyse() 
{
//////////////////////////////////////////////////////////////////
char temp[200][13];
int i=0,j=0,k=0;
int strlenth=strlen(charfile);
while(k<strlenth)
{  
	if(charfile[k]!=')')
	{ 
		temp[i][j]=charfile[k];
		j++;
		k++;
	}
	else
	{ 
		temp[i][j]=')';
		j++;
		temp[i][j]='\0';
		i++;
		j=0;
		k++;  
	}
}
i++;
temp[i][0]='\0';
int n=0;
while(n<7)
{
	{
for(i=0;temp[i][0]!='\0';i++)
  for(j=i+1;temp[j][0]!='\0';j++)
	  if(!strcmp(temp[i],temp[j]))
		  for(k=j+1;temp[k][0]!='\0';k++)
			  strcpy(temp[k-1],temp[k]);
		  }
n++;
}
temp[i-1][0]='\0';
n=0;
for(i=0;temp[i][0]=='('||temp[i][2]=='(';i++)
{
    if(n==0)
	{
	  if(temp[i][4]>='a'&&temp[i][4]<='z')
	  {
	   for(j=4;temp[i][j]!=')';j++)
	   {
         charfile_char[n][j-4]=temp[i][j];
	   }
	   n++;
	  }
	}
	else
	  if(temp[i][6]>='a'&&temp[i][6]<='z')
	  {
	   for(j=6;temp[i][j]!=')';j++)
	   {
         charfile_char[n][j-6]=temp[i][j];
	   }
	   n++;
	  }
}
charfile_char[i-1][0]='\0';
charfile="\0";
for(i=0;temp[i][0]!='\0';i++)
   charfile+=temp[i];

/////////////////////////////////////////////////////////////////
UpdateData(true);
m_TOKEN=tokenfile;
m_CHAR=charfile;
UpdateData(false);	
////////////////////////////////////////////////////////////////
  i=0;j=0;k=0;
  strlenth=strlen(tokenfile);
  char c=tokenfile[k];
  while(k<strlenth)
  {  c=tokenfile[k];
	if(tokenfile[k]!=13)
	{   tokenfile_char[i][j]=tokenfile[k];j++;k++;  }
	else
	{   tokenfile_char[i][j]='\0';i++;j=0;k++;k++;    }
  }
tokenfile_char[i][0]='\0';
}

void CProjectDlg::OnWordSave() 
{
	// TODO: Add your command handler code here

	CString SaveFilename,SaveFilename2;
	CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Text Files (*.txt;*.src)|*.txt;*.src");//设置写入文件的类型
 	if(dlg.DoModal() == IDOK)
	{
	   SaveFilename = dlg.GetPathName();
		 m_FileOutput.open((char *)SaveFilename.GetBuffer(0));
		 if((m_FileOutput.fail()))
		 {
			 AfxMessageBox("保存文件失败");
             UpdateData(false);
		 }
		 else
		 {
		    m_FileOutput<<m_TOKEN;
		    m_FileOutput.close();
  	        AfxMessageBox("文件已成功保存");
           /* m_SaveFileName = _T("");
            m_TOKEN = _T("");*/   //清空编辑框中的内容
		    UpdateData(false);
		 }
	}

}

void CProjectDlg::OnGrammerAnalyse() 
{
////////////////////////////////////文件头 判定//////////////////////////////////////////
char *token=getnexttoken();
char *end="\0";
if(!strcmp(token,"program"))
{    
	fourslist[resent_fl].no=resent_fl;
    fourslist[resent_fl].op=token;
     if(isidentifier(token=getnexttoken()))
		 fourslist[resent_fl].AG1=token;
     else
	 {
	  AfxMessageBox("该源程序命名有误!");
	  return;
	 }
}
  else
  {
	AfxMessageBox("该源程序不是一个 Sample 程序!");
    return;
  }
  if(strcmp(token=getnexttoken(),";"))
  {AfxMessageBox("缺少' ; '!");return;}
  else
  resent_fl++;
/////////////////////////////////// 常量 判定///////////////////////////////////////////
  token=getnexttoken();
  if(!strcmp(token,"const"))
  {
  token=getnexttoken();
  }
  if(!strcmp(token,"var"))
  {
  token=getnexttoken();
  varst(token);
  }
////////////////////////////////////
  next--;
  token=getnexttoken();
  if(!strcmp(token,"begin"))
  {
	  token=getnexttoken();
	  while(strcmp(end,"."))
	  {
	  if(token[0]==';')
	     token=getnexttoken();
	  token=ST_SORT(token);
	  end=tokenfile_char[next];}
  }
}

void CProjectDlg::OnGrammerSave() 
{
	// TODO: Add your command handler code here
	CString SaveFilename;
	CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Text Files (*.txt;*.src)|*.txt;*.src");//设置写入文件的类型
 	if(dlg.DoModal() == IDOK)
	{
	   SaveFilename = dlg.GetPathName();
		 m_FileOutput.open((char *)SaveFilename.GetBuffer(0));
		 if((m_FileOutput.fail()))
		 {
			 AfxMessageBox("保存文件失败");
             UpdateData(false);
		 }
		 else
		 {
		    m_FileOutput<<m_CHAR;
		    m_FileOutput.close();
  	        AfxMessageBox("文件已成功保存");
           /* m_SaveFileName = _T("");
            m_TOKEN = _T("");*/   //清空编辑框中的内容
		    UpdateData(false);
		 }
	}

	
}

void CProjectDlg::OnChangeEdit1() 
{
UpdateData(true);
UpdateData(false);	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CProjectDlg::Ond() 
{
CDia d;
d.DoModal();	
}

void CProjectDlg::OnS() 
{
CDia2 s;
s.DoModal();	
}

void CProjectDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == 2)
	{
		CenterWindow();
		CRect OriginalRect;
		GetWindowRect(&OriginalRect);
		if((OriginalRect.bottom-OriginalRect.top) < (m_sizeWin.bottom-m_sizeWin.top))
		{
			CRect NowRect(CPoint(OriginalRect.left,OriginalRect.top),CPoint(OriginalRect.right,OriginalRect.bottom+m_nAdd));
			MoveWindow(NowRect);
		}
		else
			KillTimer(2);
	}
	CDialog::OnTimer(nIDEvent);
}

void CProjectDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if(!m_bIsReSize)
	{
		CenterWindow();
		GetWindowRect(&m_sizeWin);
		CRect rect(CPoint(m_sizeWin.left,m_sizeWin.top),CPoint(m_sizeWin.right,30));
		m_bIsReSize = TRUE;
		MoveWindow(rect);
	}
}

⌨️ 快捷键说明

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