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

📄 2009.cpp

📁 简单的词法分析器
💻 CPP
字号:
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h> 
#include<string.h>
#include<ctype.h>
int i=0,j,k;
char ch='\0';            //字符变量  
char buf[200]={'\0'};     //缓冲区
char words[10] = {'\0'};  //单词数组
void main()
{            FILE *fp1;
             FILE *fp2;
            
			 void scan();
         if((fp1=fopen("D:\\HelloWorld.java","r"))==NULL)
             {
                  printf("cannot open file\n");
             }
             else fread(buf,1,200,fp1);  //program.txt文件读入缓冲区
			 buf[199]=EOF;
			 fp2=fopen("D:\\result.txt","a");
			
          
             while(ch!=EOF)
			 {scan();}

          printf("End of analysis!\n"); 
				 
             fclose(fp1);
			 fclose(fp2);
                 
             
             
            
			 
       }
void scan()
{  int code;
j=0;
words[0]='\0';words[1]='\0';words[2]='\0';words[3]='\0';words[4]='\0';
words[5]='\0';words[6]='\0';words[7]='\0';words[8]='\0';words[9]='\0';

 FILE *fp2;
 fp2=fopen("D:\\result.txt","a");
/*************声明函数************/
void GetChar();   
void GetBC();
void Concat();
int IsLetter();
int IsDigit();
int Reserve();
void Retract();

GetChar();     //取字符
GetBC();      //调用取字符函数直至ch为非空字符

   if(IsLetter()) //若ch为字母
           {while(IsLetter()||IsDigit())
                 {Concat();GetChar();}
             Retract();     //回调
             code=Reserve();  //调用查关键字函数
                if(code==0){fputs("<0,",fp2);
                            fputs(words,fp2);
                            fputs(">\n",fp2);}  //标志符
                       else{fputs("<",fp2);
                              fputs(words,fp2);
                            fputs(",->\n",fp2);}} //关键字

                           
   else if(IsDigit())  //若ch为数字
            {while(IsDigit())
                    {Concat();
                     GetChar();}
              Retract();   //回调
               fputs("<1,",fp2);
                fputs(words,fp2);
             fputs(">\n",fp2);
             }

/**********ch为其他字符***********/
   else if(ch=='+')  fputs("<+,->\n",fp2);
   else if(ch=='-')  fputs("<-,->\n",fp2);
   else if(ch=='*')  fputs("<*,->\n",fp2);
   else if(ch=='/')  fputs("</,->\n",fp2); 
   else if(ch=='>')  {GetChar();
   if(ch=='=') fputs("<>=,->\n",fp2);
					 else{Retract();fputs("<>,->\n",fp2);}}
   else if(ch=='<')  {GetChar();
   if(ch=='=') fputs("<<=,->\n",fp2);
					 else{Retract();fputs("<<,->\n",fp2);}}
   else if(ch=='=')  {GetChar();
   if(ch=='=') fputs("<==,->\n",fp2);
					 else{Retract();fputs("<=,->\n",fp2);}}
   else if(ch=='(')  fputs("<(,->\n",fp2);
   else if(ch==')')  fputs("<),->\n",fp2); 
   else if(ch==';')  fputs("<;,->\n",fp2);
   else if(ch=='{')  fputs("<{,->\n",fp2); 
   else if(ch=='}')  fputs("<},->\n",fp2);
  
   
  
   fclose(fp2);
  
}
   
 void GetChar()      //取字符函数
{ch=buf[i++];

}

void GetBC()  //检查ch是否空白,空白则调用GetChar直至ch非空
{ 
   while(isspace(ch)==1||ch=='\r')
       GetChar();

}

void Concat()  //将ch中字符连接到words之后
{       
     words[j++]=ch;
}
int IsLetter()   //判断ch是否为字母
{if(ch>='a'&&ch<='z')
return 1;
else return 0;
}

int IsDigit()    //判断ch是否为数字
{if(ch>='0'&&ch<='9')
return 1;
else return 0;
}

int Reserve()   //判断words[10]是否为关键字
{int temp;
	char *keywords[14] ={"void","main","if","then","break","int",
  "char","float","include","for","while","printf","scanf","else"};
for (k=0; k<14; k++){temp=strcmp (words,keywords[k]);
if(temp==0) {return 1;continue;}}
 return 0;

}
void Retract()  //搜索指示器回调一个字符位置并置ch为空
{i--;
 ch='\0';
} 

⌨️ 快捷键说明

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