编译原理词法分析小程序的设计

词法分析设计

题目描述

设一语言的关键词、运算符、分界符的个数与单词如下:
struct { int number; string str[10]; } keywords={3,”int”,”main”,”return”} ; //关键词
struct { int number; string str[10]; } operators ={5,”+”,”“,”=”,”+=”,”=”}; //运算符
struct { int number; string str[10]; } boundaries ={6,”(“,”)”,”{“,”}”,”,”,”;”} ; //分界符
struct { int number; string str[100];} identifieres={0}; //标识符
struct { int number; string str[100];} Unsigned_integer={0}; //无符号整数
以上类号分别为1~5,序号从0开始;
标识符是字母开头的字母数字串;常量为无符号整数;
用C++设计一程序实现词法分析

输入

输入一程序,结束符用”#”;

输出

输出单词数对:<类号,序号>。 输出标识符表,用空格分隔; 输出无符号整数表,用空格分隔;

样例

input:
main()
{ int a=2,b=3;
return 2*b+a;
}#

output:

<1,1><3,0><3,1><3,2><1,0><4,0><2,2><5,0><3,4><4,1><2,2><5,1><3,5><1,2><5,0><2,1><4,1><2,0><4,0><3,5><3,3>
identifieres:a b
Unsigned_integer:2 3

My code

#include <iostream>
#include <map>
#include <vector>
using namespace std;

vector<int> catagory, index; //保存输出的类号、序号对 

void store(int a, int b){
    catagory.push_back(a);
    index.push_back(b);
}

int main(){
    char ch;
    vector<string> integer;  //保存无符号整数 
    vector<string> identifier; //保存标识符 

    map<char,int> boundaries{{'(',0},{')',1},{'{',2},{'}',3},{',',4},{';',5}};
    map<string,int> keyword{{"int",0},{"main",1},{"return",2}};

    ch=cin.get();
    while(ch!='#'){

        if(ch==' '||ch=='\n'||ch=='\t'){ //跳过空格、制表符和换行符 
            ch=cin.get();
            continue;
        } 

        if(boundaries.count(ch)==1) store(3,boundaries[ch]);//识别分界符 

        //识别运算符 
        else if(ch=='+'){
            if((ch=cin.get())=='=') store(2,3);
            store(2,0);
            continue;
        }
        else if(ch=='*'){
            if((ch=cin.get())=='=') store(2,4);
            store(2,1);
            continue;
        }
        else if(ch=='=') store(2,2);

        //识别无符号整数 
        else if(ch>='0'&&ch<='9'){ 
            string temp;
            temp.push_back(ch);
            while((ch=cin.get())>='0'&&ch<='9'){//读入无符号整数 
                temp.push_back(ch);
            }

            int flag=0;
            for(int i=0; i<integer.size(); i++){//无符号整数已经在整数表中存在的情况 
                if(integer[i]==temp){
                    store(5,i);
                    flag=1;
                    break;
                }
            }

            if(flag==0){ //无符号整数没在整数表中 
                store(5,integer.size());
                integer.push_back(temp);
            }

            continue;
        }

        //识别标识符 
        else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')){
            string temp;
            temp.push_back(ch);
            while(((ch=cin.get())>='a'&&ch<='z')||(ch>='A'&&ch<='Z')|| //读取整个标识符 
                  (ch>='0'&&ch<='9')){
                    temp.push_back(ch);
                  }

            if(keyword.count(temp)==1){ //处理标识符是关键字的情况 
                store(1,keyword[temp]);
            }
            else{
                int flag=0;
                for(int i=0; i<identifier.size(); i++){//标识符已经存在 
                    if(identifier[i]==temp){
                        store(4,i);
                        flag=1;
                        break;
                    }
                }

                if(flag==0){//标识符是新的 
                    store(4,identifier.size());
                    identifier.push_back(temp);
                }

            }
            continue;
        }

        ch=cin.get();
    }

    for(int i=0; i<index.size(); i++){
        cout << "<" << catagory[i] << "," << index[i] << ">";
    }

    cout << "\nidentifier:";
    for(int i=0; i<identifier.size(); i++){
        if(i==identifier.size()-1) cout << identifier[i] << "\n";
        else cout << identifier[i] << " ";
    }

    cout << "Unsigned_integer:";
    for(int i=0; i<integer.size(); i++){
        if(i==integer.size()-1) cout << integer[i] << "\n";
        else cout << integer[i] << " ";
    }
}

运行结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值