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

📄 syntaxanalyzer.cpp

📁 一个用C++写的编译器
💻 CPP
字号:
#include "stdafx.h"

using namespace std;

SyntaxAnalyzer::SyntaxAnalyzer()
{;}

void SyntaxAnalyzer::initialize(string filename)
{
	myAnalyzer.setFilename(filename);
	pTable.initialize();
	pStack.initialize();
	append=new symbolElement;
}

string SyntaxAnalyzer::parse()
{
	symbolElement* curT=0;//current token entry
	stackEntry* topE=0;//top entry
	stackEntry* popE=0;//current popE
	findedE=0;//finded entry
	string ciT;//current input token
	string csT;//current stack token
	ciT="";
	csT="";
	//curT=myAnalyzer.yylex();//get a token from LexicalAnalyzer
	if(!pStack.isEmpty())//if hasn't reach the end stack
	{
		goOn:
			{
				if(!myAnalyzer.isEnd())
				{
					curT=myAnalyzer.yylex();//get a token from LexicalAnalyzer
					if(curT!=0)
					ciT=curT->token;
				}
				else
				{
					curT=append;
					ciT="dl";
				}
			}
		if(curT!=0&&curT!=myAnalyzer.noT)
		//if current token is not whitespace or comments and no error occurs.
		{
				topE=pStack.getTop();
				csT=topE->entry;
				if(isupper(csT[0]))//if the top of the parsing stack is nonterminal
				{
					findedE=pTable.search(csT,ciT);
					if(findedE==pTable.empty)
					{
						popE=pStack.pop();
						if(ciT!="dl")
						{
							myAnalyzer.ungetT();
						}
						return popE->entry+" ";
					}
					else if(findedE==pTable.unexpect)
					{
							return "Unexpected token "+transfer(ciT)+" was founded at line";
					}
					else if(findedE==pTable.missing)
					{
						pStack.pop();
						if(ciT!="dl")
						{
							myAnalyzer.ungetT();
						}
						return "Unknown missing token occured in line";
					}
					else
					{
						pStack.replace(findedE->entry);
						if(ciT!="dl")
						{
							myAnalyzer.ungetT();
						}
						return csT;
					}
				}
				else//if the current top entry of the stack is terminal.
				{
					if(csT==ciT)
					{
						popE=pStack.pop();
						return popE->entry;
					}
					else
					{
						popE=pStack.pop();
						if(ciT!="dl")
						{
							myAnalyzer.ungetT();
						}
						return "Missing token "+transfer(popE->entry)+" occured in line";
					}
				}
		}			
		else if(curT==0)//if error occurs
		{
			pStack.pop();
			return "Spelling error ocurrs in line";
		}
		else
		{
			goto goOn;

		}
	}
	else//if end of file reached
	{
			return "Parsing ended";
	}
}

int SyntaxAnalyzer::getLine()
{
	return myAnalyzer.line;
}

parsingTableEntry* SyntaxAnalyzer::getCurrentProduction()
{
	return findedE;
}
SyntaxAnalyzer::~SyntaxAnalyzer()
{
	delete append;
}

⌨️ 快捷键说明

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