Yet another problem with substr
I'm writing a lexer to generate a list of tokens from a std::string..
The string called section is not getting the values it should...(if there is nothing else wrong)..
The code is as follows...the problem(almost surely) is at the bold regions..
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
for(sit=s.begin();sit!=s.end();sit++)
{
static int start,length;
static char cur; //current char
static string section; //substr'ed string
static bool uniflag(false);
//If a section is ready to be pushed_back
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(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
{
state = sym;
section = cur;
uniflag = true;
}
if(uniflag==true)
{
// cout<<'\n'<<section<<'\n';
lt.push_back(token(section));
}
}
return lt;
}