[2]《编译》词法分析程序

//本程序用于对源程序的词法分析
//keyWord存关键字, symbol存界符
//本程序在Visual C++6.0/Turboc 3.0中编译通过
//注:在TC2.0中,输入文件名无法识别,可能是编译器问题,请直接在源文件中置预分析文件路径,并注意格式:[Disk://files//*.*]。
//作者:吴延庆
//11th-Mar-2006
#include<stdio.h>
#define MAXLEN 255
#define KEY_NUM 7
#define SYMBOL 26

union wordContent{
 char T1[MAXLEN];
 int T2;
 char T3;
};


struct word{
 int code;
 union wordContent value;
}word;

char *keyWord[KEY_NUM]={"main","int","char","if","else","for","while"};
char *symbol[SYMBOL]={"=","+","-","*","/","(",")","[","]","{","}",",",":",";",">","<",">=","<=","==","!=","&","&&","||","()","[]","{}"};
int line=1;
int flag_error=0;
FILE *fp,*fout;
InitContent()
{
 int i;
 for(i=0;i<MAXLEN;i++)word.value.T1[i]='/0';
}

CheckSymbol()
{
 int i;
 int flag1=0;/*是否有匹配*/
 int flag2=0;/*是否出现紧跟的左右括号*/
if(word.code==0)
{
 for(i=0;i<SYMBOL;i++)
  if(!strcmp(symbol[i],word.value.T1))
  {
   word.code=21+i;
   flag1=1;

    if(i<26&&i>22)
 {
  printf("(%d,%c)/n",(i)*2-20,word.value.T1[0]);
  fprintf(fout,"(%d,%c)/n",(i)*2-20,word.value.T1[0]);
  printf("(%d,%c)/n",(i)*2-19,word.value.T1[1]);
  fprintf(fout,"(%d,%c)/n",(i)*2-19,word.value.T1[1]);
  flag2=1;
 }

  }
 if(flag1==0)
{
 printf("error:the use of symbol:%s is error in line:%d /n",word.value.T1,line);
 fprintf(fout,"error:the use of symbol:%s is error in line:%d /n",word.value.T1,line);
 //flag_error=1;
}
else if(flag2==0)
{
printf("(%d,%s)/n",word.code,word.value.T1);
fprintf(fout,"(%d,%s)/n",word.code,word.value.T1);
}
}

else if(word.code==10)
{
for(i=0;i<KEY_NUM;i++)
  if(!strcmp(keyWord[i],word.value.T1))
  {
   word.code=1+i;
   flag1=1;
  }
 
printf("(%d,%s)/n",word.code,word.value.T1);
fprintf(fout,"(%d,%s)/n",word.code,word.value.T1);
}
else if(word.code==20)
{
 printf("(%d,%s)/n",word.code,word.value.T1);
 fprintf(fout,"(%d,%s)/n",word.code,word.value.T1);
}
InitContent();
}

 

main()
{
 int flag,ch_flag;

 char ch;
 int ch_i;
 char fname;
 int flag_ignore=0;
 flag=0;/*the begin of the word*/
 ch_flag=0;/*0:num   1:word   2:sym*/
 ch_i=0;

 if((fout=fopen("d://result.txt","w"))==NULL)
 {
  printf("error to create the file,please check that the disk is exist!/n");
   exit(1);
}
else fclose(fout);


if((fout=fopen("d://result.txt","a"))==NULL)
{
    printf("error to open the file,please check that the file is exist!/n");
    exit(1);
 }

printf("please input the name of the file that you want to analyze!!/nformat:[Disk:*.*]/n");
scanf("%s",&fname);

 if((fp=fopen(&fname,"r"))==NULL)
 {
 printf("error to open the file,please check that the file is exit!/n");
 exit(1);
 }
 else
 printf("The file is open!!/n");

 do
 {
 // ch=getc(fp);
if(flag_ignore==1)
{
 do
 {
 ch=getc(fp);
 }while((ch>=97&&ch<=122)||(ch>=65&&ch<=90)||(ch>=48&&ch<=57));
}
else ch=getc(fp);
     flag_ignore=0;


 /*****Analysis********************/
  if(ch!='/t'&&ch!=' '&&ch!='/n'&&ch!=EOF)
  {

    if((ch>=97&&ch<=122)||(ch>=65&&ch<=90))
    {
     if(flag==0)
     {

     ch_flag=1;
     ch_i=0;
     word.value.T1[ch_i]=ch;
     word.code=10;
     flag=1;
     }
     else
     {
      switch(ch_flag)
      {
       case 0:
   printf("error:number is followed by char in line:%d!/n",line);
   fprintf(fout,"error:number is followed by char in line:%d!/n",line);
   flag_ignore=1;

InitContent();
   flag=0;
   flag_error=1;
   break;
       case 1:
   ch_i++;
   word.value.T1[ch_i]=ch;
   break;
       case 2:
   CheckSymbol();
   ch_i=0;
   word.value.T1[ch_i]=ch;
      word.code=10;
   ch_flag=1;
   flag=1;
   break;
      }

     }
    }
    else if(ch>=48&&ch<=57)
    {
/****/
    if(flag==0)
 {
     ch_flag=0;
     ch_i=0;
     word.value.T1[ch_i]=ch;
     word.code=20;
     flag=1;
     }
     else
     {
      switch(ch_flag)
      {
       case 0:
      ch_i++;
   word.value.T1[ch_i]=ch;
   break;
       case 1:
   ch_i++;
   word.value.T1[ch_i]=ch;
   break;
       case 2:
  
   CheckSymbol();
   ch_i=0;
 
   word.value.T1[ch_i]=ch;
   word.code=20;
   flag=1;
   ch_flag=0;
   break;
      }

     }
/****/
    }
    else
    {
/****/
     if(flag==0)
     {

        ch_flag=2;
        ch_i=0;

     word.value.T1[ch_i]=ch;
     word.code=0;
     flag=1;
     }
     else
     {
      switch(ch_flag)
      {
       case 0:
       case 1:
   CheckSymbol();
   ch_i=0;
   word.value.T1[ch_i]=ch;
   word.code=0;
   ch_flag=2;
   flag=1;
   break;
       case 2:
   ch_i++;
   word.value.T1[ch_i]=ch;
   break;
      }

     }
 /****/
 }

  }

  else
{
    if(ch=='/n')
   {
     line++;
    }
     flag=0;
 }


if(flag==0)
{
     if(flag_error==0)
       CheckSymbol();
    else flag_error=0;
}


 }while(!feof(fp));

fclose(fp);
fclose(fout);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值