OK, here is the start of my scanner class, but I just keep getting back that it doesn't recognise the token. maybe I'm blind, but I can't see what's wrong with the code, does someone mind looking over the listing for me?
Scanner:
enum TOKEN_TYPE{NUM,IDENT,COND,STRING};struct TokenStruct{ char delimiter; std::string alpha; std::string num; std::string other;};class Scanner{ protected: char m_delimiter;//defines the end of a line std::string m_alpha;//letters in both Upper and Lower case std::string m_num;//every numeric digit std::string m_other;//everything else TOKEN_TYPE token_type;//stores the type of token public: Scanner():m_delimiter(';'),m_alpha("_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),m_num("0123456789"),m_other("#\"=+-*/.:!<>,'{}[]()"){} Scanner(TokenStruct tokstr):m_delimiter(tokstr.delimiter),m_alpha(tokstr.alpha),m_num(tokstr.num),m_other(tokstr.other){} ~Scanner(){} //Note: the file pointer given to GetToken() must already be open. std::string GetToken(std::ifstream& fin) { std::string token=""; char tempchar; bool decimalpoint=false,whtspc=true;//decimalpoint is used to check if the token already has a decimal point. if it has, then the token is returned without the second decimal //test if the file is actually open if(!(fin.is_open())) return std::string("file not open"); //get rid of whitespace //do{ tempchar = fin.get(); //}while(isspace(tempchar)==0); if(strcmp(&tempchar,"")) tempchar = fin.get(); //set token type if(m_alpha.find(tempchar,0)!=std::string::npos) { token_type=IDENT; token.append((const char*)tempchar); } else if(m_num.find(tempchar,0)!=std::string::npos) { token_type=NUM; } else if(m_other.find(tempchar,0)!=std::string::npos) { if(!strcmp((const char*)tempchar,"\"")) token_type=STRING; else token_type=COND; } else return std::string("token type not recognised"); return token; //time to start reading in the token } std::vector<std::string> TokenizeLine(std::ifstream& fin) { std::vector<std::string> ret_vec; std::string temp; size_t line_size; while(!(temp[0]==m_delimiter)) { temp=GetToken(fin); } return ret_vec; }};
main.cpp:
#include "tokeniser.h"#include <fstream>using namespace std;int main(){ Scanner tokens; ifstream fin; fin.open("test.txt"); std::string myString; std::vector<std::string> myVector; myString=tokens.GetToken(fin); //myVector=tokens.TokenizeLine(fin); std::cout << myString << std::endl; fin.close();}
test.txt:
a; a; a;a;abc;ab_;ab_c;a1;a1_;a_1;a,b;_a;_a_b;_1;2;234;2.3;2.34;23.4;23.45;23.;"The (quick) br0wn f0x j&umped over_the_lazy_dog";(;::;==;<=;>=;!=;++;--;=!;<";>%;:[;+-;-+;#hell, we made it!
the
return token
at the end of the Scanner::GetToken() method is just there until I know that this section is working
[Edited by - webwraith on July 4, 2006 3:26:34 PM]