zoukankan      html  css  js  c++  java
  • 0921 词法分析程序

    #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("你输入是:
    ");
    			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<<"	类别码:"<<a<<"	 单词值:";
    		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
    ");break;
                case'-':  printf("- 类别码为14
    ");break;
    		    case'*':  printf("* 类别码为15
    ");break;
    			case'/':  printf("/ 类别码为16
    ");break;
    			case';':  printf("; 类别码为26
    ");break;;
    			case'(':  printf("( 类别码为27
    ");break;
    			case')':  printf(") 类别码为58
    ");break;
    			case'#':  printf("#类别码为0
    ");;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]='';
    		i=0;
    		analyze();
    	}
    }
    

      

  • 相关阅读:
    Ubuntu下VSFTPD(五)(匿名FTP设置方法)
    Ubuntu下VSFTPD(六)(常见FTP命令及其功能) (
    ubuntu13.04装配oracle11gR2
    oracle之报错:ORA-00054: 资源正忙,要求指定 NOWAIT_数据库的几种锁
    oracle建索引的可选项
    Oracle自定义函数
    C# WinForm开发系列
    为C#自定义控件添加自定义事件
    python 爬虫抓取心得
    C# 正则表达式学习
  • 原文地址:https://www.cnblogs.com/bestmoment/p/4826972.html
Copyright © 2011-2022 走看看