Corrected it..Everything works as expected ..now.... Any other suggestion..?
Code:
#include "lexer.h"
#include "token.h"
#include <iterator>
#include <string>
//#include <iostream>
//using std::cout;
using std::list;
using std::string;
list<token> tokenize(string s)
{
list<token> lt;
string::iterator sit;
enum State {nil,num,ifr,sym} state(nil);
//^nothing,number,identifier,other single char symbols
int start,length;
char cur; //current char
string section; //substr'ed string
bool uniflag(false);
//If a section is ready to be pushed_back
for(sit=s.begin();sit!=s.end();sit++)
{
uniflag = false;
cur = *sit;
if(cur>='0'&&cur<='9')
{
if(state==nil)
{
state=num;
start = sit - s.begin();
}
else if(state==ifr)
{
state=num;
length=sit - s.begin() - start ;
section = s.substr(start,length);
start = sit - s.begin();
uniflag = true;
}
else if(state==sym)
{
state=num;
length=sit - s.begin() - start ;
section = s.substr(start,length);
start = sit - s.begin();
uniflag = true;
}
}
else if(cur>='a'&&cur<='z')
{
if(state==nil)
{
state=ifr;
start = sit - s.begin();
}
else if(state==num)
{
state=ifr;
length=sit - s.begin() - start;
section = s.substr(start,length);
start = sit - s.begin();
uniflag = true;
}
else if(state==sym)
{
state=ifr;
length=sit - s.begin() - start;
section = s.substr(start,length);
start = sit - s.begin();
uniflag = true;
}
}
else //if(state==sym) ..
{
if(state==nil)
{
state=sym;
start = sit - s.begin();
}
else if(state==num)
{
state=sym;
length=sit - s.begin() - start;
section = s.substr(start,length);
start = sit - s.begin();
uniflag = true;
}
else if(state==ifr)
{
state=sym;
length=sit - s.begin() - start;
section = s.substr(start,length);
start = sit - s.begin();
uniflag = true;
}
}
if(uniflag==true)
{
// cout<<'\n'<<section<<'\n';
lt.push_back(token(section));
}
if((sit == s.end()-1))
{
length = s.end()-s.begin()-start;
section = s.substr(start,length);
// cout<<'\n'<<section<<'\n';
lt.push_back(token(section));
}
}
return lt;
}
The main part of the code is almost repeated 3 times...is there a way to make it common for the 3 cases(a function would require so many arguments ..that it'd become tedious)?....