2013-05-21 171 views
1

我需要從鍵盤(標準輸入)讀取以下文本。請注意,它將僅由用戶通過鍵盤以這種格式輸入。從鍵盤讀取輸入

#the total size of physical memory (units are B, KB, MB, GB) 
    512MB 2  #the following are memory allocations 
    { 
      abc = alloc(1KB); 
      { 
       y_ = alloc(128MB); 
       x1= alloc(128MB); 
       y_ = alloc(32MB); 
       for (i = 0; i < 256; i++) abc[i] =alloc(512kB); 
        x1 = alloc(32MB); x2 = alloc(32MB); x3 = alloc(32MB); 
       x1.next = x2, x2.next = x3, x3.next = x1; 
    } 
    abc = alloc(256MB); 
    } 

所以基本上讓我分解它。 以#號開頭的行被視爲註釋並被忽略。 前兩個分配是物理內存大小和世代數。 將打開一個全局支架。 並且它可以後跟行稱爲

abc = alloc(1KB); 

其中ABC是對象名稱,1KB是分配的存儲器的大小。 x1.next = x2,這裏x1指向x2。

for (i = 0; i < 256; i++) abc[i] =alloc(512kB); 

以此格式輸入的for循環操作系統,它可以具有相同的行命令或可以嵌套for循環。

我有以下的代碼,照顧這個有點。我想知道它的改進。請幫忙。

而且我的代碼是這樣的:

#include <iostream> 
#include <algorithm> 
#include <string> 
#include <iomanip> 
#include <limits> 
#include <stdio.h> 
#include <sstream> 



using namespace std; 
using std::stringstream; 

string pMem,sGen, comment,val,input,input_for,id_size,id,init_str1, init_str2, inc_str, id_dummy,s_out,sss, id_dummy1; 
int gen=0, pMem_int=0,i=0, gBrckt =0,cBrckt=0, oBrckt=0, id_size_int,v1,v2, for_oBrckt=0,for_cBrckt=0,y=0, y1=0, g=0; 
unsigned long pMem_ulong =0, id_size_ulong; 
char t[20], m[256], init1[10],init2[10],inc[10]; 
unsigned pos_start, pos,pos_strt=0,pos_end=0; 

string extract(string pMem_extract); 
unsigned long toByte(int pMem_int_func, string val); 
void commentIgnore(string& input); 
void func_insert(); 
void func_insert_for(); 
stringstream out; 
void commentIgnore_for(string& input_for); 

int main() 
{ 
    /* Reading the input main memory and num of generations */ 
    /* Ignoring comment line */ 
    cin >> pMem; 
    if(pMem == "#") { 
    cin.clear(); 
    pMem.clear(); 
    getline(cin,comment); 
    cin >> pMem; 
    } 
    if(pMem == "#") { 
    cin.clear(); 
    pMem.clear(); 
    getline(cin,comment); 
    cin >> pMem; 
    } 
    if(pMem == "#") { 
    cin.clear(); 
    pMem.clear(); 
    getline(cin,comment); 
    cin >> pMem; 
    } 
    /* Reading input generations */ 
    cin>> sGen; 
    if(sGen == "#") { 
    cin.clear(); 
    sGen.clear(); 
    getline(cin,comment); 
    cin >> sGen; 
    } 
    if(sGen == "#") { 
    cin.clear(); 
    sGen.clear(); 
    getline(cin,comment); 
    cin >> sGen; 
    } 
    if(sGen == "#") { 
    cin.clear(); 
    sGen.clear(); 
    getline(cin,comment); 
    cin >> sGen; 
    } 
    /* Convert sGen and physical memory to int and report error if not a number */ 
    gen = atoi(sGen.c_str()); 
    if(gen ==0) { 
    cerr << "Generation must be a number"<<endl; 
     exit(0); 
    } 
    pMem_int = atoi(pMem.c_str()); 
    // cout<< gen<<" "<<pMem_int<<endl; 

    /* Now that the number from pMem is removed, get its unit B,MB,KB */ 
    extract(pMem); /* returns val(string) */ 

    /* convert the given physical memory to Byte. input: pMem_int*/ 
    toByte(pMem_int, val); /* return(pMem_ulong)*/ 
    // move pMem_ulond to another location to keep address intact 
    /* read rest of the inputs */ 
    /* Ignore comment lines before the global bracket */ 
    cin >> input; 
    if(input == "#"){ 
    cin.clear(); 
    input.clear(); 
    getline(cin,comment); 
    cin >> input; 
    } 
    if(input == "#"){ 
    cin.clear(); 
    input.clear(); 
    getline(cin,comment); 
    cin >> input; 
    } 
    if(input == "#"){ 
    cin.clear(); 
    input.clear(); 
    getline(cin,comment); 
    cin >> input; 
    } 

    if(input.compare("{") ==0) 
    gBrckt=1; 

    else { 
    cerr<< "Syntax error\n"; 
    exit(0); 
    } 

    /* Clearing the input stream for next input */ 
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
    cin.clear(); 
    input.clear(); 
    //cout<<"input: "<<input<<endl; 
    while(getline(cin,input)) { 

    if(input == "CTRL-D") 
     break; 

    commentIgnore(input); 
    //cout<<"inputloop: "<<input<<endl; 

    /* If input = '{' or '}'*/ 
    if(input.compare("{") ==0) 
     oBrckt = oBrckt + 1; 

    if (input.compare("}") ==0) 
     cBrckt = cBrckt + 1; 

    if (((input.find("alloc"))!= string::npos) && (input.find("alloc") < input.find("for"))) { 
     func_insert(); 
     //call the allocate function here with name: id, size: id_size_ulong 
    } 

    if ((input.find("for")) != string::npos) { 
     sscanf(input.c_str(), "for (%s = %d; %s < %d; %[^)])", init1, &v1, init2, &v2, inc); 
    init_str1 = init1, init_str2 = init2, inc_str = inc; 

    cout<<init1<<" ="<< v1<<" "<<init_str1<<" < " << v2<< " "<< inc_str<<endl; 
    cout << input <<endl; 

    if(init_str1 != init_str2) { 
     cerr << "Error!\n"; 
     exit(0); 
    } 

    if ((input.find("alloc"))!= string::npos) { 
     // unsigned pos = (input.find("alloc")); 

     if((input.find(";")) != string::npos) { 

     pos_start = (input.find(")")+1); 
     string alloc_substr = input.substr(pos_start); 
     cout<<"Substring alloc: "<< alloc_substr<<endl; 

     func_insert(); 
     //call the allocate function here with name: id, size: id_size_ulong 
     } 
     else { 
     cerr << "ERROR: SYNTAX\n"; 
     exit(0); 
     } 


    } 
    // cin.ignore(); 
    while(getline(cin,input_for)) { 
     commentIgnore_for(input_for); 

     if ((input_for.find("{") != string::npos)) { 
     pos = input_for.find("{"); 
     for_oBrckt = for_oBrckt+1; 
     string for_brckt = input_for.substr(pos,pos); 
     cout<< "Found: " << for_oBrckt<<endl; 
     } 


     if ((input_for.find("}") != string::npos)) { 
     pos = input_for.find("}"); 
     for_cBrckt = for_cBrckt+1; 
     string for_brckt = input_for.substr(pos,pos); 
     cout<< "Found: " << for_cBrckt<<endl; 
     } 

     if (((input_for.find("alloc"))!= string::npos) && (input_for.find("alloc") < input_for.find("for"))) { 
     func_insert_for(); 
     //call the allocate function here with name: id, size: id_size_ulong 
     } 




     if(for_oBrckt == for_cBrckt) 
     break; 


    } 
    cout<<"out of break"<<endl; 
    } 

     if (((input.find(".next"))!= string::npos) && (input.find(".next") < input.find("for"))) { 
     func_insert(); 
     //call the allocate function here with name: id, size: id_size_ulong 
    } 

     if(((cBrckt-oBrckt)) == gBrckt) 
     break; 
    } 

} 

/*---------------------- Function definitions --------------------------------*/ 
/* Function to extract the string part of physical memory */ 
string extract(string pMem_extract) { 
    i=0; 
    const char *p = pMem_extract.c_str(); 
    for(i=0; i<=(pMem_extract.length()); i++) { 
    if (*p=='0'|| *p=='1'|| *p=='2'|| *p=='3'|| *p =='4'|| *p=='5'|| *p=='6'|| *p=='7'|| *p=='8'|| *p=='9') 
     *p++; 
    else { 
     val = pMem_extract.substr(i); 
     return(val); 
    } 
    } 
} 

/* Convert the physical memory to bytes. return(pMem_ulong);*/ 
unsigned long toByte(int pMem_int_func, string val) 
{ 
    if (val == "KB") 
    pMem_ulong = (unsigned long) pMem_int_func * 1024; 
    else if (val == "B") 
    pMem_ulong = (unsigned long) pMem_int_func; 
    else if (val == "GB") 
    pMem_ulong = (unsigned long) pMem_int_func * 1073741824; 
    else if (val == "MB") 
    pMem_ulong = (unsigned long) pMem_int_func * 1048576; 
    else { 
    cerr<<"Missing the value in memory, B, KB, MB, GB\n"; 
    exit(0); 
    } 

    return(pMem_ulong); 
} 


/*Ignoring comment line*/ 
void commentIgnore(string& input) 
{ 
    unsigned found = input.find('#'); 

    if (found!=std::string::npos) 
    input= input.erase(found); 

    else 
    return; 
    return; 
} 


void func_insert() { 
sscanf(input.c_str(), "%s = alloc(%[^)]);", t, m); 
     id =t; 
     id_size =m; 
     cout<<"Tag: "<<id <<" Memory: "<<id_size<<endl; 
     extract(id_size); /* Separates B,MB,KB and GB of input, returns val*/ 
     id_size_int = atoi(id_size.c_str()); 
     /* Convert object size to B */ 
     toByte(id_size_int, val); /* return(pMem_ulong) */ 
     id_size_ulong = pMem_ulong; 

} 

void func_insert_for() { 
    sscanf(input_for.c_str(), "%s = alloc(%[^)]);", t, m); 
    id =t; 
    id_size =m; 
    if(!((id.find("[")) && (id.find("]")) != string::npos)) { 
    cout<<"Tag: "<<id <<" Memory: "<<id_size<<endl; 
    extract(id_size); /* Separates B,MB,KB and GB of input, returns val*/ 
    id_size_int = atoi(id_size.c_str()); 
    /* Convert object size to B */ 
    toByte(id_size_int, val); /* return(pMem_ulong) */ 
    id_size_ulong = pMem_ulong; 
    // allocate here 
    return; 
    } 
    else { 
    if(inc_str.find("++")) 
     y1 =1; 
    if(inc_str.find("=")) 
     { 
    sss = inc_str.substr(inc_str.find("+") +1); 
    y1 = atoi(sss.c_str()); 
    cout<<"y1:"<<y1<<endl; 

     } 
    pos_strt = id.find("["); 
    pos_end = id.find("]") -1; 
    cout<<"Positions start and ebd: " << pos_strt<<pos_end<<endl; 
    id_dummy = id.substr(0,pos_strt); 
    id = id_dummy; 
    cout<<"Tag: "<<id_dummy <<" Memory: "<<id_size<<endl; 
    extract(id_size); /* Separates B,MB,KB and GB of input, returns val*/ 
    id_size_int = atoi(id_size.c_str()); 
    /* Convert object size to B */ 
    toByte(id_size_int, val); /* return(pMem_ulong) */ 
    id_size_ulong = pMem_ulong; 
    //allocate here 
    cout<<"v1: " << v1 << " " << v2<<endl; 
    // g = 0; 
    for(y = v1; y < v2; y= y+y1) { 
     // allocate here 
    } 
    } 
    return; 
} 

void commentIgnore_for(string& input_for) 
{ 
    unsigned found = input_for.find('#'); 

    if (found!=std::string::npos) 
    input_for= input_for.erase(found); 

    else 
    return; 
    return; 
} 

而且我被要求做空白兼容它。這意味着輸入也可以輸入到一行中。像一條線上的兩個分配。我一直無法照顧。我需要幫助。

+1

我認爲這可能更適合[http://codereview.stackexchange.com/](http://codereview.stackexchange.com/) –

+0

@CaptainObvlious:我有一個問題在那裏看到一個問題.. –

+0

我需要使它與空白兼容。這意味着輸入也可以輸入到一行中。像一條線上的兩個分配。我不認爲我已經照顧過。我需要幫助。 – Tuffy

回答

1

我的建議是編寫一個合適的標記器 - 一段能夠理解屬於什麼的代碼(比如「單詞」),以及在哪裏分裂(例如「(,),{,}」)。標記生成器將返回一個枚舉,這樣的事情:

enum Token { 
    Token_Unknown, // Error indiciation. 
    Token_LeftParen, 
    Token_RightParen, 
    Token_LeftBracket, 
    Token_RightBracket, 
    Token_Comma, 
    Token_Semicolon, 
    Token_Equal, 
    Token_Word,  // Sequence of alphanumerics 
}; 

一旦你有一個道理,你要明白,令牌是什麼。擁有「已知」令牌(又名「關鍵字」)的表格可能是有意義的,例如「mem」,「alloc」,「for」等。如果某個關鍵字不是關鍵字,那麼它就是一個符號的名稱,例如變量。將它們存儲在一個表中,以便稍後可以引用它們。

你將不得不使用某種堆疊的位置,所以當你完成後你可以回到你來自的地方。

編寫一個通用的解析器並不難,而且你最有可能最終得到的代碼少於當前代碼。

當然,你可以擺脫一切:

if(input == "#"){ 
    cin.clear(); 
    input.clear(); 
    getline(cin,comment); 
    cin >> input; 
    } 

只是讓解析器檢查是否有'#'作爲輸入,並跳過該行的末尾(完當前令牌如果有的話,如果沒有的話,繼續前進)。

+0

將試試這一點。謝謝.. – Tuffy

+0

有免費的工具可以生成標記器和解析器。應該使用這些,而不是用手寫一切。 –

+0

爲什麼'clear()'在你看到評論的開始之後?只需跳過,直到看到下一行。 (這可能是標記器中的一個規則,或者您可以在輸入流中插入過濾流緩衝區。) –

1

IIUC,輸入是面向行的,所以使用std::getline可能不適用的 通常的規則。我自己的方法是:

  • 插入過濾streambuf以刪除註釋。

  • 使用某種基於正則表達式的詞法分析器(例如flex) 將輸入分爲標記。

  • 定義語法;一旦我得到了這麼多,我會使用bison, 爲了生成解析語法的代碼,但簡單的遞歸下降解析器不應該太難編寫。

你不說你必須用信息做什麼,一旦你 已經解析它,所以它很難給更精確的建議。

+0

基本上,我分配給定的物理內存並將其除以世代數。當我遇到alloc時,我用物理內存中由alloc指定的內存分配對象。 – Tuffy

+0

在for循環中,我遞歸地分配。這是我正在處理的垃圾收集算法。 x1.net = x2使x1指向x2。它是一個鏈表。 – Tuffy

+0

它是一個標記和代數算法的掃描 – Tuffy