Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Created November 4, 2015 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaytaylor/11e509d9a8c58a31a7d1 to your computer and use it in GitHub Desktop.
Save jaytaylor/11e509d9a8c58a31a7d1 to your computer and use it in GitHub Desktop.
Simple example of a query parser in C++.
#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];
}
}
#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