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

词法分析设计

题目描述

设一语言的关键词、运算符、分界符的个数与单词如下:
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 
#include 
#include 
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//无符号整数已经在整数表中存在的情况 
                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//标识符已经存在 
                    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; icout << "<" << catagory[i] << "," << index[i] << ">";
    }

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

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

运行结果:
编译原理词法分析小程序的设计_第1张图片

你可能感兴趣的:(编译原理)