Created
November 4, 2015 17:06
-
-
Save jaytaylor/11e509d9a8c58a31a7d1 to your computer and use it in GitHub Desktop.
Simple example of a query parser in C++.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
#include <string> | |
using namespace::std; | |
void parseTerms(string query, string components[][3]); | |
void parseToken(string token, string parsed[]); | |
int main() { | |
string query = "makemodel=acura nsx year=1997 price<=1500"; | |
string terms[3][3]; | |
parseTerms(query, terms); | |
for (int i = 0; i < 3; ++i) { | |
cout << "i=" << i << " [0]=\"" << terms[i][0] << "\" [1]=\"" << terms[i][1] << "\" [2]=\"" << terms[i][2] << "\"" << endl; | |
} | |
return 0; | |
} | |
void parseTerms(string query, string terms[][3]) { | |
stringstream ss(query); | |
string token; | |
int i = 0; | |
while (ss >> token) { | |
if (token.length() > 0) { | |
// cout << "i=" << i << " token=\"" << token << "\"" << endl; | |
parseToken(token, terms[i]); | |
// cout << "i=" << i << " [0]=\"" << terms[i][0] << "\" [1]=\"" << terms[i][1] << "\" [2]=\"" << terms[i][2] << "\"" << endl; | |
if (terms[i][1].length() == 0) { | |
if (i > 0) { | |
terms[i-1][2] += " " + terms[i][0]; | |
} | |
} else { | |
++i; | |
} | |
} | |
} | |
} | |
void parseToken(string token, string parsed[]) { | |
unsigned short stage = 0; | |
for (int i = 0; i < token.length(); ++i) { | |
switch(stage) { | |
case 0: | |
if (token[i] < 'A' || token[i] > 'z') { | |
++stage; | |
} | |
break; | |
case 1: | |
if (token[i] != '<' && token[i] != '>' && token[i] != '=' && token[i] != '!') { | |
++stage; | |
} | |
break; | |
} | |
parsed[stage] += token[i]; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace::std; | |
void parseTerms(string query, string components[][3]); | |
void parseToken(string token, string parsed[]); | |
int main() { | |
string query = "makemodel=acura nsx year=1997 price<=1500"; | |
string terms[3][3]; | |
parseTerms(query, terms); | |
for (int i = 0; i < 3; ++i) { | |
cout << "i=" << i << " [0]=\"" << terms[i][0] << "\" [1]=\"" << terms[i][1] << "\" [2]=\"" << terms[i][2] << "\"" << endl; | |
} | |
return 0; | |
} | |
void parseTerms(string query, string terms[][3]) { | |
string token; | |
int i = 0; | |
for (int j = 0; j < query.length(); ++j) { | |
if (query[j] != ' ') { | |
token += query[j]; | |
} | |
if (token.length() > 0 && (j == query.length() - 1 || query[j+1] == ' ')) { | |
// cout << "i=" << i << " token=\"" << token << "\"" << endl; | |
parseToken(token, terms[i]); | |
// cout << "i=" << i << " [0]=\"" << terms[i][0] << "\" [1]=\"" << terms[i][1] << "\" [2]=\"" << terms[i][2] << "\"" << endl; | |
if (terms[i][1].length() == 0) { | |
if (i > 0) { | |
terms[i-1][2] += " " + terms[i][0]; | |
} | |
} else { | |
++i; | |
} | |
token = ""; | |
} | |
} | |
} | |
void parseToken(string token, string parsed[]) { | |
unsigned short stage = 0; | |
for (int i = 0; i < token.length(); ++i) { | |
switch(stage) { | |
case 0: | |
if (token[i] < 'A' || token[i] > 'z') { | |
++stage; | |
} | |
break; | |
case 1: | |
if (token[i] != '<' && token[i] != '>' && token[i] != '=' && token[i] != '!') { | |
++stage; | |
} | |
break; | |
} | |
parsed[stage] += token[i]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment