Click here to Skip to main content
15,885,365 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.4K   2.8K   54  
A library allowing you to conveniently build a custom tokenizer and analyzer supporting precedence priorized rules
/*********************************************************************
	Copyright (C) 2001 by

		Alexander Berthold, alexander-berthold@web.de.
		Hoegestr. 54
		79108 Freiburg i. Breisgau
		Germany

    -- This file is part of cxAnalyzer --

    "cxAnalyzer" is free software; you can redistribute it and/or 
	modify it under the terms of the GNU Lesser General Public 
	License as published by the Free Software Foundation; either 
	version 2 of the License, or any later version.

    "cxAnalyzer" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
	License along with "cxAnalyzer"; if not, write to the Free 
	Software  Foundation, Inc., 59 Temple Place, Suite 330, 
	Boston, MA  02111-1307  USA

    ---------------------------------------------------------------
      If you find any bugs or if you make other corrections/
	  enhancements, i'd appreciate if you'd let me know about 
	  that. My email is
  
       alexander-berthold@web.de
  
      If you share this code, do not remove this text.
    ---------------------------------------------------------------

*********************************************************************/

// cxAnalyzerTree.cpp: implementation of the cxAnalyzerTree class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "cxAnalyzerException.h"
#include "cxAnalyzerTypeMap.h"
#include "cxaToken.h"
#include "cxAnalyzerExpression.h"
#include "cxAnalyzerTree.h"
#include "cxaRuleCacheElement.h"
#include "cxaRuleCache.h"
#include "cxAnalyzerMain.h"
#include "cxaParseTree.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#ifndef _DEBUG
#undef TRACE
#define TRACE printf
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cxAnalyzerTree::cxAnalyzerTree()
	{
    m_nBeginTransCnt=0;
	}

cxAnalyzerTree::~cxAnalyzerTree()
	{
	}

//////////////////////////////////////////////////////////////////////
// Operations
//////////////////////////////////////////////////////////////////////

cxAnalyzerTree::chkpoint_type cxAnalyzerTree::cpGetCheckpoint() const
	{
	return m_vecLogElems.size();
	}

void	cxAnalyzerTree::vExtractLogData(chkpoint_type cpStartPos, log_vec_type& dest, chkpoint_type cpEndPos) const
	{
	if(cpEndPos==0) cpEndPos = m_vecLogElems.size();
	dest.clear();
	dest.insert(dest.begin(), m_vecLogElems.begin()+cpStartPos, m_vecLogElems.begin()+cpEndPos);
	}

void	cxAnalyzerTree::vInsertLogData(const log_vec_type& src)
	{
	m_vecLogElems.insert( m_vecLogElems.end(), src.begin(), src.end());
	}

void    cxAnalyzerTree::vBeginTrans()
    {
    m_nBeginTransCnt++;
    }

void    cxAnalyzerTree::vCommitTrans()
    {
    if(m_nBeginTransCnt>0)
        m_nBeginTransCnt--;
    else
        m_stkTrans.pop();
    }

void    cxAnalyzerTree::vRollbackTrans()
    {
    if(m_nBeginTransCnt>0)
        m_nBeginTransCnt--;
    else
        {
        int     nRBPos = m_stkTrans.top();
        log_vec_type::iterator  it=m_vecLogElems.begin();

        it      +=nRBPos;
        if(it!=m_vecLogElems.end())
            m_vecLogElems.erase(it,m_vecLogElems.end());

        m_stkTrans.pop();
        }
    }

void    cxAnalyzerTree::vCleanUp(const cxAnalyzerMain* pamParser)
    {
    // Replace le_end_rule,le_begin_rule(NULL),le_token,le_end_rule
    // with le_token,le_end_rule
    int     nSize = m_vecLogElems.size();
    int     i;

    for(i=0;i<(nSize-3);i++)
        {
        __log_elem  &le0 = m_vecLogElems[i];
        __log_elem  &le1 = m_vecLogElems[i+1];
        __log_elem  &le2 = m_vecLogElems[i+2];
        __log_elem  &le3 = m_vecLogElems[i+3];

        if(le0.nType==le_end_rule)
            {
            if(le1.nType==le_begin_rule)
                {
                if(le1.pLogData==NULL)
                    {
                    ASSERT(le2.nType==le_token);
                    ASSERT(le2.pLogData!=NULL);
                    ASSERT(le3.nType==le_end_rule);
                    le0.nType   =le_token;
                    le0.pLogData=NULL;
                    le1.nType   =le_token;
                    le1.pLogData=NULL;
                    i   +=4;
                    continue;
                    }
                }
            }

		if(	le0.nType==le_begin_rule &&
			le1.nType==le_token && le1.pLogData==NULL &&
			le2.nType==le_end_rule)
			{
			le0.nType=le_nop;
			le1.nType=le_nop;
			le2.nType=le_nop;
			i+=2;
			continue;
			}

		if( le0.nType==le_token && le0.pLogData==NULL &&
			le1.nType==le_token && le1.pLogData==NULL)
			{
			le0.nType=le_nop;
			le1.nType=le_nop;
			i+=2;
			continue;
			}

		if( le0.nType==le_token && le0.pLogData==NULL &&
			le1.nType==le_end_rule &&
			le2.nType==le_token)
			{
			le0.pLogData=le2.pLogData;
			le2.nType=le_nop;
			i+=3;
			continue;
			}

		if(	le1.nType==le_begin_rule &&
			le2.nType==le_token && le2.pLogData==NULL &&
			le3.nType==le_end_rule)
			{
			le1.nType=le_nop;
			le2.nType=le_nop;
			le3.nType=le_nop;
			i+=3;
			continue;
			}
        }
    }

void    cxAnalyzerTree::vLog(int nType, int nAtmType, int nIDValue, const void *pLogData, const void* pExtraData)
    {
    __log_elem  le;
    le.nType    =nType;
	le.nAtmType	=nAtmType;
	le.nIDValue	=nIDValue;
    le.pLogData =pLogData;
	le.pExtraData=pExtraData;

    while(m_nBeginTransCnt>0)
        {
        m_stkTrans.push(m_vecLogElems.size());
        m_nBeginTransCnt--;
        }

    m_vecLogElems.push_back(le);
    }

bool cxAnalyzerTree::fFindPrevTokens(long lCount, token_item* pPrevItems) const
	{
	// traverse the stack backwards to find 'lCount' previous tokens
	log_vec_type::const_reverse_iterator rit;
	int		nCurLevel = 0;
	
	for(rit=m_vecLogElems.rbegin(); rit!=m_vecLogElems.rend(); rit++)
		{
		const __log_elem& logElem = (*rit);

		if(logElem.nType == le_end_rule)
			{	nCurLevel++; continue; };
		if(logElem.nType == le_begin_rule)
			{	nCurLevel--; continue; };

		if(nCurLevel==0 && logElem.nType == le_token)
			{
			lCount--;
			pPrevItems[lCount].nAtmType = logElem.nAtmType;
			pPrevItems[lCount].nIDValue = logElem.nIDValue;
			pPrevItems[lCount].patToken = (const cxaToken*)logElem.pLogData;
			pPrevItems[lCount].pvData   = logElem.pExtraData;
			if(lCount==0)
				return true;
			}
		}

	return false;
	}

bool cxAnalyzerTree::fFindPrevTokensByAtm(long lCount, int nAtmType, token_item* pPrevItems) const
	{
	// traverse the stack backwards to find 'lCount' previous tokens
	log_vec_type::const_reverse_iterator rit;
	int		nCurLevel = 0;
	
	for(rit=m_vecLogElems.rbegin(); rit!=m_vecLogElems.rend(); rit++)
		{
		const __log_elem& logElem = (*rit);

		if(logElem.nType == le_end_rule)
			{	nCurLevel++; continue; };
		if(logElem.nType == le_begin_rule)
			{	nCurLevel--; continue; };

		if(nCurLevel==0 && logElem.nType==le_token && logElem.nAtmType==nAtmType)
			{
			lCount--;
			pPrevItems[lCount].nAtmType = logElem.nAtmType;
			pPrevItems[lCount].nIDValue = logElem.nIDValue;
			pPrevItems[lCount].patToken = (const cxaToken*)logElem.pLogData;
			pPrevItems[lCount].pvData   = logElem.pExtraData;
			if(lCount==0)
				return true;
			}
		}

	return false;
	}

bool cxAnalyzerTree::fFindPrevTokensByID(long lCount, int nIDValue, token_item* pPrevItems) const
	{
	// traverse the stack backwards to find 'lCount' previous tokens
	log_vec_type::const_reverse_iterator rit;
	int		nCurLevel = 0;
	
	for(rit=m_vecLogElems.rbegin(); rit!=m_vecLogElems.rend(); rit++)
		{
		const __log_elem& logElem = (*rit);

		if(logElem.nType == le_end_rule)
			{	nCurLevel++; continue; };
		if(logElem.nType == le_begin_rule)
			{	nCurLevel--; continue; };

		if(nCurLevel==0 && logElem.nType==le_token && logElem.nIDValue==nIDValue)
			{
			lCount--;
			pPrevItems[lCount].nAtmType = logElem.nAtmType;
			pPrevItems[lCount].nIDValue = logElem.nIDValue;
			pPrevItems[lCount].patToken = (const cxaToken*)logElem.pLogData;
			pPrevItems[lCount].pvData   = logElem.pExtraData;
			if(lCount==0)
				return true;
			}
		}

	return false;
	}

void	cxAnalyzerTree::vBeginRule(int nAtmType, int nIDValue, const cxAnalyzerExpression* pExpr)
    {
    vLog(le_begin_rule,nAtmType,nIDValue,pExpr,NULL);
    }

void    cxAnalyzerTree::vEndRule()
    {
    vLog(le_end_rule,ATM_ID_INVALID,TOKEN_ID_INVALID,NULL,NULL);
    }

void    cxAnalyzerTree::vLogToken(int nAtmType, int nIDValue, void *pvData, const cxaToken* patToken)
    {
/*    if(patToken==NULL)
        return;*/
    vLog(le_token,nAtmType,nIDValue,patToken,pvData);
    }

void    cxAnalyzerTree::vDumpLog()
    {
//    int nTemp = m_stkTrans.size();
//    ASSERT(m_stkTrans.empty());

	TRACE("******************************\n");
    log_vec_type::iterator  it = m_vecLogElems.begin();
	int posi = 0;
    for(;it!=m_vecLogElems.end();it++,posi++)
        {
		TRACE("%d -- ",posi);
        __log_elem  &el = (*it);

        if(el.nType==le_begin_rule)
            {
            if(el.pLogData==NULL)
                TRACE("<< RULE: NULL\n");
            else
                TRACE("<< RULE: %s\n",(*(*((const cxAnalyzerExpression*)el.pLogData)).pstrGetInitString()).data());
            }
        if(el.nType==le_end_rule)
            TRACE("RULE ENDS >>\n");
        if(el.nType==le_token)
            {
            if(el.pLogData!=NULL)
                TRACE("TOKEN: %s\n",((const cxaToken*)el.pLogData)->lpszTokenText);
			else
				TRACE("TOKEN: NULL\n");
            }
        }
	TRACE("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
    }

cxaParseTree	*cxAnalyzerTree::paptBuildTreeInternal(const cxAnalyzerMain* pamParser)
	{
    ASSERT(m_stkTrans.empty());
	cxaParseTree			*papt = new cxaParseTree();
    log_vec_type::iterator  it = m_vecLogElems.begin();

	// Clean up the structures
	vCleanUp(pamParser);
	vDumpLog();

    for(;it!=m_vecLogElems.end();it++)
        {
        __log_elem  &el = (*it);

        if(el.nType==le_begin_rule)
            {
			const	cxAnalyzerExpression* paeTemp = ((const cxAnalyzerExpression*)el.pLogData);
			int		nPrecPrio = 32767, nIDValue = el.nIDValue;
			bool	fLB = false, fRB = false;

			if(paeTemp!=NULL)
				{
				ASSERT(nIDValue==paeTemp->nGetIDValue());
				nPrecPrio	= paeTemp->nGetPrecPrio();
				fLB			= paeTemp->fIsLeftBound(paeTemp->nGetAtmType());
				fRB			= paeTemp->fIsRightBound(paeTemp->nGetAtmType());
				}

			papt->vBeginRule(el.nAtmType,nIDValue,nPrecPrio,fLB,fRB);
            }

        if(el.nType==le_end_rule)
			papt->vEndRule();

        if(el.nType==le_token)
            {
			// @TODO: this is really, really, really ugly. i am ashamed,
			// and i will change it ASAIHSST. (as soon as i have some spare time)
			cxaToken	*pToken = (cxaToken*)el.pLogData;

			if(pToken!=NULL && el.pExtraData!=NULL)
				{
				pToken->nAtmType	=el.nAtmType;
				pToken->nIDValue	=el.nIDValue;
				pToken->pvData		=(void*)el.pExtraData;
				}

			papt->vToken((const cxaToken*)el.pLogData);
            }
        }

	return papt;
	}

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