⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stdgram.cpp

📁 Standard Grammar Toolkit
💻 CPP
字号:
/*=========================================================
stdgram.cpp
Copyright (C) 2001, ClearJump, All Rights Reserved

This is the main file for the STGRAM sample.
=========================================================*/

#include <iostream>

#include "StdInterpreter.h"

//LOCAL DEFINITIONS
//=================
int Process( CClearParse& cp );
UINT ProcessErrors( CClearParse& cp );

//test interpreter for stdtest.gdm
class TestInterpreter : public StdInterpreter
{
public:
	int m_status;

	inline TestInterpreter()
	{
		m_status = 0;
	}

	void run();

	//virtual function called by the interpreter when an error occurs
	void error_notify( InterpreterError& err )
	{
		cout<<"*interpreter error! code: "<<err.m_code<<" cperr: "<<err.m_cperr<<"\n";
		m_status = -1;
	}
};


//IMPLEMENTATION
//==============
int main( int argc, char *argv[] )
{
	if( argc != 2 )
	{
		cout<<"*** Invalid command line\n";
		return -1;
	}

	CPCHAR *pfile = argv[1];

	//initialize the parser
	//---------------------

	CPERR           err;
	CClearParse     cp;

	//open the parser
	if( !cp.Open() )
	{
		cout<<"*** Unable to open the ClearParse engine\n";
		return -1;
	}

	//add the grammar files
	err = cp.GramAdd( "stdgram.gdm" );
	if( err != CPERR_OK )
	{
		cout<<"*** Unable to add the grammar: stdgram.gdm\n";
		return -1;
	}

	//add the test grammar that references the stdgram.gdm
	err = cp.GramAdd( "stdtest.gdm" );
	if( err != CPERR_OK )
	{
		cout<<"*** Unable to add the grammar: stdtest.gdm\n";
		return -1;
	}

	//build the grammar
	//stdtest is the grammar that defines the topmost symbol
	err = cp.GramBuild( "stdtest", TRUE );  
	if( err != CPERR_OK )
	{
		cout<<"*** Unable to build the grammar\n";
		return -1;
	}

	//parse the file
	//--------------
	cout<<"Processing... "<<pfile<<"\n";

	err = cp.Parse( pfile );
	if( err == CPERR_PARSEERR || err == CPERR_OK )
	{
		//process the parsing tree
		return Process( cp );
	}
	else
	{
		cout<<"*** Unable to parse the file: "<<pfile<<"\n";
		return -1;
	}

	return 0;
}

int Process( CClearParse& cp )
{
	if( ProcessErrors(cp) )
		return -1;

	//initialize the interpreter
	TestInterpreter  interpreter;
	interpreter.set_parser( &cp );

	//run it
	interpreter.run();

	//return the status
	return interpreter.m_status;
}

UINT ProcessErrors( CClearParse& cp )
{
	UINT          i, num;
	SParseError   ErrInfo;

	//get number of errors
	num = cp.GetErrNum( );

	//output the errors
	for( i = 0; i < num; i++ )
	{
		cp.GetError( i, ErrInfo );
		cout<<ErrInfo.pszMsg;
		cout<<"\n";
	}

	return num;
}

//this method prints out the parsing tree elements
void TestInterpreter::run()
{
	HPTNODE        child;
	SPtnInfo       info;
	CPERR          err;
	HPTNODE        node;

	//get the parsing tree.
	//The root node should be the StdDoc symbol in stdgram.gdm
	node = m_parser->GetParseTree();
	if( !node ) return;

	//enumerate the parsing tree
	//--------------------------
	string str;
	double num;
	CPCHAR sym;
	CPCSTR idn;

	child = m_parser->GetFirstChild( node );
	for( ; child; child = m_parser->GetNextSibling(child) )
	{
		//get the node info
		err = m_parser->GetNodeInfo( child, info );
		if( err != CPERR_OK ) error( child, err );

		if( info.nNodeType == CPT_NONTERMINAL )
		{
			switch( info.nSymbolID )
			{
			case nid_String: //String
				if( process_string(child, str) );
					cout<<"string: "<<str.data()<<"\n";
				break;

			case nid_Number: //Number
				if( process_number(child, num) );
					cout<<"number: "<<num<<"\n";
				break;

			case nid_Identifier: //Identifier
				if( process_identifier(child, &idn) );
					cout<<"identifier: "<<idn<<"\n";
				break;

			case nid_Symbol: //Symbol
				if( process_symbol(child, sym) );
					cout<<"symbol: "<<sym<<"\n";
				break;

			default:
				error( child, InterpreterError::E_BADTREE );
				break;
			}
		}
		else  //nonterminals only
		{
			error( child, InterpreterError::E_BADTREE );
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -