Click here to Skip to main content
15,882,152 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 180.8K   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.
    ---------------------------------------------------------------

Class:      cxAnalyzerException
Author:     Alexander Berthold
Copyright:  Alexander Berthold
Date:       2001/06/12
Version:	0.1.07
Purpose:    This class stores the pattern for a specific expression.
			It can perform a check on a given token pattern if it
			matches the expression (fCheck).

			'fInitFromRule' can be used to initialize the expression.
			The syntax is like this:
			{.RULE}=PRIO:PATTERN

			where 'RULE' stands for the name of the rule. 'PRIO'
			tells the precedence priority, and 'PATTERN' describes
			the pattern, consisting of a sequence of segments embraced
			by '{}' braces.

			{.RULE} -> refers to a rule 'RULE'
			{$TOKEN} -> refers to a token 'TOKEN'
			{!number} -> refers to a computed token (a number in this case)
			{!string} -> refers to a computed token (a string in this case)


Version history:

	-	2001/05/19
		Released the version 0.1.05

	-	2001/06/02
		Added support for cxaStatusCookie status report (fCheck).
		See cxAnalyzerMain.

	-	2001/06/12
		Fixed some problems with cxaStatusCookie.
		New version number is 0.1.07.

ToDo:
	-	Optimize the code someday. Is pretty fast already, but	
		'fCheckComp' can be optimized a lot.

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

// cxAnalyzerExpression.h: interface for the cxAnalyzerExpression class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CXANALYZEREXPRESSION_H__086331A7_62F3_47CC_B6F3_B7CFEBF4E061__INCLUDED_)
#define AFX_CXANALYZEREXPRESSION_H__086331A7_62F3_47CC_B6F3_B7CFEBF4E061__INCLUDED_

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

// Forward declarations
class	cxAnalyzerMain;
class	cxAnalyzerTree;
class	cxAnalyzerTypeMap;
class	cxAnalyzerTypeInfo;

// Define the following rule to maximize the accuracy.
// More detailed explanation on the effect follows.
// TODO: see line above!
//#define RECURSIVE_COMPLETE

class cxaStatusCookie;

class cxAnalyzerExpression  
{
// Construction/Destruction
public:
	cxAnalyzerExpression(cxAnalyzerTypeMap* patmTypeMap);
	virtual ~cxAnalyzerExpression();

// Attributes
protected:
	// Fully initialized?
	bool				m_fInitializeComplete;
	// Temporary pointer to the string passed to fInitFromRule()
public:
	std::tstring*		m_pstrInitTemp;
	// The TypeMap this expression belongs to
	cxAnalyzerTypeMap	*m_patmTypeMap;
	// The precedence priority of this expression
	int					m_nPrecPrio;
	// The ID in the TypeMap of this expression
	int					m_nAtmType;
	// User-defined ID of this expression
    int                 m_nIDValue;

	// The number of 'pttm'-Patterns (see below)
	int					m_nSizePattern;
	// The pattern of this expression
	cxAnalyzerTypeInfo 
						**m_apatiPattern;

// Protected operations
protected:
	/*** Helpers for 'fCheck()': ***/

	// Check if the expression matches the criteria
	bool				fCheckComp( const cxAnalyzerMain* paHost,
									const cxaTokenStream* patsContext,
									int nAtmTypeFirstIs, /* ATM_ID_INVALID? */
                                    const cxaToken* patFirstToken,
									cxaStatusCookie* pascCondition,
									cxaTokenStream::const_iterator start,
									cxaTokenStream::const_iterator *pend,
									cxAnalyzerTree* patTree = NULL) const;


// Operations
public:
	/*** Status check operations ***/
	bool				fCheckValid() const;
	bool				fUsesAtm(const cxAnalyzerTypeMap* patm) const
						{ return (m_patmTypeMap==patm); };
	int					nGetAtmType() const { return m_nAtmType; };
	int					nGetIDValue() const { return m_nIDValue; };
	int					nGetPrecPrio() const { return m_nPrecPrio; };

	bool				fIsLeftBound(int nAtmType) const;
	bool				fIsRightBound(int nAtmType) const;


	/*** Status initialization and manipulation ***/
	bool				fInitFromRule(std::tstring strRule);
	bool				fFinishInit();


	/*** Analyzer operations ***/
	// Tests if this 'cxAnalyzerExpression' is just a simple
	// mapping rule like expr:=string or something.
	bool				fIsImplicitExpressionToAtm(int* pnAtmType) const;

	bool				fCheck( const cxAnalyzerMain* paHost,
								const cxaTokenStream* patsContext,
								int nAtmTypeFirstIs, /* ATM_ID_INVALID? */
                                const cxaToken* patFirstToken,
								cxaStatusCookie* pascCondition,
								cxaTokenStream::const_iterator start,
								cxaTokenStream::const_iterator *pend,
								cxAnalyzerTree* patTree = NULL) const;
};

#endif // !defined(AFX_CXANALYZEREXPRESSION_H__086331A7_62F3_47CC_B6F3_B7CFEBF4E061__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


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