So I've not a professional developer, but I dable in programming periodically. I'm looking to write a code, and looking for some advice on managing a parser which is reading a text file, looking at each line as a string, and trying to determine the input on that line. Any given line could be one of over 1000 different keywords, which is the difficult part. Once I have this keyword, I feel like there has to be a significantly more efficiently method of determining what it is rather than implementing 1000 if-else statements or 1000 case-break statements. Once I matched the given keyword, I plan to jump to a general routine that instantiates an object of that keyword type. I don't want to have to perform 999 tests before I find my target, it's just a waste i feel like. I tried breaking it down by alphabetical order, which reduces it greatly, but there are still an unmanageably large amount of if-else statements.
I already found out that I can't nest more than 128 if-else statements, so my current alternative is 1000s of just "if" statements without matching "else" statements, which I know is a bad practice. So here is a generalization of my current code:
if (keyword_str.compare(keyword1)) {
Parse(keyword1); // A general routine to parse all these similarly formatted keywords
}
if (keyword_str.compare(keyword2)) {
Parse(keyword2);
}
if (keyword_str.compare(keyword3)) {
Parse(keyword3);
}
//
//
//
if (keyword_str.compare(keyword999)) {
Parse(keyword999);
}
if (keyword_str.compare(keyword1000)) {
Parse(keyword1000);
}
Any help will be greatly appreciated! Thanks!
Okay, so here is the point I'm at, but still kind of lost on how to use a map to determine an object type, and then instantiate that object. Here are some code snippets:
class baseClass
{
public:
baseClass();
~baseClass();
};
//
// keyword1 class declaration
class keyword1 : public baseClass
{
public:
// Constructors
keyword1 () { cout << "keyword1 constructor..." << endl;}
~keyword1 () { cout << "keyword1 destructor..." << endl;}
protected:
};
//
// keyword2 class declaration
class keyword2 : public baseClass
{
public:
// Constructors
keyword2 () { cout << "keyword2 constructor..." << endl;}
~keyword2 () { cout << "keyword2 destructor..." << endl;}
protected:
};
//
// keyword3 class declaration
class keyword3 : public baseClass
{
public:
// Constructors
keyword3 () { cout << "keyword3 constructor..." << endl;}
~keyword3 () { cout << "keyword3 destructor..." << endl;}
protected:
};
//
//*******************
map <string, baseClass> keyword_map;
keyword_map.insert (make_pair ("keyword1", keyword1 )); // ########## This is where I'm lost
keyword_map.insert (make_pair ("keyword2", keyword2 )); // ########## This is where I'm lost
keyword_map.insert (make_pair ("keyword3", keyword3 )); // ########## This is where I'm lost
// Search for keyword
string searching_for = "keyword3";
map <string, baseClass> ::const_iterator it = keyword_map.find(searching_for);
if (it == keyword_map.end()) {
cout << "No keyword found." << endl;
}
else
{
cout << "Found the keyword!" << endl;
it->second; // ########## This is where I'm lost
}