1. 程式人生 > >編譯原理丨第七週 ——1000. 詞法分析程式設計 **

編譯原理丨第七週 ——1000. 詞法分析程式設計 **

Description

設一語言的關鍵詞、運算子、分界符的個數與單詞如下: 
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

輸入一程式,結束符用”#”;

Output

輸出單詞數對:<類號,序號>。 輸出識別符號表,用空格分隔; 輸出無符號整數表,用空格分隔;

Sample Input  Copy sample input to clipboard
main()
{ int a=2,b=3;
  return 2*b+a;
}#
Sample 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

Problem Source: 詞法分析

編譯原理西西里的題目,還是比較簡單的,需要注意的是要用cin.get(ch),不然讀不進空格

// Problem#: 20907
// Submission#: 5186361
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
//
//  main.cpp
//  cifa
//
//  Created by xie on 2017/10/18.
//  Copyright © 2017年 xie. All rights reserved.
//

#include <iostream>


using namespace std;

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}; //無符號整數


void handle(string my_str)
{
    string temp_int = "";//用來暫時儲存無符號整數的一部分
    string temp_word = "";//用來暫時儲存字串


    for(int i = 0;i<my_str.size();++i)
    {
        if(my_str[i] == ' ' || my_str[i] == '\n')//空格與換行不用處理
            continue;

        //先處理分界符的情況
        if(my_str[i] == '(') 
        {
            cout<<"<3,0>";
            continue;
        }   
        if(my_str[i] == ')') 
        {
            cout<<"<3,1>";
            continue;
        }
        if(my_str[i] == '{') 
        {
            cout<<"<3,2>";
            continue;
        }
        if(my_str[i] == '}') 
        {
            cout<<"<3,3>";
            continue;
        }
        if(my_str[i] == ',') 
        {
            cout<<"<3,4>";
            continue;
        }
        if(my_str[i] == ';') 
        {
            cout<<"<3,5>";
            continue;
        }



        //再到運算子
        if(my_str[i] == '=')
        {
            cout<<"<2,2>";
            continue;
        }
        if(my_str[i] == '+' )
        {
            if(my_str[i+1] == '=')
            {
                cout<<"<2,3>";
                continue;
            }
            else
            {
                cout<<"<2,0>";
                continue;
            }
        }
        if(my_str[i] == '*')
        {
            if(my_str[i+1] == '=')
            {
                cout<<"2,4";
                continue;
            }
            else
            {
                cout<<"<2,1>";
                continue;
            }
        }

        //再到無符號整數
        if(isdigit(my_str[i]))
        {
            temp_int += my_str[i];
            if(isdigit(my_str[i+1]))//如果下一位還是數字的話,留到下一次再處理
                continue;
            else
            {
                bool flag = false;//判斷數字在Unsigned_integer中是否已經存在
                for(int j = 0;j < Unsigned_integer.number;++j)
                {
                    if(temp_int == Unsigned_integer.str[j])
                    {
                        cout<<"<5,"<<j<<">";
                        temp_int = "";//記得復原temp_int
                        flag = true;
                        break;
                    }
                }
                if(flag == false)
                {
                    cout<<"<5,"<<Unsigned_integer.number<<">";
                    Unsigned_integer.str[Unsigned_integer.number++] = temp_int;
                    temp_int = "";
                    
                }
            }
        }

        if(isalpha(my_str[i]))
        {
            temp_word += my_str[i];
            if(('a'<=my_str[i+1]&&my_str[i+1]<='z')||('A'<=my_str[i+1]&&my_str[i+1]<='Z'))
            {
                //cout<<endl<<my_str[i+1];
                continue;
            }


            bool flag1 = false;
            for(int j = 0;j<3;++j)//處理關鍵字
            {
                if(temp_word == keywords.str[j])
                {
                    cout<<"<1,"<<j<<">";
                    flag1 = true;
                    temp_word = "";
                    break;
                }
            }
            if(flag1) continue;
            
            bool flag = false;//處理識別符號,過程與上面類似
            for(int j = 0;j<identifieres.number;++j)
            {
                if(temp_word == identifieres.str[j])
                {
                    cout<<"<4,"<<j<<">";
                    temp_word = "";
                    flag = true;
                    break;
                }
            }

            if(!flag)
            {
                cout<<"<4,"<<identifieres.number<<">";
                identifieres.str[identifieres.number++] = temp_word;
                temp_word = "";
                
            }



        }



    }

    cout<<endl;
    cout<<"identifieres:";

    if(identifieres.number == 0)
        ;
    else    
        for(int i = 0;i<identifieres.number;++i)
            cout<<identifieres.str[i]<<" ";
    cout<<endl;

    cout<<"Unsigned_integer:";

    if(Unsigned_integer.number == 0)
        ;
    else
        for(int i = 0;i<Unsigned_integer.number;++i)
            cout<<Unsigned_integer.str[i]<<" ";

    cout<<endl;





}





int main(int argc, const char * argv[]) {
    
    char temp_char;
    string temp_string;
    
    while(cin.get(temp_char) && temp_char != '#')
        temp_string += temp_char;

    //cout<<temp_string;
    handle(temp_string);

    return 0;
}