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

📄 unit1.~cpp

📁 针对C++的简单词法分析器,局限性较大,算法简单,易懂,导入词法文件,输出分析结果
💻 ~CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{    for(int i=0;i<=N;i++)
     {label[i]=NULL;
       consts[i]=NULL;}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
OpenDialog1->Filter="文本文档(*.txt)|*.TXT|C文件(*.c)|*.C|C++文件(*.cpp)|*.CPP";
if(OpenDialog1->Execute())
{Memo1->Lines->LoadFromFile(OpenDialog1->FileName);}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
SaveDialog1->Filter="文本文档(*.txt)|*.TXT|C文件(*.c)|*.C|C++文件(*.cpp)|*.CPP";
if(SaveDialog1->Execute())
{Memo2->Lines->SaveToFile(SaveDialog1->FileName);}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{if(Memo1->Lines->Count==0)
 {
  ShowMessage("There's no code in!");
 }
 else{
        Memo2->Lines->Clear();

         for(line=0;line<Memo1->Lines->Count;line++)
         {char* buffer; bufnum=0;
          buffer=new char[Memo1->Lines->Strings[line].Length()+1];
          strcpy(buffer,Memo1->Lines->Strings[line].c_str());
              char buf=buffer[0];

          
               while(bufnum+1<=Memo1->Lines->Strings[line].Length())
                { if(buf==' '){buf=buffer[++bufnum]; }
                 if(Isletter(buf))
                  buf=alphaprocess(buf,buffer);
                 else if(IsDigit(buf))
                     buf=digitprocess(buf,buffer);
                 else buf=otherprocess(buf,buffer);}

                 }

 }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Memo1->Lines->Clear();
Memo2->Lines->Clear();
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Isletter(char ch)
{  if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')return 1;
   return 0;
   }
//--------------------------------------------------------------------------
int __fastcall TForm1::IsDigit(char ch)
{  if(ch>='0'&&ch<='9')return 1;
   return 0;
   }
//---------------------------------------------------------------------------
int __fastcall TForm1::search(char searchchar[],int wordtype)
{  int i=0;
   switch(wordtype)
    {   case 1: for(i=0;i<=7;i++)
                 {if(strcmp(key[i],searchchar)==0)
                     return(i+1);
                     }
        case 2:{for(i=0;i<=5;i++)
                 { if(strcmp(border[i],searchchar)==0)
                      return (i+1);}
                      return (0);
                      }
        case 3:{for(i=0;i<=3;i++)
                 {if(strcmp(arithmetic[i],searchchar)==0)
                    {return (i+1);
                    }
                    }
                    return (0);
                    }
        case 4:{for(i=0;i<=5;i++)
                 {   if(strcmp(relation[i],searchchar)==0)
                       {return (i+1);
                       }
                       }
                       return (0);
                       }
        case 5:{for(i=0;i<=constnum;i++)
                {  if(strcmp(consts[i],searchchar)==0)
                     {return (i+1);
                     }
                }
                consts[i-1]=(char *)malloc(sizeof(searchchar));
                strcpy(consts[i-1],searchchar);
                constnum++;
                return(i);
                }
        case 6:{for(i=0;i<labelnum;i++)
                 { if(label[i]!=NULL){
                    if(strcmp(label[i],searchchar)==0)
                       return (i+1);
                       }
                       }
                       label[i-1]=(char *)malloc(sizeof(searchchar));
                       strcpy(label[i-1],searchchar);
                       labelnum++;
                       return (i);
                       }
                       }
                       return -1;}
//-------------------------------------------------------------------------------

char __fastcall TForm1::alphaprocess(char buffer,char *bufs)
{ AnsiString str1,str2;
  int atype;
  int i=-1;
  char alphatp[M];
  while((Isletter(buffer))||(IsDigit(buffer)))
  {  alphatp[++i]=buffer;
      bufnum++;
      buffer=bufs[bufnum];
     //buffer=Memo1->Lines->String[++i];
  }
  alphatp[i+1]='\0';
  if(atype=search(alphatp,1))
      { str1=IntToStr(line)+".   关键字"+alphatp+"(1,"+(AnsiString)(atype-1)+")";
         Memo2->Lines->Add(str1);
      /*printf("%s(1,%d)\n",alphatp,atype-1);*/ }
  else
   {atype=search(alphatp,6);
    str2=IntToStr(line)+".   标识符"+alphatp+"(6,"+(AnsiString)(atype-1)+")";
      Memo2->Lines->Add(str2);
    //printf("%s(6,%d)\n",alphatp,atype-1);
    }

    return (buffer);
    }
//-------------------------------------------------------------------------------
char __fastcall TForm1::digitprocess(char buffer,char *bufs)
{   AnsiString str;
    int i=-1;
    char digittp[M];
    int dtype;
    while(IsDigit(buffer))
    {   digittp[++i]=buffer;
        bufnum++;
        buffer=bufs[bufnum];
        }
    digittp[i+1]='\0';
    dtype=search(digittp,5);
    str=IntToStr(line)+".   常数"+digittp+"(5,"+(AnsiString)(dtype-1)+")";
    Memo2->Lines->Add(str);
    return (buffer);
    }
//------------------------------------------------------------------------------------------
char __fastcall TForm1::otherprocess(char buffer,char *bufs)
{AnsiString str1,str2,str3,str4,str5;

 int i=-1;
 char othertp[M];
 int otype,otypetp;
 othertp[0]=buffer;
 othertp[1]='\0';
 if(otype=search(othertp,3))
 {  str1=IntToStr(line)+".   算术运算符"+othertp+"(3,"+(AnsiString)(otype-1)+")";
    Memo2->Lines->Add(str1);
    bufnum++;
    buffer=bufs[bufnum];
    goto out;
    }
 if(otype=search(othertp,4))
 {bufnum++;
  buffer=bufs[bufnum];
  othertp[1]=buffer;
  othertp[2]='\0';
  if(otypetp=search(othertp,4))
  {str2=IntToStr(line)+".   关系运算符"+othertp+"(4,"+(AnsiString)(otypetp-1)+")";
   Memo2->Lines->Add(str2);
   goto out;
   }
   else
       othertp[1]='\0';
       str2=IntToStr(line)+".   关系运算符"+othertp+"(4,"+(AnsiString)(otype-1)+")";
       goto out;
       }
   if(buffer==':')
   { bufnum++;
     buffer=bufs[bufnum];
       if(buffer=='=')
             str3=line+".   :=(2,2)";
             Memo2->Lines->Add(str3);
             bufnum++;
             buffer=bufs[bufnum];
             goto out;
   }
   else
      {
        if(otype=search(othertp,2))
        {  str4=IntToStr(line)+".    界符"+othertp+"(2,"+(AnsiString)(otype-1)+")";
           Memo2->Lines->Add(str4);
           bufnum++;
           buffer=bufs[bufnum];
           goto out;
           }
           }
        if((buffer!='\n')&&(buffer!=' '))
            str5="error,not a world,"+buffer;
            Memo2->Lines->Add(str5);
            bufnum++;
            buffer=bufs[bufnum];
        out: return (buffer);
        }
//----------------------------------------------------------------------------------------

⌨️ 快捷键说明

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