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

📄 yufa.txt

📁 词法分析
💻 TXT
📖 第 1 页 / 共 2 页
字号:
语义分析程序 
#include "stdio.h"
#include "string.h"
char prog[100],token[8],ch;
char *rwtab[6]={"begin","if","then","while","do","end"};
int syn,p,m,n,sum,q;
int kk;
struct { char result1[8];
    char ag11[8];
    char op1[8];
    char ag21[8];
  } quad[20];
char *factor();
char *expression();
int yucu();
char *term();
int statement();
int lrparser();
char *newtemp();
scaner();
emit(char *result,char *ag1,char *op,char *ag2);
main()
{ int j;
 q=p=kk=0;
 printf("\nplease input a string (end with '#'): ");
 do
   { scanf("%c",&ch);
     prog[p++]=ch;
   }while(ch!='#');
 p=0;
 scaner();
 lrparser();
if(q>19)printf(" to long sentense!\n");
else for (j=0;j<q;j++)printf("   %s = %s %s %s \n\n",quad[j].result1,quad[j].ag11,quad[j].op1,quad[j].ag21);
getch();
}
int lrparser()
  { int schain=0;
    kk=0;
    if (syn==1)
      { scaner();
 schain=yucu();
 if(syn==6)
  { scaner();
    if((syn==0)&&(kk==0))  printf("Success!\n");
  }
 else { if(kk!=1)printf("short of 'end' !\n");
        kk=1;
        getch();
        exit(0);
      }
      }
    else { printf("short of 'begin' !\n");
    kk=1;
    getch();
    exit(0);
  }
    return (schain);
}
int yucu()
{ int schain=0;
  schain=statement();
  while(syn==26)
   { scaner();
     schain=statement();
   }
  return (schain);
}
int statement()
{ char tt[8],eplace[8];
  int schain=0;
  if (syn==10)
   { strcpy(tt,token);
     scaner();
     if(syn==18)
       { scaner();
  strcpy(eplace,expression());
  emit(tt,eplace,"","");
  schain=0;
 }
     else { printf("short of sign ':=' !\n");
     kk=1;
     getch();
     exit(0);
    }
    return (schain);
   }
}
char *expression()
{ char *tp,*ep2,*eplace,*tt;
  tp=(char *)malloc(12);
  ep2=(char *)malloc(12);
  eplace=(char *)malloc(12);
  tt=(char *)malloc(12);
  strcpy(eplace,term());
  while((syn==13)||(syn==14))
   { if (syn==13)strcpy(tt,"+");
     else strcpy(tt,"-");
     scaner();
     strcpy(ep2,term());
     strcpy(tp,newtemp());
     emit(tp,eplace,tt,ep2);
     strcpy(eplace,tp);
   }
  return (eplace);
}
char *term()
{ char *tp,*ep2,*eplace,*tt;
  tp=(char *)malloc(12);
  ep2=(char *)malloc(12);
  eplace=(char *)malloc(12);
  tt=(char *)malloc(12);
  strcpy(eplace,factor());
  while((syn==15)||(syn==16))
   { if (syn==15)strcpy(tt,"*");
     else strcpy(tt,"/");
     scaner();
     strcpy(ep2,factor());
     strcpy(tp,newtemp());
     emit(tp,eplace,tt,ep2);
     strcpy(eplace,tp);
   }
  return (eplace);
}
char *factor()
{ char *fplace;
  fplace=(char *)malloc(12);
  strcpy(fplace,"");
  if(syn==10)
   { strcpy(fplace,token);
     scaner();
   }
  else if(syn==11)
   { itoa(sum,fplace,10);
     scaner();
   }
  else if(syn==27)
   { scaner();
     fplace=expression();
     if(syn==28) scaner();
     else { printf("error on ')' !\n");
     kk=1;
     getch();
     exit(0);
   }
   }
  else { printf("error on '(' !\n");
  kk=1;
  getch();
  exit(0);
       }
  return (fplace);
}
char *newtemp()
{ char *p;
  char m[8];
  p=(char *)malloc(8);
  kk++;
  itoa(kk,m,10);
  strcpy(p+1,m);
  p[0]='t';
  return(p);
  }
scaner()
 {  sum=0;
    for(m=0;m<8;m++)token[m++]=NULL;
    m=0;
    ch=prog[p++];
    while(ch==' ')ch=prog[p++];
    if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
      { while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))
   {token[m++]=ch;
    ch=prog[p++];
   }
      p--;
      syn=10;
      token[m++]='\0';
      for(n=0;n<6;n++)
 if(strcmp(token,rwtab[n])==0)
    { syn=n+1;
      break;
    }
      }
    else if((ch>='0')&&(ch<='9'))
      { while((ch>='0')&&(ch<='9'))
 { sum=sum*10+ch-'0';
   ch=prog[p++];
 }
 p--;
 syn=11;
      }
    else switch(ch)
       { case '<':m=0;
    ch=prog[p++];
    if(ch=='>')
      {  syn=21;
      }
    else if(ch=='=')
      {  syn=22;
      }
    else
      {  syn=20;
         p--;
      }
    break;
  case '>':m=0;
    ch=prog[p++];
    if(ch=='=')
      { syn=24;
      }
    else
      { syn=23;
        p--;
      }
    break;
  case ':':m=0;
    ch=prog[p++];
    if(ch=='=')
      { syn=18;
      }
    else
      { syn=17;
        p--;
      }
    break;
  case '+': syn=13; break;
  case '-': syn=14; break;
  case '*': syn=15;break;
  case '/': syn=16;break;
  case '(': syn=27;break;
  case ')': syn=28;break;
  case '=': syn=25;break;
  case ';': syn=26;break;
  case '#': syn=0;break;
 default: syn=-1;break;
       }
    }
emit(char *result,char *ag1,char *op,char *ag2)
{ 
strcpy(quad[q].result1,result);
strcpy(quad[q].ag11,ag1);
strcpy(quad[q].op1,op);
strcpy(quad[q].ag21,ag2);
q++;
}
 



词法分析:
#include<iostream.h> 
#include<fstream.h> 
#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 
#include<conio.h> 
#include<process.h> /*头文件*/ 

void init(); 
char *DchangeB(char *buf); 
int search(char *buf,int type,int command); 
void intdeal(char *buffer); 
void chardeal(char *buffer); 
void errordeal(char error,int lineno); 
void scanner(); 

void init() 
{ char *key[]={"","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","volatile","while"}; /*C语言所有关键字/ 
char *limit[]={" ","(",")","[","]","->",".","!","++","--","&","~", 
"*","/","%","+","-","<<",">>","<","<=",">",">=","==","!=","&&","||", 
"=","+=","-=","*=","/=",",",";","{","}","#","_","'"};/*运算、限界符*/ 
fstream outfile; 
int i,j; 
char *c; 
outfile.open("key.txt",ios::out); 
for(i=0;i<32;i++) 
outfile<<key[i]<<endl; 
outfile.close(); 
outfile.open("Limit.txt",ios::out); 
for(j=0;j<38;j++) 
outfile<<limit[j]<<endl; 
c=""; 
outfile<<c; 
outfile.close(); 
outfile.open("bsf.txt",ios::out); 
outfile.close(); 
outfile.open("cs.txt",ios::out); 
outfile.close(); 
outfile.open("output.txt",ios::out); 
outfile.close(); 
} 

char *DchangeB(char *buf) 
{ 

int temp[20]; 
char *binary; 
int value=0,i=0,j; 
for(i=0;buf[i]!='\0';i++) 
value=value*10+(buf[i]-48); /*将字符转化为十进制数*/ 
if(value==0) 
{ 
binary=new char[2]; 
binary[0]='0'; 
binary[1]='\0'; 
return(binary); 
} 
i=0; 
while(value!=0) 
{ 
temp[i++]=value%2; 
value/=2; 
} 
temp[i]='\0'; 
binary=new char[i+1]; 
for(j=0;j<=i-1;j++) 
binary[j]=(char)(temp[i-j-1]+48); 
binary[i]='\0'; 
return(binary); /*十进制转化为二进制*/ 

} 

int search(char *buf,int type,int command) 
{ int number=0; 
fstream outfile; 
char ch; 
char temp[30]; 
int i=0; 
switch(type) 
{ 
case 1: outfile.open("key.txt",ios::in);break; 
case 2: outfile.open("bsf.txt",ios::in);break; 
case 3: outfile.open("cs.txt",ios::in);break; 
case 4: outfile.open("limit.txt",ios::in);break; 
} 
outfile.get(ch); 
while(ch!=EOF){ 
while(ch!='\n') 
{ 
temp[i++]=ch; 
outfile.get(ch); 
} 
temp[i]='\0'; 
i=0; 
number++; 
if(strcmp(temp,buf)==0) 
{ 
outfile.close(); 
return number; /*若找到,返回在相应表中的序号*/ 
} 
else 
outfile.get(ch); 
} //结束外层while循环 
if(command==1) 
{ 
outfile.close( ); 
return 0; /*找不到,当只需查表,返回0,否则还需造表*/ 

} 
switch(type) 
{ 
case 1: outfile.open("key.txt",ios::in);break; 
case 2: outfile.open("bsf.txt",ios::in);break; 
case 3: outfile.open("cs.txt",ios::in);break; 
case 4: outfile.open("limit.txt",ios::in);break; 
} 
outfile<<buf; 
outfile.close(); 
return number+1; 
} 


void intdeal(char *buffer){ 

fstream outfile; 
int result; 
result=search(buffer,1,1); /*先查关键字表*/ 
outfile.open("output.txt",ios::app); 
if(result!=0) 
outfile<<buffer<<result<<endl; /*若找到,写入输出文件*/ 
else 
{ 
result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/ 
outfile<<buffer<<result<<endl; 
} /*写入输出文件*/ 
outfile.close(); 
} 

void chardeal(char *buffer) 
{ fstream outfile; 
int result; 
result=search(buffer,1,1); /*先查关键字表*/ 
outfile.open("output.txt",ios::app); 
if(result!=0) 
outfile<<buffer<<result<<endl; /*若找到,写入输出文件*/ 
else 
{ 
result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/ 
outfile<<buffer<<result<<endl; 
} /*写入输出文件*/ 
outfile.close(); 
} 

void errordeal(char error,int lineno) 
{ cout<<"\nerror: "<<error<<" ,line"<<lineno; 
} 

void scanner() 
{ fstream infile,outfile; 
char filename[20]; 
char ch; 
int err=0; 
int i=0,line=1; 
int count,result,errorno=0; 
char array[30]; 
char *word; 
printf("\n please input the file scanner name:"); 
scanf("%s",filename); 
err=1; 
infile.open(filename,ios::nocreate|ios::in); 
while(! infile) 
{ 
cout<<"cannot open file"<<endl; 
printf("please input the file name again:\n"); 
scanf("%s",filename); 
infile.open(filename,ios::nocreate|ios::in); 
err++; 
if(err==3) 
{cout<<"SORROY YOU CAN'T VUEW THE PRGARME\n"; 
cout<<"TANKE YOU VIEW"<<endl; 
exit(0);} 
} 
infile.get(ch); 
while(ch!=EOF) 
{ /*按字符依次扫描源程序,直至结束*/ 
i=0; 
if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')) 
{ /*以字母开头*/ 
while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')||((ch>='0')&&(ch<='9'))) 
{ 
array[i++]=ch; 
infile.get(ch); 
} 
word=new char[i+1]; 
memcpy(word,array,i); 
word[i]='\0'; 
intdeal(word); 
if(ch!=EOF) 
infile.seekg(-1,ios::cur); 
} 
else if(ch>='0'&&ch<='9') 
{ /*以数字开头*/ 

⌨️ 快捷键说明

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