日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

c語言詞法分析器

 funson 2007-03-28

   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(左括號(hào)運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘[‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(右中括號(hào)運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘]‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(左中括號(hào)運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘-‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   if(ch==‘-‘)
    {
    token[i++]=ch;
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(自加運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
    ch=fgetc(input);
    continue;
    }
   else if (ch==‘>‘)
    {
    token[i++]=ch;
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(指針運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
    ch=fgetc(input);
    continue;
    }
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(負(fù)運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘.‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(點(diǎn)運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘&‘)  
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(地址與運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;   
   }
  else if(ch==‘!‘)  
   {
   token[i++]=ch;
   ch=fgetc(input);
   if(ch==‘=‘)
    {
    token[i++]=ch;
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(不等于比較符\t,%-16s)\t%4d\n",count,token,i);
    ch=fgetc(input);
    continue;
    }
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(取反運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;   
   }
  else if(ch==‘~‘)  
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(按位運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;   
   }
  else if(ch==‘*‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(乘運(yùn)算符\t,%-16s)\t%4d\t\n",count,token,i);
   continue;   
   }
  else if(ch==‘%‘)  
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(求余運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;   
   }
  else if(ch==‘/‘)  
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(除法運(yùn)算符\t,%-16s)\t%4d\t\n",count,token,i);
   continue;   
   }  
  else if(ch==‘<‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   if(ch==‘=‘)
    {
    token[i++]=ch;
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(小于等于比較符\t,%-16s)\t%4d\n",count,token,i);
    ch=fgetc(input);
    continue;
    }
   if(ch==‘<‘)
    {
    token[i++]=ch;
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(左移運(yùn)算符\t,%-16s)\t%4d\t\n",count,token,i);
    ch=fgetc(input);
    continue;
    }
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(小于比較符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘>‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   if(ch==‘=‘)
    {
    token[i++]=ch;
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(大于等于比較符\t,%-16s)\t%4d\n",count,token,i);
    ch=fgetc(input);
    continue;
    }
   if(ch==‘>‘)
    {
    token[i++]=ch;
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(右移運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
    ch=fgetc(input);
    continue;
    }
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(大于比較符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘=‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   if(ch==‘=‘)
    {
    token[i++]=ch;
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(等于比較符\t,%-16s)\t%4d\n",count,token,i);
    ch=fgetc(input);
    continue;
    }
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(賦值運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘,‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(逗號(hào)界符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   } 
  else if(ch==‘;‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(分號(hào)界符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  
  else if(ch==‘{‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(邊界運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘}‘)
   {
   token[i++]=ch;
   ch=fgetc(input);
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(邊界運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘+‘)
   { 
   token[i++]=ch;
   ch=fgetc(input);
   if(ch==‘+‘)
    {
    token[i++]=ch;
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(自加運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
    ch=fgetc(input);
    continue;
    }
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(加運(yùn)算符\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  else if(ch==‘"‘)
   { 
   token[i++]=ch;
   ch=fgetc(input);
   while(ch!=‘"‘&&ch!=EOF)
    {
    token[i++]=ch;
    ch=fgetc(input);    
    }
   if(ch==‘"‘)
    {
    token[i++]=ch;
    ch=fgetc(input);
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(字符串\t\t,%-16s)\t%4d\n",count,token,i);
    continue;
    }
   else
    {
    while(ch!=EOF)
    {
    token[i++]=ch;
    ch=fgetc(input);
    }
    token[i]=‘\0‘;
    count++;fprintf(output,"%4d\t(字符串錯(cuò)誤\t,%-16s)\t%4d\n",count,token,i);
    }
   } 
  else
   {
   while(ch!=‘ ‘&&ch!=‘\t‘&&ch!=‘\n‘&&ch!=EOF)
   {
    token[i++]=ch;
    ch=fgetc(input);
   }
   token[i]=‘\0‘;
   count++;fprintf(output,"%4d\t(無法識(shí)別錯(cuò)誤\t,%-16s)\t%4d\n",count,token,i);
   continue;
   }
  }
 fclose(input);
 fclose(output);
}

/*判斷是不是關(guān)鍵字:
 1,則是標(biāo)識(shí)符
 0,則是關(guān)鍵字
  2,則是運(yùn)算符
 */

int reserver(char *str)
{
 FILE *fpReserver;
 char *tmpStr=‘\0‘;

 if(strcmp(str,"sizeof")==0)
  return 2;
 /*打開關(guān)鍵字文件*/
 if((fpReserver=fopen("keyword.txt","r"))==NULL)
 { printf("file open in readonly mode,but an error generate!\n");
  exit(0);
 }
 while(fscanf(fpReserver,"%s",tmpStr)!=EOF)
  if(strcmp(str,tmpStr)==0)
   {fclose(fpReserver);return 0;}
 fclose(fpReserver);
 return 1; 



/*

輸入文件input.txt

#include<stdio.h>

void E(char *);
void E1(char *);
void T(char *);
void T1(char *);
void F(char *);

void main()
{
 char *str="i+(i*i";
 printf("%s\n",str);
 E(str);
}

void E(char *str)
{
 T(str);
 printf("1%s\n",str);
 E1(str);
 printf("2%s\n",str);
}

void E1(char *str)
{
 if (*str==‘+‘)
 {
  str++;
  T(str);
  E1(str);
 }

}

void T(char *str)
{
 F(str);
 T1(str);
}
void T1(char *str)
{
 if(*str==‘*‘)
  {
   str++;
   F(str);
   T1(str);
  }
}

void F(char *str)
{
 if(*str==‘(‘)
 {
  str++;
  E(str);
  if(*str==‘)‘)
   str++;
  else
  {
   printf("lost ‘)‘\n");
   exit(0);
  }
 }
 else if(*str==‘i‘)
  str++;
      else
  {
   printf("lost variable\n");
   exit(0);
  }
}

*/

/*

關(guān)鍵字文件keyword.txt

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

*/

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多