Click here to Skip to main content
13,799,674 members
Click here to Skip to main content

Stats

161.2K views
2.6K downloads
54 bookmarked
Posted 13 Jun 2001
Licenced

Tokenizer and analyzer package supporting precedence prioritized rules

, 1 Jan 2002
A library allowing you to conveniently build a custom tokenizer and analyzer supporting precedence priorized rules
cxTokenizer
cxTokenizer.dsp
cxTokenizer.plg
cxtPackage
cxtPackage.plg
cxtPackage.dsp
mathTok
mathTok.plg
mathTok.dsp
simpleCalc
simpleCalc.bmp
simpleCalc.dsp
simpleCalc.plg
tkCommon
cxAnalyzer
cxAnalyzer.dsp
cxAnalyzer.plg
COPYING
cpAbsd.dsw
cxTokenizer.dsp
cxTokenizer.plg
Debug
cxAnalyzer.dsp
cxAnalyzer.plg
cxtPackage.plg
cxtPackage.dsp
COPYING
cpAbsd.dsw
stlport_vc645.dll
grammarIDE.exe
COMMON.CPP
cxTokenizer.dsp
PACKAGE.H
cxtPackage.dsp
emptyTestApp
res
emptyTestApp.ico
emptyTestApp.dsp
grammarIDE
res
grammarIDEDoc.ico
icon1.ico
zoomable.ico
Toolbar.bmp
grammarIDE.ico
vssver.scc
grammarIDE.dsp
simpleCalc.bmp
simpleCalc.dsp
vssver.scc
ANALYZER.H
cxAnalyzer.dsp
COMMON.H
COPYING
cpAbsd.dsw
DEBUG.CPP
DEBUG.H
COMMON.CPP
cxTokenizer.dsp
PACKAGE.H
cxtPackage.dsp
emptyTestApp.ico
emptyTestApp.dsp
vssver.scc
ANALYZER.H
cxAnalyzer.dsp
COMMON.H
COPYING
cpAbsd.dsw
DEBUG.CPP
DEBUG.H
/*********************************************************************
	Copyright (C) 2001/2 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.
    ---------------------------------------------------------------

Class:      cxaToken / cxaTokenStream / cxaParseElement /
			cxaParseNode / cxaParseBranch
Author:     Alexander Berthold
Copyright:  Alexander Berthold
Date:       2001/12/19
Version:	0.2.01
Purpose:    - cxaToken:
				Contains data for a token
			- cxaTokenStream:
				Implements cxTokenizerListener. Accepts the output token stream
				from the tokenizer and builds a list from the tokens.
			- cxaParseElement / cxaParseNode / cxaParseBranch
				Derived from the appropriate ctkHLinkedListXXX classes.
				Build the parse tree.
			

Version history:

	-	2001/12/15
		Created

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

// cxaToken.inl: interface for the cxaToken class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CXATOKEN_INL__0CD5BF48_9045_4916_B780_68258BC9D524__INCLUDED_)
#define AFX_CXATOKEN_INL__0CD5BF48_9045_4916_B780_68258BC9D524__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

inline void cxaParseBranch::enumerate(ctkSortedEnumerator<LPCTSTR>& enumerator, int nNestLevel, int *pnCounter) const
	{
	sit_data_t		sit;
	bool			fContinue = true;
	std::tstring	str0,str00;
	TCHAR			szTemp[257];
	szTemp[256]		='\0';
	for(int i=0;i<nNestLevel-1;i++) str0+='|',str0+=' ',str00+='|',str00+=' ';
	if(nNestLevel!=0) str00+='+', str00+='-',str0+='|', str0+=' ';
	if(pnCounter==NULL) pnCounter = (int*)_alloca(sizeof(*pnCounter));

	vEnumBegin(&sit);
	_sntprintf(szTemp,256,_T("%s[ Rule: AtmID#=%d, IDValue=%d, Prio=%d"), str00.c_str(), nAtmType, nIDValue, nPrecPrio );
	enumerator.enumElement( (*pnCounter)++,  szTemp );
	while(fContinue)
		{
		const cxaParseElement* papeCur = papeEnumGetAt(&sit);
		std::tstringstream strmLine;

		if(papeCur->fIsBranch())
			{
			if(nNestLevel==-1)
				{
				const cxaParseBranch *papbCur = papeCur->papbElem();
				_sntprintf(szTemp,256,_T("%s> Rule: AtmID#=%d, IDValue=%d, Prio=%d"),str0.c_str(),papbCur->nAtmType,papbCur->nIDValue,papbCur->nPrecPrio);
				enumerator.enumElement( (*pnCounter)++, szTemp );
				}
			else
				{
				const cxaParseBranch *papbCur = papeCur->papbElem();
				papbCur->enumerate(enumerator,nNestLevel+1,pnCounter);
				}
			}
		else
			{
			const cxaParseNode *papnCur = papeCur->papnElem();
			const cxaToken *patToken = papnCur->patGetToken();
			if(patToken!=NULL)
				{
				_sntprintf(szTemp,256,_T("%s| Token: AtmID#=%d, IDValue=%d, Text='%s'"),
					str0.c_str(), patToken->nAtmType, patToken->nIDValue, 
					patToken->lpszTokenText!=NULL?patToken->lpszTokenText:"<N/A>");
				}
			else
				_tcscpy(szTemp,"<<NULL TOKEN>>");
			enumerator.enumElement( (*pnCounter)++, szTemp );
			}
		fContinue		=fEnumNext(&sit);
		}
	vEnumEnd(&sit);
	_sntprintf(szTemp,256,_T("%s] ENDS."),str00.c_str());
	enumerator.enumElement( (*pnCounter)++, szTemp);
	};

inline void cxaParseBranch::enumerateRaw(ctkSortedEnumerator<LPCTSTR>& enumerator, int nNestLevel, int *pnCounter) const
	{
	const_iterator	it;
	std::tstring	str0,str00;
	TCHAR			szTemp[257];
	szTemp[256]		='\0';
	for(int i=0;i<nNestLevel-1;i++) str0+='|',str0+=' ',str00+='|',str00+=' ';
	if(nNestLevel!=0) str00+='+', str00+='-',str0+='|', str0+=' ';
	if(pnCounter==NULL) pnCounter = (int*)_alloca(sizeof(*pnCounter));

	_sntprintf(szTemp,256,_T("%s[ Rule: AtmID#=%d, IDValue=%d, Prio=%d"), str00.c_str(), nAtmType, nIDValue, nPrecPrio);
	enumerator.enumBegin();
	enumerator.enumElement( (*pnCounter)++,  szTemp );

	for(it=begin();it!=end();it++)
		{
		const cxaParseElement* papeCur = (*it);
		std::tstringstream strmLine;

		if(papeCur->fIsBranch())
			{
			if(nNestLevel==-1)
				{
				const cxaParseBranch *papbCur = papeCur->papbElem();
				_sntprintf(szTemp,256,_T("%s> Rule: AtmID#=%d, IDValue=%d"),str0.c_str(),papbCur->nAtmType,papbCur->nIDValue);
				enumerator.enumElement( (*pnCounter)++, szTemp );
				}
			else
				{
				const cxaParseBranch *papbCur = papeCur->papbElem();
				papbCur->enumerateRaw(enumerator,nNestLevel+1,pnCounter);
				}
			}
		else
			{
			const cxaParseNode *papnCur = papeCur->papnElem();
			const cxaToken *patToken = papnCur->patGetToken();

			if(patToken==NULL)
				_sntprintf(szTemp,256,_T("%s| NULL Token"), str0.c_str() );
			else
				_sntprintf(szTemp,256,_T("%s| Token: AtmID#=%d, IDValue=%d, Text='%s'"),
					str0.c_str(), patToken->nAtmType, patToken->nIDValue, 
					patToken->lpszTokenText!=NULL?patToken->lpszTokenText:"<N/A>");

			enumerator.enumElement( (*pnCounter)++, szTemp );
			}
		}
	_sntprintf(szTemp,256,_T("%s] ENDS."),str00.c_str());
	enumerator.enumElement( (*pnCounter)++, szTemp);
	enumerator.enumEnd();
	};

#endif // !defined(AFX_CXATOKEN_INL__0CD5BF48_9045_4916_B780_68258BC9D524__INCLUDED_)

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

Share

About the Author

Alexander Berthold
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.181207.3 | Last Updated 2 Jan 2002
Article Copyright 2001 by Alexander Berthold
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid