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

📄 program.txt

📁 此代码实现的是 对源程序进行解释 编码 是编译原理的词法分析示例
💻 TXT
字号:
// pro.cpp : Defines the entry point for the console application.
//

// program.cpp : Defines the entry point for the console application.
//
#include <fstream>
#include "stdafx.h"
#include <iostream>
#include <string>
#include "stdio.h"
using namespace std;
char s[100];    //临时数组
char ide[100];  //标识符存储
char c[100];    //字符常量存储
char str[100][100];      //字符串常量存储
char d[100][100];        //整型常量存储
char f[100][100];        //浮点型常量存储
int t=0;  //标记标识符
int j=0;  //标记字符
int k=0;  //标记字符串
int l=0;  //标记整型
int m=0;  //标记浮点型
int i=0;




char name1[40][20]={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","register","return",
"short","signed","sizeof","static","struct","switch","typedef","union","unsigned","void",
"volatatile","while"};


struct keyword
{
	char *name;
	int n2;
	int n1;
        bool b1;
}keyword[32]={
{&name1[0][0],0,0,false},
{&name1[1][0],1,0,false},
{&name1[2][0],2,0,false},
{&name1[3][0],3,0,false},
{&name1[4][0],4,0,false},
{&name1[5][0],5,0,false},
{&name1[6][0],6,0,false},
{&name1[7][0],7,0,false},
{&name1[8][0],8,0,false},
{&name1[9][0],9,0,false},
{&name1[10][0],10,0,false},
{&name1[11][0],11,0,false},
{&name1[12][0],12,0,false},
{&name1[13][0],13,0,false},
{&name1[14][0],14,0,false},
{&name1[15][0],15,0,false},
{&name1[16][0],16,0,false},
{&name1[17][0],17,0,false},
{&name1[18][0],18,0,false},
{&name1[19][0],19,0,false},
{&name1[20][0],20,0,false},
{&name1[21][0],21,0,false},
{&name1[22][0],22,0,false},
{&name1[23][0],23,0,false},
{&name1[24][0],24,0,false},
{&name1[25][0],25,0,false},
{&name1[26][0],26,0,false},
{&name1[27][0],27,0,false},
{&name1[28][0],28,0,false},
{&name1[29][0],29,0,false},
{&name1[30][0],30,0,false},
{&name1[31][0],31,0,false}
 };


//分隔符
struct specialword
{
	char name;
	int n2;
	int n1;
        bool b2;
}spw[12]={{'#',1,1},
		{'(',2,1,false},
		{')',3,1,false},
		{'[',4,1,false},
		{']',5,1,false},
		{'\'',6,1,false},
		{'|',7,1,false},
		{';',8,1,false},
		{':',9,1,false},
    		{'{',10,1,false},
		{'}',11,1,false},
		{',',12,1,false},
		
};

//运算符
struct operate
{
	char name;
	int n2;
	int n1;
        bool b3;
}op[33]={{'+',1,2,false},
          {'-',2,2,false},
          {'*',3,2,false},
          {'/',4,2,false},
          {'%',5,2,,false},
          {'=',6,2,false},
          {'<',7.2,false},
          {'>',8,2,false},
          {'!',9,2,false},
          {'&',10,2,false},
          {'^',11,2,false},
          {'|',12,2,false}
};

struct identifier
{ 
  char *name;
  int n1;
  int n2;
}identifier[100];
//常量 
struct int_constant
{ 
  char *name;
  int n0;
  int n1;
  int n2;
}int_constant[100]; 

struct float_constant
{ 
  char *name;
  int ide[100];
  int n0;
  int n1;
  int n2;
}float_constant[100];  
struct char_constant
{ 
  char *name;
  int n0;
  int n1;
  int n2;
}char_constant[100];

struct string_constant
{ 
  char *name;
  int n0;
  int n1; 
  int n2;
}string_constant[100];
 


bool specialword_handle(char );
bool keyword_handle(char *);
bool identifier_handle(char *);
bool char_handle(char *);
bool string_handle(char *);
bool int_handle(char *);
bool float_handle(char *);
bool operate_handle(char );
void handle(char *);
void outprint();

int main()
{
    
	char c[100];
	ifstream infile("a");
	if(!infile){
		cerr<<"error:unable to open input file!\n";
		return -1;
	}
  else 
      while( infile.get(c[i]) ){
        if(c[i]=='/')
           if(infile.get(c[i+1]=='*'))
			{
				handle(&c[0]);
				char ch;
				j+=2;
				while(1)
				{
					while(j<len&&s[j]!='*')j++;
					
					if(j<len&&s[j+1]=='/'){ i=++j; break;}
					else if(j>=len)
					{
						while(1)
						{
							fscanf(fp,"%c",&ch);
							if(ch=='*')
							{
								fscanf(fp,"%c",&ch);
								if(ch=='/'){zhusi=true;break;}
							}
						}
						if(zhusi)break;
					}
				}
				if(zhusi)break;
			}
			else if(s[j]=='/'&&s[j+1]=='/')
			{//处理注释"//"
			
				deal(i,j-1);
				char ch;
				bool zhusiwan=false;
				j=len;
				
				if(s[j]==10){zhusiwan=true;break;}
				while(1)
				{
					fscanf(fp,"%c",&ch);
					if(ch==10){zhusiwan=true;break;}
				}
                if(zhusiwan)break;
			}
        if(!(specialword_handle(c[i]))){
	    handle(&c[0]);
            i=0;
            continue;
   		}
        else
           if(!(operate_handle(&c[i]))){
	         	handle(&c[0]);
                        i=0;
 			continue;
			}            
           else
               if(c[i]==' ' || c[i]=='\n' || c[i]=='\t'){
                   handle(&c[0]); 
                   i=0;
			   }
             i++;
	 }
  outprint();
  return 0;
}






void handle(char *p)
{
      int n;
     
      if(keyword_handle(p))
          return;
      else   
          if(identifier_handle(p)) 
              return;
          else
             if(int_handle(p)) return;
	     else
         	 if(s[0]=='\''&& s[i-1]=='\'')
                    char_handle(p);  

                 else
                    if(s[0]=='"'&&s[i-1]=='"')
                      string_handle(p);
	            else
                       for(n=0;n<i;n++)
                          if(p[i]=='.'||p[i]=='e')
                             float_handle(p);
                          else
      		             printf("cannot read it");

}



//分隔符
bool specialword_handle(char c)
{ int n;
  for(n=0;n<13;n++)
        if(c==spw[n].name)){
	spw[n].b2=true;
        printf("1 %d  %c\n",spw[n].n2,spw[n].name);
  	return 1; 
    } 
  return 0;
}

//保留字
bool keyword_handle(char *p)
{
	
 int n;
 for(n=0;n<32;n++)
    if(!( strcmp(p,keyword[n].name) )){
         keyword[n].b1=true;
         printf("0  %d  %s\n",keyword[n].n2,keyword[n].name);
         return 1;
     }
 return 0;
 
}

//标识符
bool identifier_handle(char *p)
{
 
 char *q=&ide[t];
 strcpy(q,p);
 int n;
 for(n=0;n<t;n++)
   if(!strcmp(p,identifier[n].name)){
         printf("3 %d   %s\n",identifier[n].n2,identifier[n].name);
         return 1;
       }
   else{
         identifier[t].name=q;
         identifier[t].n1=1;
         identifier[t].n2=i;
         printf("3 %d   %s\n",identifier[t].n2,identifier[t].name);
         t++;
       }
return 0;
}
 
//字符常量
bool char_handle(char *p)
{
  
  char *q=&c[j];
  strcpy(q,p);
  int n;
  for(n=0;n<j;n++)
     if(!( strcmp(p,char_constant[n].name) )){
         printf("4 2 %d   %s\n",char_constant[n].n2,char_constant[n].name);
         return 1;
     }
     else{
         char_constant[j].name=q;
         char_constant[j].n0=4;
         char_constant[j].n1=2;
         char_constant[j].n2=j;
         printf("4 2 %d   %s\n",char_constant[j].n2,char_constant[j].name);
         j++;
	 return 0;
       }
}


//字符串常量  
 bool string_handle(char *p)
{
  char *q=&str[k][0];
  strcpy(q,p);
  int n;
  for(n=0;n<k;n++)
     if(!( strcmp(p,string_constant[n].name) )){
         printf("4 3 %d   %s\n",string_constant[n].n2,string_constant[n].name);
         return 1;
     }
     else{
         string_constant[k].name=q;
         string_constant[k].n0=4;
    	 string_constant[k].n1=3;
	 string_constant[k].n2=k;
         printf("4 3 %d   %s\n",string_constant[k].n2,string_constant[k].name);
	 k++;
         return 0;
        }
  
}

//整型常量
 bool int_handle(char *p)
{
  char *q=&d[l][0];
  strcpy(q,p);
  int n;
  for(n=0;n<l;n++)
     if(!( strcmp(p,int_constant[n].name) )){
         printf("4 0 %d   %s\n",int_constant[n].n2,int_constant[n].name);
	 return 1;
     }
     else{
         int_constant[l].name=q;
         int_constant[l].n0=4;
    	 int_constant[l].n1=3;
	 int_constant[l].n2=l;
         printf("4 0 %d   %s\n",int_constant[l].n2,int_constant[l].name);
	 l++;
         return 0;
	}
  return 0;
}     
 
bool float_handle(char *p)
{ char *q=&f[m][0];
  strcpy(q,p);
  int n;
  for(n=0;n<m;n++)
     if(!( strcmp(p,string_constant[n].name)) ){
         printf("4 1 %d   %s\n",string_constant[n].n2,string_constant[n].name);
  	 return 1;
      }
     else{
         string_constant[m].name=q;
         string_constant[m].n0=4;
    	 string_constant[m].n1=3;
	 string_constant[m].n2=m;
         printf("%d %d   %s\n",float_constant[m].n0,float_constant[m].n1,float_constant[m].n2);
	 m++;
         return 0;
	}
}  
    
bool operate_handle(char p){
		
 int n;
 for(n=0;n<32;n++)
    if(p==op[n].name){
	 op[n].b3=true;
         printf("2 %d   %s\n",op[n].n2,op[n].name);
         return 1;
     }
 return 0;
}

void outprint()
{       int a;
  	printf("本程序涉及的保留字如下:\n");
	for(a=0;a<32;a++)
		 if(keyword[a].b1==ture)
		 {
			 printf("%-20s  %d\n",keyword[a].name,keyword[a].n2);
			 
		 }
        printf("本程序涉及的分隔符如下:\n");
	for(a=0;a<12;a++)
		 if(spw[a].b2==ture)
		 {
			 printf("%-20s  %d\n",spw[a].name,spw[a].n2);
			 
		 }
        printf("本程序涉及的运算符如下:\n");
	for(a=0;a<12;a++)
		 if(operate[a].b2==ture)
		 {
			 printf("%-20s  %d\n",operate[a].name,operate[a].n2);
			 
		 }
	printf("本程序涉及的标识符如下:\n");
	for(a=0;a<t;a++)
	 {
		 printf("%-20s  %d\n",ide[a].name,ide[a].n2);
		 
	 }
        printf("本程序涉及的整数常量表如下:\n");
	 for(a=0;a<l;a++)
	 {
		 printf("%-20s  %d\n",d[a].name,d[a].n2);
		 
	 }
	 printf("本程序涉及的浮点常量表如下:\n");
	 for(a=0;a<m;a++)
	 {
		 printf("%-20s  %d\n",f[a].name,f[a].n2);
		 
	 }
	 printf("本程序涉及的字符常量表如下:\n");
	 for(a=0;a<j;a++)
	 {
		 printf("%-20s  %d\n",c[a].name,c[a].n2);
		 
	 }
	 printf("本程序涉及的字符串常量表如下:\n");
	 for(a=0;a<k;a++)
	 {
		 printf("%-20s  %d\n",str[a].name,str[a].n2);
		 
	 }
}


           	   

⌨️ 快捷键说明

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