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

📄 cffxq.txt

📁 编译原理实验 词法分析器
💻 TXT
字号:
/*
lee's 词法分析器


Builde 2004年12月8日

CUMT online.cumt.edu.cn
*/


#include<iostream.h>
#include<fstream.H>
#include<stdlib.h>
#include<string.h>

bool letter(char);
bool digit(char);
char keyword[9][9]={"main","for","if","else","while","do","for","int","char"};
char character;  //字符变量,存放最新读进的源程序字符
char token[50];  //字符数组,存放构成单词符号的字符串
bool flags; 

bool putchar()
{
 cout<<"您是否是把代码文件写入了wh.txt文件中?[y(Y)/n(N)]..."<<endl;
 char get1;
 cin>>get1;
 if(get1=='Y'||get1=='y')
 {
  return true;
 }
 else
 {
  return false;
 }
}
//-----------------------
void getbc()
{
 if(character==' '||character==char(10))
 {  
  if(strlen(token)!=0)
  {
   for(int k=0;k<9;k++)
   {
    int a;
   if(strlen(keyword[k])>strlen(token))
    a=strlen(keyword[k]);
   else
    a=strlen(token);
    if (!strncmp(keyword[k],token,a))
    {
     cout<<"< "<<token<<" , 关键字>"<<endl;
     flags=false;
     goto Loop;
    }
   }
   if(!strncmp("+",token,1)||!strncmp("-",token,strlen(token)))
   {
    cout<<"< "<<token<<" , 算术运算符>"<<endl;
    flags=false;
    goto Loop;
   } 
    else if(!strncmp("<",token,1)||!strncmp("<=",token,strlen(token))||!strncmp("=<",token,strlen(token))||!strncmp("<>",token,strlen(token))||!strncmp("=",token,strlen(token))||!strncmp(">=",token,strlen(token))||!strncmp(">",token,strlen(token)))
   {
    cout<<"< "<<token<<" , 关系运算符>"<<endl;
    flags=false;
    goto Loop;
   }
    else if(!strncmp(":=",token,2))
    {
     cout<<"< "<<token<<" , 赋值运算符>"<<endl;
     flags=false;
    goto Loop;
   }
     else if(!strncmp(":",token,strlen(token)))
    {
     cout<<"< "<<token<<" , 冒号>"<<endl;
     flags=false;
    goto Loop;
   }
      else if(!strncmp(";",token,strlen(token)))
    {
     cout<<"< "<<token<<" , 分号>"<<endl;
     flags=false;
    goto Loop;
   }
      else if (!strncmp(")",token,strlen(token)))
      {
        cout<<"< "<<token<<" , 右括号>"<<endl;
     flags=false;
    goto Loop;
      }
       else if (!strncmp("(",token,strlen(token)))
      {
        cout<<"< "<<token<<" , 左括号>"<<endl;
     flags=false;
    goto Loop;
      }
	else
    {
     if (flags==true)
     {
      cout<<"< "<<token<<", 数>"<<endl;
      flags=false;
      goto Loop;
     }else
       cout<<"< "<<token<<", 标示符>"<<endl;
     flags=false;
      goto Loop;
    }  
Loop:       for(int j=strlen(token);j>=0;j--)
    token[j]='\0';    
  }
 }
 else if(digit(character))
 {
       token[strlen(token)]=character;
    if(strlen(token)==1)
    {
    flags=true;
    }
 }
 else if(letter(character))
 { 
   token[strlen(token)]=character;
 }
 else 
  token[strlen(token)]=character;
}


bool letter(char character1)
{
 if(((char(97)<=character1)&&(character1<=char(122)))||((char(65)<=character1)&&(character1<=char(90))))
  return true;
 else
  return false;
}


bool digit(char character1)
{
 if((char(48)<=character1)&&(character1<=char(57)))
  return true;
 else
  return false;
}

void main()
{
 bool one;
 one=putchar();
 if(one==false)
 {
  cout<<"请准备好了代码再运行此文件"<<endl;
 }
 else
 {
  fstream infile;
  infile.open ("wh.txt",ios::in);
  char ch;
  while(infile.get (ch))
  {
   character=ch;
   getbc();
  }
  int tmp;
  cin>>tmp;
 }
}

⌨️ 快捷键说明

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