词法分析 方法一

54 篇文章 2 订阅
11 篇文章 2 订阅

1.没有加上种别码

测试程序:

#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include<string.h>
#include<stdio.h>
#include<time.h>

using namespace std;
string instr;//输入符号串
int index;//当前输入符号读入字符的位置
char character;//全局变量字符,存放最新读入的字符
string token;//字符数组,存放已读入的字符序列
map<string, int> Symbol;//标识符集
map<string, int> Digit;//常数集
map<string, int> other;//other
map<string, int>::iterator ite;
const int len = 100;
string Reserve[len];//保留字表
map<string,int> type;
void init_Reserve()  //构造保留字表的函数
{
    Reserve[1] = "begin";
    Reserve[2] = "end";
    Reserve[3] = "if";
    Reserve[4] = "then";
    Reserve[5] = "while";
    Reserve[6] = "do";
    Reserve[7] = "const";
    Reserve[8] = "var";
    Reserve[9] = "call";
    Reserve[10] = "procedure";

}
void getChar()  //读入一个字符
{
    character = instr[index++];
}
void getnbc()  //读入非空白字符
{
    while (character == ' ')
    {
        getChar();
    }
}
void concat()  //连接字符串
{
    token = token + character;
}
bool letter()  //判断是否为字母
{
    if ((character >= 'A'&&character <= 'Z') || (character >= 'a'&&character <= 'z'))
        return true;
    return false;
}
bool digit()  //判断是否为数字
{
    if (character >= '0'&&character <= '9')
        return true;
    return false;
}
void retract() //回退字符的函数
{
//    character = ' ';
    index--;
}
int reserve()  //匹配保留字符
{
    for (int i = 0; i < len; i++)
        if (Reserve[i] == token)
            return i;
    return -1;
}
string symbol()
{
    ite = Symbol.find(token);
    if (ite != Symbol.end())
    {
        return ite->first;
    }
    else
    {
        Symbol[token] = Symbol.size();
        return token;
    }
}
string constant()
{
    ite = Digit.find(token);
    if (ite != Digit.end())
    {
        return ite->first;
    }
    else
    {
        Digit[token] = Digit.size();
        return token;
    }
}
string Three()
{
    ite = other.find(token);
    if (ite != other.end())
    {
        return ite->first;
    }
    else
    {
        other[token] = other.size();
        return token;
    }
}
void error()
{
    cout << token << "\t-->\t该单词不存在" << endl;
}
//词法分析函数,逐个识别单词
void LexAnalyze()
{
    token = "";
    getChar();
    getnbc();
    string val;
//    int num = -1;
    if(character>='a'&&character<='z' || character>='A'&&character<='Z')
    {
        while (letter() || digit())  //为字母或数字
        {
            concat();//追加到token末尾

            if(token=="odd")
            {
                getChar();
                Three();
                return ;
            }
            getChar();//读取下一个字符
        }
        retract();//回退一个字符
        {
            val = symbol();//查看标识符表

        }
    }
    else if(character>='0' && character<='9')
    {
        int flag=0;
        while (character>='0' && character<='9' || character>='a' && character<='z' || character>='A' && character <='Z')
        {
            if(character>='0' && character<='9')
                concat();

            if(character>='a' && character<='z' || character>='A' && character <='Z')
            {
                concat();
                flag=1;
            }
            getChar();
        }
        if(flag==1)
        {
            return error();
        }
        retract();
        val = constant();//查看常数表

    }
    else if(character=='<')
    {
        concat();
        getChar();
        if (character == '=')
        {
            concat();
            Three();
//返回<=符号
        }
        else if(character=='>')
        {
            concat();
            Three();
        }
        else
        {
            retract();
            Three();
//返回<符号
        }
    }
    else if(character=='>')
    {
        concat();
        getChar();
        if (character == '=')
        {
            concat();
            Three();
//返回>=符号
        }
        else
        {
            retract();
            Three();
//返回>符号
        }
    }
    else if(character=='=')
    {
        concat();
        getChar();
        if (character == '=')
        {
            concat();
            Three();
//返回==符号
        }
        else
        {
            retract();
            Three();
//返回=符号
        }
    }

    else if(character==':')
    {
        concat();
        getChar();
        if (character == '=')
        {
            concat();
            Three();

        }
        else
        {
            cout<<token<<endl;
            return error();
        }
    }

    else if(character=='+')
    {
        concat();
        Three();
    }


    else if(character=='-')
    {
        concat();
        Three();
    }

    else if(character=='*')
    {
        concat();
        Three();
    }


    else if(character=='/')
    {
        concat();
        Three();
    }


    else if(character=='(')
    {
        concat();
        Three();
    }

    else if(character==')')
    {
        concat();
        Three();
    }

    else if(character==',')
    {
        concat();
        Three();
    }

    else if(character=='.')
    {
        concat();
        Three();
    }

    else if(character==';')
    {
        concat();
        Three();
    }

    else
    {
        return error();
    }

}
void show_table()
{
    cout << "\n==================" << "标识符" << "==================" << endl;
//    cout << "标识符\t\t类别编码\t表中位置" << endl;
    for (ite=Symbol.begin(); ite != Symbol.end(); ite++)
    {
        int flag=0;
        for (int i = 0; i < len; i++)
        {
//               cout<<Reserve[i]<<endl;
            if (Reserve[i] == ite->first)
            {
                cout<<ite->first<<"是保留字"<<"   ";
                cout << "("<<ite->first <<","<<ite->second<<")" << endl;
                flag=1;
                break;
            }
        }
        if(flag==0)
            cout << "("<<ite->first <<","<<ite->second<<")" << endl;
    }
    cout << "\n==================" << "常数表" << "==================" << endl;
//    cout << "常量值\t\t类别编码\t表中位置" << endl;
    for (ite = Digit.begin(); ite != Digit.end(); ite++)
    {
        cout << "("<<ite->first <<","<<ite->second<<")" << endl;
    }
    cout << "\n==================" << "算符、界符" << "==================" << endl;
    for (ite = other.begin(); ite != other.end(); ite++)
    {
        cout << "("<<ite->first <<","<<ite->second<<")" << endl;
    }
}
int main()
{
//    clock_t start,finish;
//    double totaltime;
//    start=clock();


//    type[""]


    init_Reserve();//保留字表初始化
    Symbol.clear();//标识符集初始化
    Digit.clear();//常数集初始化
    index = 0;
    character = ' ';
    token = "";
    cout << "请输入待词法分析的源程序代码:输入文件结束符结束输入\n" << endl;
    //源程序代码输入处理
//    instr="var a,b,c; a:=1; b:=2; c:=a+b";


    char ch[1000];
    string ins;
    while(gets(ch))
    {
//        ins=ch;

        instr=ch;
        index=0;
        while (index < instr.size())
        {
            LexAnalyze();
        }
//        cout<<instr<<endl;
        memset(ch,0,sizeof(ch));
    }
//    cout<<instr<<endl;
//    string in;
//    while (cin >> in&&in != "@")
//    {
//        instr = instr + " " + in;
//    }
    //识别二元组初始化
//    while (index < instr.size())
//    {
//        LexAnalyze();
//    }
    //展示构造的各种词汇表
    show_table();

//    finish=clock();
//    totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
//    cout<<totaltime<<endl;
//    //system("pause");
    return 0;

}



测试结果:

 

 

2.加上了种别码

测试程序:

#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include<string.h>
#include<stdio.h>
#include<time.h>

using namespace std;
string instr;//输入符号串
int index;//当前输入符号读入字符的位置
char character;//全局变量字符,存放最新读入的字符
string token;//字符数组,存放已读入的字符序列
map<string, int> Symbol;//标识符集
map<string, int> Digit;//常数集
map<string, int> other;//other
map<string, int>::iterator ite;
const int len = 100;
string Reserve[len];//保留字表
map<string,int> type;
void init_Reserve()  //构造保留字表的函数
{
    Reserve[1] = "begin";
    Reserve[2] = "end";
    Reserve[3] = "if";
    Reserve[4] = "then";
    Reserve[5] = "while";
    Reserve[6] = "do";
    Reserve[7] = "const";
    Reserve[8] = "var";
    Reserve[9] = "call";
    Reserve[10] = "procedure";

}
void getChar()  //读入一个字符
{
    character = instr[index++];
}
void getnbc()  //读入非空白字符
{
    while (character == ' ')
    {
        getChar();
    }
}
void concat()  //连接字符串
{
    token = token + character;
}
bool letter()  //判断是否为字母
{
    if ((character >= 'A'&&character <= 'Z') || (character >= 'a'&&character <= 'z'))
        return true;
    return false;
}
bool digit()  //判断是否为数字
{
    if (character >= '0'&&character <= '9')
        return true;
    return false;
}
void retract() //回退字符的函数
{
//    character = ' ';
    index--;
}
int reserve()  //匹配保留字符
{
    for (int i = 0; i < len; i++)
        if (Reserve[i] == token)
            return i;
    return -1;
}
string symbol()
{
    ite = Symbol.find(token);
    if (ite != Symbol.end())
    {
        return ite->first;
    }
    else
    {
        Symbol[token] = Symbol.size();
        return token;
    }
}
string constant()
{
    ite = Digit.find(token);
    if (ite != Digit.end())
    {
        return ite->first;
    }
    else
    {
        Digit[token] = Digit.size();
        return token;
    }
}
string Three()
{
    ite = other.find(token);
    if (ite != other.end())
    {
        return ite->first;
    }
    else
    {
        other[token] = other.size();
        return token;
    }
}
void error()
{
    cout << token << "\t-->\t该单词不存在" << endl;
}
//词法分析函数,逐个识别单词
void LexAnalyze()
{
    token = "";
    getChar();
    getnbc();
    string val;
//    int num = -1;
    if(character>='a'&&character<='z' || character>='A'&&character<='Z')
    {
        while (letter() || digit())  //为字母或数字
        {
            concat();//追加到token末尾

            if(token=="odd")
            {
                getChar();
                Three();
                return ;
            }
            getChar();//读取下一个字符
        }
        retract();//回退一个字符
        {
            val = symbol();//查看标识符表

        }
    }
    else if(character>='0' && character<='9')
    {
        int flag=0;
        while (character>='0' && character<='9' || character>='a' && character<='z' || character>='A' && character <='Z')
        {
            if(character>='0' && character<='9')
                concat();

            if(character>='a' && character<='z' || character>='A' && character <='Z')
            {
                concat();
                flag=1;
            }
            getChar();
        }
        if(flag==1)
        {
            return error();
        }
        retract();
        val = constant();//查看常数表

    }
    else if(character=='<')
    {
        concat();
        getChar();
        if (character == '=')
        {
            concat();
            Three();
//返回<=符号
        }
        else if(character=='>')
        {
            concat();
            Three();
        }
        else
        {
            retract();
            Three();
//返回<符号
        }
    }
    else if(character=='>')
    {
        concat();
        getChar();
        if (character == '=')
        {
            concat();
            Three();
//返回>=符号
        }
        else
        {
            retract();
            Three();
//返回>符号
        }
    }
    else if(character=='=')
    {
        concat();
        getChar();
        if (character == '=')
        {
            concat();
            Three();
//返回==符号
        }
        else
        {
            retract();
            Three();
//返回=符号
        }
    }

    else if(character==':')
    {
        concat();
        getChar();
        if (character == '=')
        {
            concat();
            Three();

        }
        else
        {
            cout<<token<<endl;
            return error();
        }
    }

    else if(character=='+')
    {
        concat();
        Three();
    }


    else if(character=='-')
    {
        concat();
        Three();
    }

    else if(character=='*')
    {
        concat();
        Three();
    }


    else if(character=='/')
    {
        concat();
        Three();
    }


    else if(character=='(')
    {
        concat();
        Three();
    }

    else if(character==')')
    {
        concat();
        Three();
    }

    else if(character==',')
    {
        concat();
        Three();
    }

    else if(character=='.')
    {
        concat();
        Three();
    }

    else if(character==';')
    {
        concat();
        Three();
    }

    else
    {
        return error();
    }

}
void show_table()
{
    cout << "\n==================" << "标识符" << "==================" << endl;
//    cout << "标识符\t\t类别编码\t表中位置" << endl;
    for (ite=Symbol.begin(); ite != Symbol.end(); ite++)
    {
//        int flag=0;
//        for (int i = 0; i < len; i++)
//        {
               cout<<Reserve[i]<<endl;
//            if (Reserve[i] == ite->first)
//            {
//                cout<<ite->first<<"是保留字"<<"   ";
//                cout << "("<<ite->first <<","<<ite->second<<")" << endl;
//                flag=1;
//                break;
//            }
//
//
//        }
//        if(flag==0)
//            cout << "("<<ite->first <<","<<ite->second<<")" << endl;

        map<string,int>::iterator iter;
        iter=type.find(ite->first);
        if(iter!=type.end())
            cout << "("<<iter->second <<","<<ite->second<<")" << endl;
        else
            cout << "("<<type.size() <<","<<ite->second<<")" << endl;
    }
    cout << "\n==================" << "常数表" << "==================" << endl;
//    cout << "常量值\t\t类别编码\t表中位置" << endl;
    for (ite = Digit.begin(); ite != Digit.end(); ite++)
    {
//        cout << "("<<ite->first <<","<<ite->second<<")" << endl;
        map<string,int>::iterator iter;
        iter=type.find(ite->first);
//        if(iter!=type.end())
//            cout << "("<<iter->second <<","<<ite->second<<")" << endl;
//        else
        cout << "("<<type.size()-1 <<","<<ite->second<<")" << endl;
    }
    cout << "\n==================" << "算符、界符" << "==================" << endl;
    for (ite = other.begin(); ite != other.end(); ite++)
    {
//        cout << "("<<ite->first <<","<<ite->second<<")" << endl;
        map<string,int>::iterator iter;
        iter=type.find(ite->first);
        cout << "("<<iter->second <<","<<ite->second<<")" << endl;
//           cout<<iter->second<<endl;
    }
}
int main()
{
//    clock_t start,finish;
//    double totaltime;
//    start=clock();


    type["begin"]=type.size();
    type["end"]=type.size();
    type["if"]=type.size();
    type["then"]=type.size();
    type["while"]=type.size();
    type["do"]=type.size();
    type["const"]=type.size();
    type["var"]=type.size();
    type["call"]=type.size();
    type["procedure"]=type.size();
    type["+"]=type.size();
    type["-"]=type.size();
    type["*"]=type.size();
    type["/"]=type.size();
    type["odd"]=type.size();
    type["="]=type.size();
    type["<>"]=type.size();
    type["<"]=type.size();
    type[">"]=type.size();
    type["<="]=type.size();
    type[">="]=type.size();
    type[":="]=type.size();
    type["("]=type.size();
    type[")"]=type.size();
    type[","]=type.size();
    type["."]=type.size();
    type[";"]=type.size();
    type["常数"]=type.size();
    type["标识符"]=type.size();



    init_Reserve();//保留字表初始化
    Symbol.clear();//标识符集初始化
    Digit.clear();//常数集初始化
    index = 0;
    character = ' ';
    token = "";
    cout << "请输入待词法分析的源程序代码:输入文件结束符结束输入\n" << endl;
    //源程序代码输入处理
//    instr="var a,b,c; a:=1; b:=2; c:=a+b";


    char ch[1000];
    string ins;
    while(gets(ch))
    {
//        ins=ch;

        instr=ch;
        index=0;
        while (index < instr.size())
        {
            LexAnalyze();
        }
//        cout<<instr<<endl;
        memset(ch,0,sizeof(ch));
    }
//    cout<<instr<<endl;
//    string in;
//    while (cin >> in&&in != "@")
//    {
//        instr = instr + " " + in;
//    }
    //识别二元组初始化
//    while (index < instr.size())
//    {
//        LexAnalyze();
//    }
    //展示构造的各种词汇表
    show_table();

//    finish=clock();
//    totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
//    cout<<totaltime<<endl;
//    //system("pause");
    return 0;

}


 

测试结果:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的探索之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值