词法分析器(C++/C)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34195441/article/details/78988765

从一个文件中读取源码,运行后可以直接看到结果,也可在指定文件中查看结果。

这个词法分析器只实现了部分关键字、字符等的识别,可在Key[][]数组自行添加,种别码的判别与添加用case更加方便,可写下试试。

#include <iostream>
#include <fstream>
#include <ctype.h>
#include <cstdlib>
#include <cstring>
using namespace std;
char key[6][10]={"begin","if","then","while","do","end"};
ifstream in;
ofstream out;
void deal(char *ch)
{
    char str[110];
    int l,i,j;
    memset(str,0,sizeof(str));
    for(i=0;i<strlen(ch);)
    {
        if(ch[i]==' '||ch[i]=='\n'||ch[i]=='\t')//处理空格,制表符,换行符 
        {
            i++;
        }
        else if(isalpha(ch[i]))//处理遇到的字符 
        {
            l=0;
            str[l++]=ch[i];//暂存字符 
            i++;
            while(isdigit(ch[i])|| isalpha(ch[i]))//一直搜索到最后一个字符或数字 
            {
                str[l++]=ch[i];
                i++;
            }
            str[l]='\0';//封装为字符串 
            for(j=0;j<6;j++)//检查是否与给定关键字匹配 
            {
                if(strcmp(str,key[j])==0)//若匹配则为对应关键字 
                {
                    cout<<"("<<j+1<<","<<str<<")"<<endl;//输出到dos窗口 
                    out<<"("<<j+1<<","<<str<<")"<<endl;//输入到文件 
                    break; 
                }
            }
            if(j==6)//若不匹配则为一般字符 
            {
                cout<<"(10,"<<str<<")"<<endl;
                out<<"(10,"<<str<<")"<<endl;
            }
        }
        else if(isdigit(ch[i]))//若遇到数字 
        {
            l=0;
            str[l++]=ch[i];//暂存遇到的数字 
            i++;
            while(isdigit(ch[i]))//一直搜索到数字结尾 
            {
                str[l++]=ch[i];
                i++;
            }
            str[l]='\0';
            cout<<"(11,"<<str<<")"<<endl;
            out<<"(11,"<<str<<")"<<endl;
        }
        else//判断是否为运算符,界符符号 
        {
            if(ch[i]=='+')
            {
                cout<<"(13,+)"<<endl;
                out<<"(13,+)"<<endl;
            }
            else if(ch[i]=='-')
            {
                cout<<"(14,-)"<<endl;
                out<<"(14,-)"<<endl;
            }
            else if(ch[i]=='*')
            {
                cout<<"(15,*)"<<endl;
                out<<"(15,*)"<<endl;
            }
            else if(ch[i]=='/')
            {
                cout<<"(16,/)"<<endl;
                out<<"(16,/)"<<endl;
            }
            else if(ch[i]==':')
            {
                if((i+1)<strlen(ch)&&ch[i+1]=='=')
                {
                    cout<<"(18,:=)"<<endl;
                    out<<"(18,:=)"<<endl;
                    i++;
                }
                else
                {
                    cout<<"(17,:)"<<endl;
                    out<<"(17,:)"<<endl;
                }
            }
            else if(ch[i]=='<')
            {
                if((i+1)<strlen(ch)&&ch[i+1]=='>')
                {
                    cout<<"(21,<>)"<<endl;
                    out<<"(21,<>)"<<endl;
                    i++;
                }
                else if((i+1)<strlen(ch)&&ch[i+1]=='=')
                {
                    cout<<"(22,<=)"<<endl;
                    out<<"(22,<=)"<<endl;
                    i++;
                }
                else
                {
                    cout<<"(20,<)"<<endl;
                    out<<"(20,<)"<<endl;
                }
            }
            else if(ch[i]=='>')
            {
                if((i+1)<strlen(ch)&&ch[i+1]=='=')
                {
                    cout<<"(24,>=)"<<endl;
                    out<<"(24,>=)"<<endl;
                    i++;
                }
                else
                {
                    cout<<"(23,>)"<<endl;
                    out<<"(23,>)"<<endl;
                }
            }
            else if(ch[i]=='=')
            {
                cout<<"(25,=)"<<endl;
                out<<"(25,=)"<<endl;
            }
            else if(ch[i]==';')
            {
                cout<<"(26,;)"<<endl;
                out<<"(26,;)"<<endl;
            }
            else if(ch[i]=='(')
            {
                cout<<"(27,()"<<endl;
                out<<"(27,()"<<endl;
            }
            else if(ch[i]==')')
            {
                cout<<"(28,))"<<endl;
                out<<"(28,))"<<endl;
            }
            else if(ch[i]=='#')
            {
                cout<<"(0,#)"<<endl;
                out<<"(0,#)"<<endl;
            }
            else
            {
                cout<<"出错"<<endl; 
                out<<"出错"<<endl;
                exit(0);
            }
            i++;
        }
    }
}
int main()
{
    char ch[110],s;
    /*int sum=0;
    do{
       s=cin.get();
       ch[sum++]=s;
    }while(s!='#');
    ch[sum]='\0';
    deal(ch);
    */
    
    char in_name[50],out_name[50];//读入文件 输出文件 
    cout<<"指定读入文件名:"<<endl;
    cin>>in_name;
    in.open(in_name);
    if(in.fail())
    {
        cout<<"读入文件打开失败"<<endl;
        exit(0);
    } 
    cout<<"指定输出文件名:"<<endl;
    cin>>out_name;
    out.open(out_name);
    if(out.fail())
    {
        cout<<"写入文件打开失败"<<endl;
        exit(0); 
    } 
    while(in.good()&&!in.eof())//如果文件正常且没有到达文件结束 
    {
        memset(ch,0,110);
        in.getline(ch,110);
         deal(ch);
    }
    system("pause");
    return 0;
}





猜你喜欢

转载自blog.csdn.net/qq_34195441/article/details/78988765
今日推荐