Issue with simple bool function
Mar 7, 2012 at 9:35pm UTC
Hey, I'm sure that given enough time I'll simply find the very stupid mistake I've made, but I was wondering if anyone could tell me where I'm screwing up on this and maybe a duct-tape fix? (Haha)
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<cmath>
#include<cstdlib>
#include "lexer.h"
using namespace std;
//How to make a vector matrix:
// vector<vector<type>> name
string trim(string x)
{
x = x.substr(1, (x.size()-1));
return x;
}
bool hasParentheses(string x)
{
if ((x.at(0) == '(' ) && (x.at(x.size()-1) == ')' ))
return true ;
else
return false ;
}
void display(vector<Token> x)
{
for (int i = 0; i < x.size(); i++)
cout << x[i].value << "\t" ;
}
void display(vector< vector<Token> > x)
{
for (int i = 0; i < x.size(); i++)
{
for (int j = 0; j < x[i].size(); j++)
{
cout << x[i][j].value << "\t" ;
}
cout << endl;
}
}
int main()
{
//Variables
Token tok;
Lexer lex;
string input;
// "display" variables
string file;
string sortedBy;
char * offnen;
fstream doc;
int numColumns;
vector<string> categories;
Token tok2;
vector< vector<Token> > docMatrix;
vector<Token> columns;
vector< vector<Token> > temptrix;
//Needs to be a vector or it can't be pushed onto the vector matrix
vector<Token> * line;
////////////////////////////////////////////////////////////
// MAIN MENU
////////////////////////////////////////////////////////////
while (true )
{
cout << ">" ;
getline(cin, input);
lex.set_input(input);
tok = lex.next_token();
if (tok.value == "display" )
{
file = lex.next_token().value;
cout << file << endl;
if (lex.next_token().value != "sortedby" )
goto bad;
sortedBy = lex.next_token().value;
cout << sortedBy << endl;
offnen = (char *) file.c_str();
doc.open(offnen);
while (!doc.eof())
{
getline(doc, input);
lex.set_input(input);
//Potential for error IF:
//There is anything else with parentheses
while (lex.has_more_token())
{
//Error here
while (true )
{
tok = lex.next_token();
tok2 = lex.next_token();
if (hasParentheses(tok2.value) == true ) // <-- Error here!
//{
//columns.push_back(tok);
//numColumns++;
//}
//else
break ;
}
lex.next_token();
//docMatrix.push_back(columns);
/*
while(lex.has_more_token())
{
//tok = lex.next_token();
line = new vector<Token>;
for(int i = 0; i < numColumns; i++)
{
tok = lex.next_token();
line->push_back(tok);
}
docMatrix.push_back((*line));
}
*/
}
}
display(docMatrix);
}
else if (tok.value == "nljoin" )
{
}
else if (tok.value == "smjoin" )
{
}
else if (tok.value == "exit" )
{
break ;
}
else
{
bad:
cout << "Incorrect input" << endl;
}
}
return 0;
}
//Code for potential reuse
/*
//traverses from row 1 to end
for(int i = 1; i < docMatrix.size(); i++)
{
if(docMatrix[i].size() < columns)
{
docMatrix[i].push_back(tok);
break;
}
}
//Now do it again, but this time for tok2
for(int i = 1; i < docMatrix.size(); i++)
{
if(docMatrix[i].size() < columns)
{
docMatrix[i].push_back(tok2);
break;
}
}
*/
/*
//Now vector matrix should be filled with tokens
//so let's display it
for(int i = 0; i < docMatrix.size(); i++)
{
for(int j = 0; j < docMatrix[i].size(); j++)
cout << docMatrix[i][j].value << "\t";
cout << endl;
}
doc.close();
*/
And here's the lexer class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/**
* *****************************************************************************
* file name : Lexer.cpp
* author : Hung Q. Ngo
* description: implementation of Lexer interface
* *****************************************************************************
*/
#include "Lexer.h"
#include <iostream>
using namespace std;
/**
* -----------------------------------------------------------------------------
* scan and return the next token
* cur_pos then points to one position right past the token
* the token type is set to ERRTOK on error, at that point the global state
* variable err will be set to true
* -----------------------------------------------------------------------------
*/
Token Lexer::next_token() {
Token ret;
size_t last;
if (in_err) {
ret.type = ERRTOK;
ret.value = "" ;
return ret;
}
// if not in error state, the default token is the ENDTOK
ret.type = ENDTOK;
ret.value = "" ;
if (has_more_token()) {
last = cur_pos; // input_str[last] is a non-space char
if (input_str[cur_pos] == '"' ) {
cur_pos++;
while (cur_pos < input_str.length() && input_str[cur_pos] != '"' )
cur_pos++;
if (cur_pos < input_str.length()) {
ret.type = STRING;
ret.value = input_str.substr(last+1, cur_pos-last-1);
cur_pos++; // move past the closing "
} else {
in_err = true ;
ret.type = ERRTOK;
ret.value = "" ;
}
} else {
while (cur_pos < input_str.length() &&
separators.find(input_str[cur_pos]) == string::npos &&
input_str[cur_pos] != '"' ) {
cur_pos++;
}
ret.type = IDENT;
ret.value = input_str.substr(last, cur_pos-last);
}
}
return ret;
}
/**
* -----------------------------------------------------------------------------
* set a new input string, restart
* -----------------------------------------------------------------------------
*/
void Lexer::set_input(string str) {
input_str = str;
restart();
}
/**
* -----------------------------------------------------------------------------
* -----------------------------------------------------------------------------
*/
bool Lexer::has_more_token() {
while (cur_pos < input_str.length() &&
separators.find(input_str[cur_pos]) != string::npos) {
cur_pos++;
}
return (cur_pos < input_str.length());
}
/**
* -----------------------------------------------------------------------------
* restart from the beginning, reset error states
* -----------------------------------------------------------------------------
*/
void Lexer::restart() {
cur_pos = 0;
in_err = false ;
}
The Lexer is provided by my instructor, so I don't intend to edit that. I just can't seem to figure out what I'm doing wrong.
Mar 8, 2012 at 11:01pm UTC
Hi
Could you specify and make clear where you have a problem and what does not work ?
by the way you may simplify your
bool hasParentheses(string x)
as
1 2 3 4 5
bool hasParentheses(const string& x){
return x[0] == '(' && x[ x.size() - 1 ] == ')' ;
}
Mar 9, 2012 at 11:53pm UTC
Haha, well I edited my hasParentheses function anyway so that's all working now, but why the const and the string&? I almost never use those, so what's the advantage?
Topic archived. No new replies allowed.