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

📄 cifafenxi.cpp

📁 此程序用来完成编译原理的PL/0的词法分析程序
💻 CPP
字号:
#include<iostream>
#include<string>
#include<fstream>
#include<cstddef>
using namespace std;
string key_word[45]={"const","begin","do","if","then","else","end","while","read","write","odd",
"call","var","procedure","for","goto","switch","break","continue","include","bool","catch","bad",
"cast","struct","void","signed","static","sizeof","typedef","case","default","auto","extern","register",
"enum","int","float","long","short","char","union","double","unsigned","return"};
ifstream& open_file(ifstream &in, const string &file)
      {
        in.close();     
        in.clear();     
        in.open(file.c_str()); 
        return in; 
       }
//关键字,自定义的标识符,数字,错误的标识符,其他的符号  
int number;
int p,sum,aller=20;
int stringlength;
string lower_word( string str)//把所有的字母都转换成小写的形式 
{
    string str1(str);
    for(int i=0;i<str.length();i++)
        str1[i]=tolower(str1[i]);
    return str1;
}
void getsym(string str)
{
    int w,i=0;
    char ch;
    char next_word;
    char array[50]={0};
    string str2(str);
    ch=str2[i];
    while(ch==' ')
    {
        if(p==stringlength-1)
            exit(1); //非正常的退出
        else
        {     
        ch=str2[++i];
          p++;
        }
    }
   if ((ch>='a'&&ch<='z')||ch=='_')
    {
        w=-1;
      do
        {
            if(w<aller)
            {
                w=w+1;
                array[w]=ch;
            }
            else
            {   
                cout<<"      您的输入已经结束! ";
                cout<<endl; 
                system("pause");
                 exit(1);
                }
           ch=str2[++i];
            p++;
        }while((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9')||ch=='_');
       for(sum=0;sum<45;sum++)
        {
            if(key_word[sum]==array)
                break;
        }
        if (sum<45)
        {
        
            cout<<key_word[sum].c_str()<<"      您输入的是关键字"; 
            cout<<endl;
        }
        else
        {   
    
            cout<<array<<"      您输入的是标识符";
            cout<<endl;
        }
        return;
    }
    else 
       if(ch>='0'&&ch<='9')
        {  
             w=0;
            number=0;
            do
            {    array[w]=ch; 
                w=w+1;
               number=10*number+(int(ch)-int('0'));//变换成真正的数字,代表两位数的表示方法
                ch=str2[++i];
                p++;
                next_word=str2[i];
            }while(ch>='0'&&ch<='9');
            if (number>32767)
              {  
                  
                cout<<"      您输入的数字太大了,超出了表示的范围!";
                cout<<endl;
                exit(1);     //非正常退出 
         
         
              }     
        if(next_word>='a'&&next_word<='z')
         {  
                do{
                   array[w]=next_word;
                    next_word=str2[++i];
                    w++;
                    p++;
                }while((next_word>='a'&&next_word<='z')||(next_word>='0'&&next_word<='9'));

                cout<<array<<"      您输入的是错误的标识符!";
                cout<<endl;
            
                system("pause");
        
            }
            else
            {
                cout<<array<<"       您输入的是数字";
                cout<<endl;
            }
            return;
        }
        else
            if(ch=='!')
            {
                ch=str2[++i];
                p++;
                if(ch=='=')
                {
                   ch=str2[++i];
                    p++;
                    cout<<"!=          您输入的是不等于符号";
                    cout<<endl;
                }
                else
                {
        
                    cout<<"!           您输入的是错误的符号";
                    cout<<endl;
                   
                }
                return;
            }
        //else
            if(ch==':')
            {
                ch=str2[++i];
                p++;
                if(ch=='=')
                {
                   ch=str2[++i];
                    p++;
                    cout<<":=          您输入的是赋值符号";
                    cout<<endl;
                }
                else
                {
        
                    cout<<":              您输入的是冒号";
                    cout<<endl;
                   
                }
                return;
            }
            else
            
                if(ch=='<')
                {
                    ch=str2[++i];
                    p++;
                    if(ch=='=')
                    {
                       ch=str2[++i];
                        p++;
                        cout<<"<=            您输入的是小于等于";
                        cout<<endl;
                    }
                    else
                    {
            
                     cout<<"<           您输入的是小于";
                     cout<<endl;
                    }
                    return;
                }
                else
                {
                    if(ch=='>')
                    {
                        ch=str2[++i];
                        p++;
                        if(ch=='=')
                        {
                        ch=str2[++i];
                            p++;
                            cout<<">=          您输入的是大于等于";
                            cout<<endl;
                        }
                        else
                        {
                        
                            cout<<">          您输入的是大于";
                            cout<<endl;
                        }
                        return;

                    }
                    else
                    {   
                         switch(ch)
                       {

          case ';':cout<<ch<<"        您输入的符号为分号!"<<endl;
          break;
          case ')':cout<<ch<<"        您输入的符号为右括号!"<<endl;
          break;
          case '=':cout<<ch<<"        您输入的符号为等号!"<<endl;
          break;
          case ',':cout<<ch<<"        您输入的符号为逗号!"<<endl;
          break;
          case '.':cout<<ch<<"        您输入的符号为点号!"<<endl;
          break;
          case '#':cout<<ch<<"        您输入的符号为不等号!"<<endl;
          break;
          case '+':cout<<ch<<"        您输入的符号为加号!"<<endl;
          break;
          case '-':cout<<ch<<"        您输入的符号为减号!"<<endl;
          break; 
          case '*':cout<<ch<<"        您输入的符号为乘号!"<<endl;
          break;
          case '/':cout<<ch<<"        您输入的符号为斜线!"<<endl;
          break;
          case '(':cout<<ch<<"        您输入的符号为左括号!"<<endl;
          break;
          case '[':cout<<ch<<"        您输入的符号为左中括号!"<<endl;
          break;
          case ']':cout<<ch<<"        您输入的符号为右中括号!"<<endl;
          break;
          case '{':cout<<ch<<"        您输入的符号为左大括号!"<<endl;
          break;
          case '}':cout<<ch<<"        您输入的符号为右大括号!"<<endl;
          break;
          case '"':cout<<ch<<"        您输入的符号为双引号!"<<endl;
          break;
          //case '"':cout<<ch<<"      您输入的符号为右双引号!"<<endl;
          //break; 
          
          default: cout<<ch<<     "您输入符号的是错误符号:";
                   cout<<endl;
            
                    }       
                        ch=str2[++i];
                        p++;
                   }
                }
            return;
    }
 int main(int argc,char **argv)  //传递命令行的参数 
    {
         string  str0;
         char ch1;
         //ifstream infile();
         ifstream infile;
         //ifstream infile(argv[1]);
         if(argc<2||!open_file(infile,argv[1])){
        cerr<<"No input file!"<<endl;
        return EXIT_FAILURE;
        }
          while(infile)
         {
             infile.get(ch1);
             if(ch1=='\n')
                 ch1=' ';
                 str0+=ch1;
         }
          infile.close();
         string str4(str0);
        stringlength=str4.length();
        
        str4=lower_word(str4);
         p=0;
       while(p<str4.length())
    {
        
        getsym(str4.substr(p,str4.length()));
    }
    system("pause");
    return 0;
 }

⌨️ 快捷键说明

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