所以我有一個簡單的回車符分隔的文件,上面寫着:解析簡單的語法,只有關鍵字和數抽取
room(800,400)
place(desk, 5, 6)
place(chair, 8, 5)
place(bed, 6, 6)
place(closet, 1, 4)
我試圖存儲每個關鍵字(桌,椅,牀和衣櫃的發生)和相關的X,Y和存儲的地方(不重要!)並提取room
尺寸,並再次在一些地方保存。我的代碼如下所示:
#include <iostream>
#include <fstream>
#include <string>
using namepace std;
void Keyword(ifstream & stream, string token) {
string line;
while (getline(stream, line)) {
if (line.find(token) != string::npos) {
cout << line << endl;
if(token == "room") {
//store the numbers and string to somewhere
}
if (token == "table") {
//store the numbers and string to somewhere
}
}
}
cout << token << " Not Found!" << endl;
}
int main()
{
// Shape Grammar Parser
ifstream infile("shape.dat");
Keyword(infile, "room");
return 0;
}
我所試圖做的是,當解析器看到place(chair, 8, 5)
它存儲在數據結構chair, 8, 5
,或者當它看到房間它提取room, 800, 400
。
然而上述實施打破與此實現我只能提取的椅子,而不是相關的數字。如何做到這一點?我對正則表達式完全沒有經驗,所以我沒有嘗試它。
輝煌。這是我一直在尋找的東西! –