parsingstack.cpp

来自「这是我们老师给我们的例子」· C++ 代码 · 共 118 行

CPP
118
字号
#include "stdafx.h"

using namespace std;


ParsingStack::ParsingStack()
{}

void ParsingStack::initialize()
{
	currentPosition=-1;
	head=0;
	count=0;
	current=0;
	popHead=0;
	popCurrent=0;
	head=push("dl");
	push("P");
}

stackEntry* ParsingStack::push(string entry)
{
	if(current==0)
	{
		current=new stackEntry;
		current->entry=entry;
		count++;
		return current;
	}
	else
	{
		stackEntry* temp;
		temp=new stackEntry;
		temp->entry=entry;
		temp->lower=current;
		current=temp;
		count++;
		return temp;
	}
}

stackEntry* ParsingStack::pop()
{
	stackEntry* temp;
	temp=current;
	current=current->lower;
	if(popHead==0)
	{
		popHead=popCurrent=temp;
	}
	else
	{
		popCurrent->lower=temp;
		popCurrent=popCurrent->lower;
	}
	count--;
	return temp;
}


string ParsingStack::getEntry(string tableEntry)
//divide the string of the production into several entries.
{
	if(currentPosition<0)
	{
		currentPosition=tableEntry.length()-1;
	}
	string temp="";
	while(currentPosition>=0&&tableEntry[currentPosition]!=' ')
	{
		temp=tableEntry.substr(currentPosition,1)+temp;
		currentPosition--;
	}
	if(currentPosition>=0)
	{
		currentPosition--;
	}
	return temp;
}

stackEntry* ParsingStack::replace(string tableEntry)
//replace the top entry of parsing stack with the entries returned by getEntry(string);
{
	pop();
	push(getEntry(tableEntry));
	while(currentPosition>=0)
	{
		push(getEntry(tableEntry));
	}
	return current;
}

stackEntry* ParsingStack::getTop()
{
	return current;
}

bool ParsingStack::isEmpty()
{
	if(count==0)
	{
		return true;
	}
	else return false;
}

ParsingStack::~ParsingStack()
{
	stackEntry* temp;
	while(popHead!=popCurrent&&popHead!=0)
	{
		temp=popHead;
		popHead=popHead->lower;
		delete temp;
	}
	delete popHead;
}

⌨️ 快捷键说明

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