以前做的词法分析小程序

来源:互联网 发布:淘宝比阿里巴巴还便宜 编辑:程序博客网 时间:2024/05/19 13:21

#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#define LEN 4048
char program[LEN];

void print(char program[],int dot)   //测试程序内容
{
 for(int i=dot;;i++)
 {
  if(program[i]!='/0')
   cout<<program[i];
  else
   break;
 }
}

void commentTest()    //对注释进行校验
{
// cout<<"Comment start..."<<endl;
 int temp1=0,temp2=0,i=0;  //temp1与temp2记录注释位置
 while(program[i]!='/0')
 {
  if(program[i]=='/'&&program[i+1]=='*')
  {   
   temp1=i;   
  }
  if(program[i]=='*'&&program[i+1]=='/')
  {
   temp2=i+1;
  }

  i++;
 }
// cout<<"The first /'s Address is"<<temp1<<endl;
// cout<<"The second /'s Address is"<<temp2<<endl;
 if(temp1!=temp2)
 {
  int temp=temp1;
  for(int j=1;j<temp2-temp+2;j++)
  {
   while(program[temp]!='/0')
   {
    program[temp]=program[temp+1];
    ++temp;
   }
   temp=temp1;
  }
 }
}


void wordsTest()    //词法分析
{
 int i=0;
 while(program[i]!='/0')
 {
  //cout<<"i is"<<i;
  
  switch(program[i])
  {
  /*
  case ' ':
   {
    cout<<" "<<"/t"<<"' ' is NULL"<<endl;
    break;
   }
  */
  case '=':
   {      
    cout<<"="<<"/t"<<"',' is NULL"<<endl;     
    while(program[i]!=','||program[i]!=';')
    {
     cout<<program[i];
     i++;
    }
    cout<<"/t"<<"',' is VALUES"<<endl;
    break;
   }

  case ',':
   {
    cout<<","<<"/t"<<"',' is NULL"<<endl;
    break;
   }
  case ';':
   {
    cout<<";"<<"/t"<<"';' is NULL"<<endl;
    break;
   }
  case 'm':
   { 
    int j=i+1;
    if(program[j]=='a'&&program[j+1]=='i'&&program[j+2]=='n')
    {
     cout<<"main"<<"/t"<<"'main' is a function name"<<endl;
    }
    else
    {
     cout<<"varial"<<"/t"<<"Varial"<<endl;
    }
    i+=3;

    break;
   }
  case 'c':
   { 
    int j=i+1;
    if(program[j]=='h'&&program[j+1]=='a'&&program[j+2]=='r')
    {
     cout<<"char"<<"/t"<<"'char' is a key name"<<endl;
    }
    else
    {
     cout<<"varial"<<"/t"<<"Varial"<<endl;
    }
    i+=3;
    ////////
    i++;
    i++;
    while(program[i]!=';')
    {
     if(program[i]!=','&&program[i]!=';'&&program[i]!='='&&program[i]!='/'')
     {
      cout<<program[i];
     }
     else if(program[i]=='=')
     { 
      cout<<"/t"<<"varial... ..."<<endl;   
      
      cout<<program[i]<<"/t"<<"'=' is NULL"<<endl;
      i++;
      cout<<program[i]<<"/t"<<"'/'' is NULL"<<endl;
      i++;
      while(program[i]!=','&&program[i]!=';'&&program[i]!='/'')
      {
       cout<<program[i];
       i++;
      }
      cout<<"/t"<<" is value"<<endl;
       //i++;
      cout<<program[i]<<"/t"<<"'/'' is NULL"<<endl;
       
      //i--;       
      //else
      // i++;
     }
     else if(program[i]=',')
     {
      if(program[i-1]=='/'')
       ;
      else
       cout<<"/t"<<"varial... ..."<<endl;
      cout<<program[i]<<"/t"<<"',' is NULL"<<endl;
     }
     i++;
    }
    if(program[i-1]=='/'')
     ;
    else
     cout<<"/t"<<"varial... ..."<<endl;
    //cout<<"/t"<<"varial... ..."<<endl;
    //cout<<program[i]<<"/t"<<"NULL"<<endl;
    i--;
    ////////
    break;
   }
  case 'v':
   { 
    int j=i+1;
    if(program[j]=='o'&&program[j+1]=='i'&&program[j+2]=='d')
    {
     cout<<"void"<<"/t"<<"'void' is a key name"<<endl;
    }
    else
    {
     cout<<"varial"<<"/t"<<"Varial"<<endl;
    }
    i+=3;
    break;
   }
  case 'i':
   { 
    int j=i+1;
    if(program[j]=='n'&&program[j+1]=='t')
    {
     cout<<"int"<<"/t"<<"'int' is a key name"<<endl;
    }
    else
    {
     
     cout<<"varial"<<"/t"<<"Varial"<<endl;
    }
    i+=2;
    ////////////////////////////////////////////////
    i++;
    i++;
    while(program[i]!=';')
    {
     if(program[i]!=','&&program[i]!=';'&&program[i]!='=')
     {
      cout<<program[i];
     }
     else if(program[i]=='=')
     { 
      cout<<"/t"<<"varial... ..."<<endl;
      
      {
       cout<<program[i]<<"/t"<<"'=' is NULL"<<endl;
       i++;
       while(program[i]!=','&&program[i]!=';')
       {
        cout<<program[i];
        i++;
       }
       cout<<"/t"<<" is value"<<endl;
       i--;       
      }
      //else
      // i++;
     }
     else if(program[i]=',')
     {
      if(program[i-1]>='0'&&program[i-1]<='9')
       ;
      else
       cout<<"/t"<<"varial... ..."<<endl;
      cout<<program[i]<<"/t"<<"',' is NULL"<<endl;
     }
     i++;
    }
    if(program[i-1]>='0'&&program[i-1]<='9')
       ;
      else
       cout<<"/t"<<"varial... ..."<<endl;
    //cout<<"/t"<<"varial... ..."<<endl;
    //cout<<program[i]<<"/t"<<"NULL"<<endl;
    i--;
    //////////////////////////////////////////////
    break;
   }
  case 'f':
   { 
    int j=i+1;
    if(program[j]=='l'&&program[j+1]=='o'&&program[j+2]=='a'&&program[j+3]=='t')
    {
     cout<<"float"<<"/t"<<"'float' is a key name"<<endl;
    }
    else
    {
     cout<<"Varial"<<"/t"<<"Varial"<<endl;
    }
    i+=4;
    ////////
    i++;
    i++;
    while(program[i]!=';')
    {
     if(program[i]!=','&&program[i]!=';'&&program[i]!='=')
     {
      cout<<program[i];
     }
     else if(program[i]=='=')
     { 
      cout<<"/t"<<"varial... ..."<<endl;
      
      {
       cout<<program[i]<<"/t"<<"'=' is NULL"<<endl;
       i++;
       while(program[i]!=','&&program[i]!=';')
       {
        cout<<program[i];
        i++;
       }
       cout<<"/t"<<" is value"<<endl;
       i--;       
      }
      //else
      // i++;
     }
     else if(program[i]=',')
     {
      if(program[i-1]>='0'&&program[i-1]<='9')
       ;
      else
       cout<<"/t"<<"varial... ..."<<endl;
      cout<<program[i]<<"/t"<<"',' is NULL"<<endl;
     }
     i++;
    }
    if(program[i-1]>='0'&&program[i-1]<='9')
       ;
      else
       cout<<"/t"<<"varial... ..."<<endl;
    //cout<<"/t"<<"varial... ..."<<endl;
    //cout<<program[i]<<"/t"<<"NULL"<<endl;
    i--;
    ////////
    break;
   }
  case 'd':
   { 
    int j=i+1;
    if(program[j]=='o'&&program[j+1]=='u'&&program[j+2]=='b'&&program[j+3]=='l'&&program[j+4]=='e')
    {
     cout<<"double"<<"/t"<<"'double' is a key name"<<endl;
    }
    else
    {
     cout<<"varial"<<"/t"<<"Varial"<<endl;
    }
    i+=5;
    ////////
    i++;
    i++;
    while(program[i]!=';')
    {
     if(program[i]!=','&&program[i]!=';'&&program[i]!='=')
     {
      cout<<program[i];
     }
     else if(program[i]=='=')
     { 
      cout<<"/t"<<"varial... ..."<<endl;
      
      {
       cout<<program[i]<<"/t"<<"'=' is NULL"<<endl;
       i++;
       while(program[i]!=','&&program[i]!=';')
       {
        cout<<program[i];
        i++;
       }
       cout<<"/t"<<" is value"<<endl;
       i--;       
      }
      //else
      // i++;
     }
     else if(program[i]=',')
     {
      if(program[i-1]>='0'&&program[i-1]<='9')
       ;
      else
       cout<<"/t"<<"varial... ..."<<endl;
      cout<<program[i]<<"/t"<<"',' is NULL"<<endl;
     }
     i++;
    }
    if(program[i-1]>='0'&&program[i-1]<='9')
       ;
      else
       cout<<"/t"<<"varial... ..."<<endl;
    //cout<<"/t"<<"varial... ..."<<endl;
    //cout<<program[i]<<"/t"<<"NULL"<<endl;
    i--;
    ////////
    break;
   }
  case '(':
   {
    cout<<"("<<"/t"<<"'(' is a fuhao"<<endl;
    break;
   }
  case ')':
   {
    cout<<")"<<"/t"<<"')'is a fuhao"<<endl;
    break;
   }
  case '{':
   {
    cout<<"{"<<"/t"<<"'{' is a fuhao"<<endl;
    break;
   }
  case '}':
   {
    cout<<"}"<<"/t"<<"'}' is a fuhao"<<endl;
    break;
   }
  default: ;//break;
  }  
  i++;
 }
// cout<<"Error!"<<endl;
}


void main()
{
 cout<<"Please input your program:<# is over!>"<<endl;
 for(int i=0;i<LEN;i++)
 {
  char ch;
  ch=getchar();
  if(ch!='#')
  { 
   program[i]=ch;
  }
  else
  {
   program[++i]='/0';

   commentTest();
   wordsTest();
  // print(program,0);
   exit(0);
  }
 }
}

 

原创粉丝点击