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

📄 unit1.cpp

📁 此为词法分析程序中的程序清单
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include<stdio.h>
#include<string.h>
#include "Unit1.h"
int x=1;                                 //输出表格中的行下标
char s[1000];                            //暂存由界面或文件输入的字符
FILE *fp1,*fp2;                          //输入输出文件存取指针
int ssj=1,E=1,z=0,ssj1=1;                //控制界面或文件输入,和是否演示动画的参数
int ad=200;                              //自定义符号的起始地址
int count=0,y=1;                         //出错统计参数
int haha=1;                              //出错时动画处理参数
AnsiString add;                          //自定义符号的地址增量
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)      //程序初始化按钮
{
Animate11->Active=true;
 Animate11->Visible=true;
 if(RadioButton1->Checked==true)          //选择输入方式(文件或界面)
 ssj=1;                
 else
 ssj=0;
 if(CheckBox1->Checked==true)             //选择是否播放动画
 ssj1=1;
 else
 ssj1=0;
int i;
x=1;                                      //输出从第一列开始
z=0;                                      //对s数组的下标清零
count=0;                                  //出错计数清零
Edit1->Text=0;                            
y=0;                                      //出错统计清零
for(int row=0;row<=5;row++)
 for(int col=0;col<200;col++)
    StringGrid1->Cells[row][col]="\0";    //表格数据清零
if(ssj==1)
{
 for(i=0;i<=500;i++)
 s[i]='\0';
 Edit2->Text=RichEdit1->GetTextLen();
  for(i=1;i<=RichEdit1->GetTextLen();i++)
{
 s[i-1]=RichEdit1->Text[i];               //界面形式输入的数据存储
}
}
else
{
fp1=fopen("c:\\in.txt","r");
fp2=fopen("c:\\out.txt","w");
char c;
 for(i=0;i<=500;i++)
 s[i]='\0';
i=0;
while(!feof(fp1))
{
 c=fgetc(fp1);
 s[i]=c;                                 //文件形式输入的数据存储
 i++;
}
s[i-1]='\0';
}
//Edit2->Text=s;
StringGrid1->Cells[0][0]="序号";         //对表头初始化
StringGrid1->Cells[1][0]="£";
StringGrid1->Cells[2][0]="特征";
StringGrid1->Cells[3][0]="类型";
StringGrid1->Cells[4][0]="地址/编码";
StringGrid1->Cells[5][0]="内容";
}
//---------------------------------------------------------------------------
char a[10]={'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'},b[10]={'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'};
int i=0,j=0,k=0,n=0;                           
char o[2],d[10],c[10];                  //全局变量定义并初始化

struct                                  //模拟保留字表
{
 char a[10];
 char b[9];
}str[]={{"\0","00000000"},{"main","00000001"},{"auto","00000010"},{"break","00000011"},{"cin","00000100"},{"cout","00000101"},{"case","00000110"},{"char","00000111"},{"const","00001000"},{"continue","00001001"},{"default","00001010"},{"do","00001011"},
{"double","00001100"},{"else","00001101"},{"enum","00001110"},{"extern","00001111"},{"float","00010000"},{"for","00010001"},{"goto","00010010"},{"if","00010011"},{"int","00010100"},{"long","00010101"},{"register","00010110"},{"return","00010111"},{"short","00011000"},{"signed","00011001"},{"sizeof","00011010"},{"static","00011011"},{"struct","00011100"},{"switch","00011101"},{"typedef","00011110"},{"union","00011111"},{"unsigned","00100000"},{"void","00100001"},{"volatile","00100010"},{"while","00100011"}};

struct                                 //用户自定义存储空间
{
 char a[10];
 char b[9];
}str1[]={{"\0","00000000"}};

struct                                 //运算符号表
{
 char a;
 char b[9];
}str2[]={{'\0',"00000000"},{'.',"10000000"},{'!',"10000001"},{'~',"10000010"},{'-',"10000011"},{'*',"10000100"},{'&',"10000101"},{'/',"10000110"},{'%',"10000111"},{'+',"10001000"},{'<',"10001001"},{'>',"10001010"},{'^',"10001011"},{'|',"10001100"},{'=',"10001101"}};

struct                                //双字符符号表
{
 char a[3];
 char b[9];
}str3[]={{"\0","00000000"},{"->","11000000"},{"++","11000001"},{"--","11000010"},{"<<","11000011"},{">>","11000100"},{"<=","11000101"},{">=","11000110"},{"==","11000111"},{"!=","11001000"},{"&&","11001001"},{"||","11001010"},{"+=","11001011"},{"-=","11001100"},{"*=","11001101"},{"/=","11001110"},{"%=","11001111"},{"&=","11010000"},{"^=","11010001"},{"|=","11010010"}};

struct                               //标识符符号表
{
 char a;
 char b[9];
}str4[]={{'\0',"01000000"},{'`',"01000001"},{'@',"01000010"},{'#',"01000011"},{'$',"01000100"},{'_',"01000101"},{'[',"01000110"},{']',"01000111"},{':',"01001000"},{'\"',"01001001"},{'\'',"01001010"},{'?',"01001011"}};

int check(char a[10])               //保留字,用户自定义符号处理函数
{
 int i;
 for(i=1;i<=36;i++)
 if(strcmp(a,str[i].a)==0)          //与保留字表进行匹配,若匹配,则为保留字
 {
  Form1->StringGrid1->Cells[5][x]=a;         //对输出表格赋值
  Form1->StringGrid1->Cells[4][x]=str[i].b;
  Form1->StringGrid1->Cells[3][x]="保留字";
  Form1->StringGrid1->Cells[2][x]="";
  Form1->StringGrid1->Cells[1][x]='0';
  Form1->StringGrid1->Cells[0][x]=x;
  if(ssj==0)
  fprintf(fp2,"%10d,         0,          ,      保留字,%10s,%10s\n",x,str[i].b,a);     //对输出文件赋值
  if(Form1->StringGrid1->Cells[5][x]=="int"||Form1->StringGrid1->Cells[5][x]=="char"||Form1->StringGrid1->Cells[5][x]=="long"||Form1->StringGrid1->Cells[5][x]=="float")
  for(i=0;i<10;i++)                 //只有在int,char等后面的字母字符串为用户自定义的     
   c[i]=a[i];
  x++;                              //表格行下标加一
  a[0]='\0';a[1]='\0';a[2]='\0';a[3]='\0';a[4]='\0';a[5]='\0';a[6]='\0';a[7]='\0';a[8]='\0';a[9]='\0';
  return(1);
 }
  for(i=1;i<E;i++)                 //检验该字符串是否被定义为用户自定义
  if(strcmp(a,str1[i].a)==0)
  {
  Form1->StringGrid1->Cells[5][x]=a;     //对输出表格赋值
  Form1->StringGrid1->Cells[4][x]=str1[i].b;
  Form1->StringGrid1->Cells[3][x]="用户自定义";
  Form1->StringGrid1->Cells[2][x]="v";
  Form1->StringGrid1->Cells[1][x]='2';
  Form1->StringGrid1->Cells[0][x]=x;
  if(ssj==0)
  fprintf(fp2,"%10d,         2,         v,  用户自定义,%10s,%10s\n",x,str1[i].b,a);     //对输出文件赋值
  x++;
  a[0]='\0';a[1]='\0';a[2]='\0';a[3]='\0';a[4]='\0';a[5]='\0';a[6]='\0';a[7]='\0';a[8]='\0';a[9]='\0';
  return(0);
  }
  if(strcmp(c,"int")==0||strcmp(c,"char")==0||strcmp(c,"long")==0||strcmp(c,"float")==0||strcmp(c,"short")==0||strcmp(c,"float")==0)
  {
  for(i=0;i<10;i++)                  //当字母字符串前是int,char等时该字符串定义为用户自定义
    str1[E].a[i]=a[i];
  add=ad;                            //分配内存单元
  add=add+"H";
  ad=ad+2;
  for(i=1;i<5;i++)
  str1[E].b[i-1]=add[i];
  str1[E].b[i-1]='\0';
  Form1->StringGrid1->Cells[5][x]=a;             //对输出表格赋值
  Form1->StringGrid1->Cells[4][x]=str1[E].b;
  Form1->StringGrid1->Cells[3][x]="用户自定义";
  Form1->StringGrid1->Cells[2][x]="v";
  Form1->StringGrid1->Cells[1][x]='2';
  Form1->StringGrid1->Cells[0][x]=x;
  if(ssj==0)
  fprintf(fp2,"%10d,         2,         v,  用户自定义,%10s,%10s\n",x,str1[E].b,a);     //对输出文件赋值
  x++;
  E++;
  a[0]='\0';a[1]='\0';a[2]='\0';a[3]='\0';a[4]='\0';a[5]='\0';a[6]='\0';a[7]='\0';a[8]='\0';a[9]='\0';
  return(0);                                //暂存数组a清空
  }
   Form1->StringGrid1->Cells[5][x]=a;          //若该字符串不属于前面定义的,则视为出错
   Form1->StringGrid1->Cells[4][x]="error";
   Form1->StringGrid1->Cells[3][x]="error";
   Form1->StringGrid1->Cells[2][x]="error";
   Form1->StringGrid1->Cells[1][x]="error";
   Form1->StringGrid1->Cells[0][x]=x;
   if(ssj==0)
     fprintf(fp2,"%10d,     error,     error,       error,     error,%10s\n",x,a);
   x++;
   a[0]='\0';a[1]='\0';a[2]='\0';a[3]='\0';a[4]='\0';a[5]='\0';a[6]='\0';a[7]='\0';a[8]='\0';a[9]='\0';
   return(0);
}

int and(int s,char b[])              //将数字字符串的内容拼接为一个自然数
{
 int n=0,k=0;
 for(k=1;k<=s;k++)
 {
  n=n*10+b[k-1]-48;                 
 }
 b[0]='\0';b[1]='\0';b[2]='\0';b[3]='\0';b[4]='\0';b[5]='\0';b[6]='\0';b[7]='\0';b[8]='\0';b[9]='\0';
 return(n);                         //暂存数组a清空
}

void fuyu(char o[])                //双字符符号处理函数
{
                                   int m;
                                   for(m=0;m<20;m++)              //与双字符符号表进行匹配
                                   {
                                    if(strcmp(o,str3[m].a)==0)
                                     break;
                                   }
                                   Form1->StringGrid1->Cells[5][x]=o;      //匹配成功输出结果
                                   Form1->StringGrid1->Cells[4][x]=str3[m].b;
                                   Form1->StringGrid1->Cells[3][x]="运算符";
                                   Form1->StringGrid1->Cells[2][x]="";
                                   Form1->StringGrid1->Cells[1][x]='0';
                                   Form1->StringGrid1->Cells[0][x]=x;
                                   Form1->Edit6->Text=o;
                                   if(ssj==0)
                                   fprintf(fp2,"%10d,         0,          ,      运算符,%10s,%10s\n",x,str3[m].b,o);
                                   x++;
}
int find1(int q)         //检验是否有“ )”与“( ”相匹配
{
  while(s[q]!='\0')      //从当前字符开始查找,直到结尾   
  {
   q=q+1;
   if(s[q]=='(')         //发现“( ”时递归调用find1()
   {
    q=find1(q);
    if(q==0)
     return 0;
   }
   if(s[q]==')')         //发现“ )”时返回当前下标继续匹配   
   {
    q=q+1;
    return q;
   }
  }
  if(s[q]=='\0')
   return 0;
}
int find2(int q)        //检验是否有“( ”与“ )”相匹配
{
  while(s[q]!='\0')    //同find1()
  {
   q=q-1;
   if(s[q]==')')
   {
    q=find2(q);
    if(q==0)
     return 0;
   }
   if(s[q]=='(')
   {
    q=q-1;
    return q;
   }
  }
  if(s[q]=='\0')
   return 0;
}
int find3(int q)       // 检验是否有“ } ”与“ { ”相匹配
{
  while(s[q]!='\0')    //同find1()
  {
   q=q+1;
   if(s[q]=='{')
   {
    q=find3(q);
    if(q==0)
     return 0;
   }
   if(s[q]=='}')
   {
    q=q+1;
    return q;
   }
  }
  if(s[q]=='\0')
   return 0;
}
int find4(int q)         // 检验是否有“{ ”与“ }”相匹配
{
  while(s[q]!='\0')      //同find1()
  {
   q=q-1;
   if(s[q]=='}')
   {
    q=find4(q);
    if(q==0)
     return 0;
   }
   if(s[q]=='{')
   {
    q=q-1;
    return q;
   }
  }
  if(s[q]=='\0')
   return 0;
}

ok(char ch,int p)                 //主程序
{
 int m;
 if(ch>=65&&ch<=90||ch>=97&&ch<=122)      //当CH为字母时送字母处理分程序处理
    {
     if(ssj1==1)
     {
      Form1->Edit5->Text="\0";Form1->Edit6->Text="\0";
      Form1->Animate2->Play(1,24,1);
     }
      k=0;
    }
 else if(ch>=48&&ch<=57)                  //当CH为数字时送数字处理分程序处理
    {
     if(ssj1==1)               
     {
      Form1->Edit4->Text="\0";Form1->Edit6->Text="\0";
      Form1->Animate1->Play(1,24,1);
     }
      k=1;
    }
 else                                    //除字母,数字外的符号送字符处理分程序
    {
     if(ssj1==1)
     {
      Form1->Edit4->Text="\0";Form1->Edit5->Text="\0";

⌨️ 快捷键说明

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