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

📄 cifafenxi.cpp

📁 新手编的一个简单的C++程序~~~实现了词法分析的功能
💻 CPP
字号:
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
char pd(char x)
{
	if(((x>='A')&&(x<='Z'))||((x>='a')&&(x<='z')))
	{
		return 'A';
	}
	else if((x>='0')&&(x<='9'))
	{
		return 'B';
	}
	else if(x=='{')
	{
		return 'C';
	}
	else if(x=='}')
	{
		return 'D';
	}
	else if(x=='+')
	{
		return 'E';
	}
	else if(x=='-')
	{
		return 'F';
	}
	else if(x=='*')
	{
		return 'G';
	}
	else if(x=='/')
	{
		return 'H';
	}
	else if(x==';')
	{
		return 'I';
	}
	else if(x=='(')
	{
		return 'J';
	}
	else if(x==')')
	{
		return 'K';
	}
	else if(x=='<')
	{
		return 'L';
	}
	else if(x=='=')
	{
		return 'M';
	}
	else if(x=='>')
	{
		return 'N';
	}
	else if(x==':')
	{
		return 'O';
	}
	else if(x==' ')
	{
		return 'P';
	}
	else if(x=='\n')
	{
		return 'Q';
	}
}
int main()
{
	 char wj[10000];
	 int i=0;
	 fstream outfile,infile;
	 infile.open("cx.txt",ios::in);//先把所要测试的程序以文本文挡命名为cx,放入程序所在文件夹中
	 if(!infile)
     {
         cout<<"不能打开文件\n";
         exit(0);
     }
	 infile.get(wj,10000,'#');//infile>>wj;
	 infile.close();
	 outfile.open("jg.txt",ios::out);
	 if(!outfile)
     {
         cout<<"不能打开文件\n";
         exit(0);
     }
	 outfile<<"1代表标示符;\n2代表整形数据;\n3代表保留字;\n4代表分界符(赋值号);\n5代表分界符(加号);\n6代表分界符(减号);\n7代表分界符(乘号);\n8代表分界符(除号);\n9代表分界符(分号);\n10代表分界符(前括号);\n11代表分界符(后括号);\n12代表分界符(小于号);\n13代表分界符(大于号);\n14代表分界符(不等于号);\n15代表分界符(大于等于);\n16代表分界符(小于等于);\n17代表分界符(等于);\n"<<endl;
	 while(wj[i])
	 {
		 switch(pd(wj[i]))
		 {
		 case 'A':
			 int j;
			 j=i;
			 i++;
			 while((pd(wj[i])=='A')||(pd(wj[i])=='B'))
			 {
				 i++;
			 }
			 int h,m,y;//n;
			 h=i-j;
			 char ls[50];
			 for(m=0;m<h;m++)
			 {
				 ls[m]=wj[j];
				 j++;
			 }
			 ls[h]=NULL;
			 /*char gjc[12][6]={{"begin"},{"end"},{"if"},{"then"},{"else"},{"for"},{"to"},{"do"},{"while"},{"not"},{"and"},{"or"}};
			 for(int n=0;n<12;n++)
			 {
				 int s=0,t=0;
				 while(gjc[n][s]&&ls[t])
				 {
					 if(gjc[n][s]==ls[t])
					 {
						 s++;
						 t++;
						 continue;
					 }
					 else
					 {
						 break;
					 }
				 }
				 if((t==h)&&(!(gjc[n][s])))
				 {
					 for(y=0;y<h;y++)
					 {
						 outfile<<ls[y];
					 }
					 outfile<<"       3"<<endl;
					 break;
				 }
				 else
				 {
					 continue;
				 }
			 }
			 if(n==12)
			 {
				 for(y=0;y<h;y++)
				 {
					 outfile<<ls[y];
				 }
				 outfile<<"       1"<<endl;
				 break;
			 }
			 else
			 {
				 break;
			 }**/
             /*char b1[6]={"begin"};
             char b2[4]={"end"};
             char b3[3]={"if"};
             char b4[5]={"then"};
             char b5[5]={"else"};
             char b6[4]={"for"};
             char b7[3]={"to"};
             char b8[3]={"do"};
             char b9[6]={"while"};
             char b10[4]={"not"};
             char b11[4]={"and"};
             char b12[3]={"or"};
             char *p[12]={b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12};
             for(int n=0;n<=11;n++)
             {
                 int x=0;
                 while(*(p[n])&&ls[x])
                 {
                      if(*(p[n])==ls[x])
                      {
                          (p[n])++;
                          x++;
                      }
                      else 
                      {
                          break;
                      }
                 }
                 if((!(*(p[n])))&&(!(ls[x])))
                 {
                     for(y=0;y<h;y++)
                     {
                         outfile<<ls[y];
                     }
                     outfile<<"      3"<<endl;
                     break;
                     }
                     else
                     {
                          continue;
                     }
                 }
                 if(n==12)
                 {**/
                     for(y=0;y<h;y++)
					 {
                          outfile<<ls[y];
                     }
                     outfile<<"      1"<<endl;
                     break;
				 /*}
                else
                 {
                     break;
                 }**/
          case 'B':
			  outfile<<wj[i];
              i++;
              while(pd(wj[i])=='B')
              {
                   outfile<<wj[i];
                   i++;
              }
              outfile<<"       2"<<endl;
              break;
          case 'C': 
			  //outfile<<wj[i];
              i++;
              while(pd(wj[i])!='D')
              {
                  //outfile<<wj[i];
                  i++;
              }
              //outfile<<wj[i]<<"        4"<<endl;
              i++;
              break;
           case 'E':
			   outfile<<wj[i]<<"         5"<<endl;
               i++;
               break;
           case 'F': 
			   outfile<<wj[i]<<"         6"<<endl;
               i++;
               break;
           case 'G':
			   outfile<<wj[i]<<"         7"<<endl;
               i++;
               break;
           case 'H':
			   outfile<<wj[i]<<"         8"<<endl;
               i++;
               break;    
           case 'I':
			   outfile<<wj[i]<<"         9"<<endl;
               i++;
               break; 
           case 'J':
			   outfile<<wj[i]<<"         10"<<endl;
               i++;
               break;  
           case 'K':
			   outfile<<wj[i]<<"         11"<<endl;
               i++;
               break;
           case 'L':
			   outfile<<wj[i];
               i++;
               if((pd(wj[i])=='M')||(pd(wj[i])=='N'))
               {
                    if(pd(wj[i])=='M')
                    {
                         outfile<<wj[i]<<"         16"<<endl;
                    }
                    else
                    {
                         outfile<<wj[i]<<"         14"<<endl;
                    }
                    i++;
               }
               else
               {
                   outfile<<"        12"<<endl;
               }
               break;
            case 'M':
				outfile<<wj[i]<<"         17"<<endl;
                i++;
                break;
            case 'N':
				outfile<<wj[i];
                i++;
                if(pd(wj[i])=='M')
                {
                    outfile<<wj[i]<<"          15"<<endl;
                    i++;
                }
                else
                {
                    outfile<<"         13"<<endl;
                }
                break;
            case 'O':
				outfile<<wj[i];
                i++;
				if(pd(wj[i])=='M')
				{
					outfile<<wj[i]<<"         4"<<endl;
                    i++;
                    break;
				}
				else
				{
					outfile<<"          illegal input"<<endl;
					break;
				}
            case 'P':
				//outfile<<wj[i];
                i++;
                while(pd(wj[i])=='P')
                {
                   // outfile<<wj[i];
                    i++;
                }
                //outfile<<"          19"<<endl;
                break;
			case 'Q':
                //outfile<<"Enter"<<endl;
				i++;
				break;
            default:
				outfile<<wj[i]<<"         illegal input;"<<endl;
				i++;
                break;
        }
    }
	outfile<<"program end"<<endl;
    outfile.close();
	return 0;
}
	

⌨️ 快捷键说明

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