String Tokenizer

When you are writing a lexical analyzer it would be helpful to have a class like the StreamTokenizer class from Sun's Java, so I've done something like that and here it is the CStringTokenizer class, the usage of this class is the same as by the StreamTokenizer from Java, there are a few additional functionality's and the function names are slightly different

The CStringTokenizer class is contained in the files StringTokenizer.h and StringTokenizer.cpp


The interface of the class:

class CStringTokenizer : public CObject 

    // Constructor, you must pass as parameter the string, it initializes the 
    // tokenizer with  the default settings (see implementation)
    CStringTokenizer(CString& string);    
    virtual ~CStringTokenizer();        // Destructor

// Private stuff  for internal use (see the sample code)


    double GetNumValue();    // returns numeric value of the last returned token
    void PascalComments(BOOL bFlag);    // Enable / disable Pascal comments
    CString GetStrValue();    // returns the string value of the last token
    void QuoteChar(int ch);    // specifies that this char is used as quote
    int LineNo();    // returns the current line number
    void PushBack();    // push back a token (can not be used twice) 
    int NextToken();    // parse next token returns a TT_ constant or a char value
    void LowerCaseMode(BOOL bFlag);    // Enable / Disable lower case
    void SlSlComments(BOOL bFlag);    // Enable / Disable "//" comments
    void SlStComments(BOOL bFlag);    // Enable / Disable "/*" comments
    void EolIsSignificant(BOOL bFlag);    // Is true is set EOL is returned by Next Token as a token
    void ParseNumbers();    // Enables number parsing (integer / double in normal format)
    void ResetSyntax();    // reset syntax
    void WordChars(int cLow, int cHi);    // specify that the characters in the range are word characters
    void WhiteSpaceChars(int cLow, int cHi);    // specify that the characters in the range are white space characters
    void OrdinaryChars(int cLow, int cHi);    // specify that the characters in the range are ordinary characters
    void OrdinaryChar(int ch);    // specify that the character is a ordinary character
    void CommentChar(int ch);    // specify comment char

How to use the CStringTokenizer class:

you must include in your file

#include "StringTokenizer.h"

sample code for using the string tokenizer class:

    CString str;

    // sample string
    str = _T("cwsddde1231+-\"asdfgasd\"-{dfsdf}iwreu/*dsfghsdgf*/fgdfg//wejfshg"); 
    str += TT_EOF;    // add EOF to the string end

    CStringTokenizer strtok(str);    // String Tokenizer class

    int val;
    while((val = strtok.NextToken())!=TT_EOF)    // parse the string
        // display token code and str value
        CString msg;
        msg.Format ("%d %s",val,strtok.GetStrValue());    

This class is writen to be used at many types of lexical analyzers, you can inherit your own lexical analyzer class from this CStringTokenizer class.


Bug corections:

1. String memory alocation error corected

2. Pascal comments bug corected

Sample project:

The sample project shows how you can use the String Tokenizer Class, and how you can adjust it to your needs, the project also makes some pseudo Pascal, sintactical and some semantical analisis, the String Tokenizer should be now bugfree, but the PascalLexical, sintactical or semantical analizer should have bugs (I know 2 of them).

Download demo project - 46 KB


  • if the file size exceeds 6KB?

    Posted by Legacy on 05/24/2002 12:00am

    Originally posted by: novice programmer

    It takes a lot of time to load huge file and this fails
    when the file size exceeds 6KB.what is the solution for that?


  • how does the lexical's state table built???

    Posted by Legacy on 05/06/2001 12:00am

    Originally posted by: TechY

    I keep looking over and over again.
    I still don't understand how the state table built ;(
    and how does it use the state table???

    I am totally stuck on "state table" stuff.

    can someone help me out???



Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date