词法分析实验报告

#include <stdio.h>
#include <string.h>
#include <iostream.h>
/*全局变量*/
char * chr_form[100];
int q=0,temp;
char *word[6]={"begin","end","if","then","do","while"};
int i=0,j=0,k=0,t=0;
char ch,str[20];
char *ss[100];
char form[1000];
void analyze();
void main ()
{
    printf("输入字符串,以*结束:");
    form[0]=cin.get();
    for(q=1;form[q-1]!='##';q++)
    {
        form[q]=cin.get();
        if(form[q]=='*')
        {
            printf("你输入是:\n");
            cout.write(form,q);
            break;
        }
    }
    cout<<endl;
    analyze();
 
}
 
void GetChar()
{
    ch=form[k];
    k++;
}
void getbc()
{
    while(ch==' ')
    {
         
        GetChar();
    }
}
void concat()
{
    str[i]=ch;
    i++;
}
bool isletter()
{
    if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
        return(1);
    else
        return(0);
}
bool isdigit()
{
if(((ch)<='9')&&((ch)>='0'))
return (1);
else
return (0);
}
int reserve()
{
    for(int q=0;q<5;q++)
    {
        if(strcmp(word[q],str)==0)
            return q;
        if(q==4)
            return -1;
    }
}
void retract()
{
    k--;
    ch=NULL;
}
char *insertld()
{
    chr_form[j]=str;
    j++;
    return chr_form[0];
}
char * insertconst()
{
    ss[t]=str;
    t++;
    return ss[0];
}
int code;
void output(int a,char *p1,char *p2)
{
    cout<<"\t类别码:"<<a<<"\t 单词值:";
        while(p1<=p2)
        {
            printf("%s",*p1);
            p1++;
        }
        cout<<endl;
}
 
void analyze()
{
    GetChar();
    getbc();
    if(isletter())
    {
        while (isletter()||isdigit())
        {
            concat();
        GetChar();
        }
        retract();
        code=reserve();
        switch(code)
        {
        case 0:cout<<"单词:" <<str<<"  类别码为: "<<1<<endl;break;
 
        case 1:cout<<"单词:" <<str<<"  类别码为: "<<2<<endl;break;
     
        case 2:cout<<"单词:" <<str<<"  类别码为: "<<3<<endl;break;
 
      case 3:cout<<"单词:" <<str<<"  类别码为: "<<4<<endl;break;
       case 4:cout<<"单词:" <<str<<"  类别码为: "<<5<<endl;break;
      case 5:cout<<"单词:" <<str<<"  类别码为: "<<6<<endl;break;
 
      default:
          cout<<"单词:" <<str<<"  类别码为: "<<6<<endl;break;
 
 
    }
    }
    else
    {
        if( isdigit())
        {
          while(isdigit()||ch=='.')
          {
              concat();
              GetChar();
          }
          retract();
          cout<<"单词:"<<str<<"  类别码为:"<<7<<endl;
        }
        else
        {
            switch(ch)
            {
            case'+': printf("+ 类别码为13\n");break;
            case'-':  printf("- 类别码为14\n");break;
            case'*':  printf("* 类别码为15\n");break;
            case'/':  printf("/ 类别码为16\n");break;
            case';':  printf("; 类别码为26\n");break;;
            case'(':  printf("( 类别码为27\n");break;
            case')':  printf(") 类别码为58\n");break;
            case'#':  printf("#类别码为0\n");;break;
         
            case':':GetChar();
                if(ch=='=')
                {
                    cout<<"单词::=  类别码为: "<<18<<endl;break;
                }
                else
                {
                    retract();
                    cout<<"单词::   类别码为: "<<17<<endl;break;
                }
            case'=':cout<<"单词:=  类别码:"<<24<<endl; break;
            case'>':GetChar();switch(ch)
                    {
            case'=':cout<<"单词:>=  类别码:"<<24<<endl;break;
            default: retract;
                cout<<"单词:=> 类别码:"<<12<<endl;break;
                    }
            case'<':GetChar();
                switch(ch)
                {
                case'=':cout<<"单词:<=  类别码:"<<21<<endl;break;
                case'>':cout<<"单词:<>  类别码:"<<22<<endl;break;
               default: retract();
                    cout<<"单词:<  类别码:"<<20<<endl;break;
            }
        }
    }
    }
 
    while(k<q)
    {
        for(int p=0;p<50;p++)
            str[p]='\0';
        i=0;
        analyze();
    }
}

posted on 2016-10-16 22:37  嘻嘻嘻宸  阅读(116)  评论(0编辑  收藏  举报