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

📄 parsingstack.cpp

📁 一个用C++写的编译器
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -