zoukankan      html  css  js  c++  java
  • 编译原理词法分析程序

    #include <iostream>
    #include<string>
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    using namespace std;
    #define  MAX 22
    char ch =' ';
    string key[15]={"switch","break","if","then","else","while","write","read",
    "do", "int","const","char","float","double","for"};
    
    int Iskey(string c)    //关键字判断
    {
       int i;
       for(i=0;i<MAX;i++)
        {
          if(key[i].compare(c)==true)
            return 1;
        }
        return 0;
    }
    int IsLetter(char c)   //判断是否为字母
    {
        if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A')))
            return 1;
        else
            return 0;
    }
    int IsDigit(char c)    //判断是否为数字
    {
         if(c>='0'&&c<='9')
            return 1;
         else
            return 0;
    }
    void analyse(FILE *fpin)  //词法分析程序
    {
        string arr="";
        while((ch=fgetc(fpin))!=EOF)
        {
            arr="";
            if(ch==' '||ch=='	'||ch=='
    ')
            {
            }
            else if(IsLetter(ch))
            {
                while(IsLetter(ch)||IsDigit(ch))
                {
                    if((ch<='Z')&&(ch>='A'))
                        ch=ch+32;
                    arr=arr+ch;
                    ch=fgetc(fpin);
                }
                fseek(fpin,-1L,SEEK_CUR);
                if (Iskey(arr))
                {
                    cout<<arr<<"	$关键字"<<endl;
                }
                else  cout<<arr<<"	$普通标识符"<<endl;
            }
    
            else if(IsDigit(ch))
            {
                while(IsDigit(ch)||(ch=='.'&&IsDigit(fgetc(fpin))))
                {
                    arr=arr+ch;
                    ch=fgetc(fpin);
                }
                fseek(fpin,-1L,SEEK_CUR);
                cout<<arr<<"	$无符号实数"<<endl;
            }
           else switch(ch)
            {
               case'+':
               case'-' :
               case'*' :
               case'=' :
               case'/' :cout<<ch<<"	$运算符"<<endl;break;
               case'(' :
               case')' :
               case'[' :
               case']' :
               case';' :
               case'.' :
               case',' :
               case'{' :
               case'}' :cout<<ch<<"	$界符"<<endl;break;
               case':' :
                {ch=fgetc(fpin);
                if(ch=='=')
                    cout<<":="<<"	$运算符"<<endl;
                else
                {
                    cout<<"="<<"	$运算符"<<endl;;
                    fseek(fpin,-1L,SEEK_CUR);
                }
                }break;
                case'>' :{ch=fgetc(fpin);
                             if(ch=='=') cout<<">="<<"	$运算符"<<endl;
                             if(ch=='>')cout<<">>"<<"	$输入控制符"<<endl;
                             else {cout<<">"<<"	$运算符"<<endl;
                                 fseek(fpin,-1L,SEEK_CUR);}
                             }break;
                case'<' :{ch=fgetc(fpin);
                             if(ch=='=')cout<<"<="<<"	$运算符"<<endl;
                             else if(ch=='<')cout<<"<<"<<"	$输出控制符"<<endl;
                             else if(ch=='>') cout<<"<>"<<"	$运算符"<<endl;
                             else{cout<<"<"<<"	$运算符"<<endl;
                                fseek(fpin,-1L,SEEK_CUR);}
                            }break;
                default : cout<<ch<<"	$无法识别字符"<<endl;
            }
        }
    }
    int main()
    {
       char in_fn[30];
       FILE * fpin;
       cout<<"请输入源文件名(包括路径和后缀名):";
       for(;;)
        {
           cin>>in_fn;
           if((fpin=fopen(in_fn,"r"))!=NULL)
                break;
           else
            cout<<"文件路径错误!请输入源文件名(包括路径和后缀名):";
        }
       cout<<"
    ********************分析如下*********************"<<endl;
       analyse(fpin);
       fclose(fpin);
    
       return 0;
    }

    请dalao不吝赐教。
  • 相关阅读:
    使用SecureCRT连接虚拟机中Linux系统的详细方法以及虚拟网络配置方法
    虚拟机快照克隆多台的方法
    Linux虚拟机网络设置
    Hadoop学习笔记之一:Hadoop IPC
    webpack超详细配置, 使用教程(图文)
    webstrom提示不见了
    vuejs实现本地数据的筛选分页
    关于手机端audio无法自动播放问题解决方法
    计算机实现加法的学习心得
    计算机编码随记
  • 原文地址:https://www.cnblogs.com/liesun/p/7350340.html
Copyright © 2011-2022 走看看