Click here to Skip to main content
15,885,842 members
Articles / Programming Languages / C++

Tokenizer and analyzer package supporting precedence prioritized rules

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
1 Jan 20023 min read 181.5K   2.8K   54  
A library allowing you to conveniently build a custom tokenizer and analyzer supporting precedence priorized rules
// mathTok.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
 
class   cTestListener : 
    public cxTokenizerListener
                
{
// Operations
public:
    virtual bool            fCheckValid() const { return true; };
    virtual bool            fShouldDelete() const { return false; };
    virtual void            vRegisterToken(const std::tstring& strTokenText, 
                                           const cxTokenizerTokenRule* pttrRule,
										   const cxTokenizerInputStream* ptisStream)
        {
        if(pttrRule!=NULL)
            {
            std::cout << "token(" << pttrRule->nGetIDValue() << "): ";
            std::cout << strTokenText << std::endl;
            }
        else
            {
            std::cout << "NULL token: " << strTokenText << std::endl;
            }
        };
};

int main(int argc, char* argv[])
	{
    std::tstringstream  setup(
                            "[tokens]\n"
                            "100:sin\n"     "101:cos\n"
                            "102:tan\n"     "103:sqrt\n"
                            "[seperators]\n"
                            "200:+\n"       "201:-\n"
                            "202:*\n"       "203:/\n"
                            "204:(\n"       "205:)\n"
                            "[rules]\n"
                            "300:numbers\n"
                        );

    // Let the user enter the string to tokenize
    TCHAR               szUserData[256];
    std::cout << "Enter a string to tokenize: ";
    std::cin.getline(szUserData,256);

    // 'setup': see previous slide
    cxTokenizerMap      tmap(setup); 
    // Build a cxTokenizer - compatible input stream
    cxTokenizerTextInputStream<cxTokenizerInputStreamDefaultImpl<crlf_cronly> > tistream(szUserData);
    // Create an instance of our listener class
    cTestListener       tlis;

    cxTokenizer         tokenizer(&tistream,&tmap,&tlis);
    
    while(!tistream.fIsEofReached())
        tokenizer.vParseCharacter();

	return 0;
	}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions