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

📄 parsetable.cpp

📁 这是编译原理编译器中的一部分
💻 CPP
字号:
#include <iomanip>

#include "parse.h"
#include "read.h"
#include "make.h"

extern DVec_str productions;
extern vecStr terminals;
extern vecNter nonterminals;
extern DVec_int table;
extern vector< string> vec ;

extern stack<string> parseStack;
extern vecStr inputStr;

void parseTable()
{
	ifstream sfile("source.txt");vector<string> parseVec;
	if( !sfile )  cout<<"Cannot open the source file\n";

	else 
	{
		string str;
		cout<<"input the test string with a \" # \" to end:";
		while( cin>>str && str != "#")//sfile >> str )//
				inputStr.push_back( str );
	}
	parseStack.push("$");parseVec.push_back("$");
	inputStr.push_back("$");
	parseStack.push(productions[0][0]);parseVec.push_back(productions[0][0]);

	int i = 0;string next = inputStr[i];
//------------------------------------------------------------------------------------------------
	string output;
	cout<<setw(30)<<"Parsing stack"<<setw(30)<<"Input"<<setw(30)<<"Action\n";
	for(int k = 0;k < (int)parseVec.size();k++)		
			output += parseVec[k] + " ";
	cout<<left<<setw(30)<<output;output.clear();

	
		for(int k = i;k < (int)inputStr.size();k++)
			output += inputStr[k] + " ";
		cout<<right<<setw(30)<<output;
//-----------------------------------------------------------------------------------------------
	while(parseStack.top() != "$"  || next !="$" )
	{
		if( !(hasexisted(terminals,next) || next == "$") )
		{
			//cout<<"Error input :"<<next;action = ERROR;
			cout<<right<<setw(30)<<"error\n";
			break;
		}
		string top = parseStack.top();
		if( hasexisted(terminals, top ) && top == next )
		{
			parseStack.pop();parseVec.pop_back();
			next = inputStr[++i];
			cout<<right<<setw(30)<<"matching\n";
			//action = MATCHING;
		}
		else  if( findIndex(top ) != -1 )
		{//cout<<endl<<findIndex( top )<<" "<<findTerIndex( vec,next );
		    int index1 ;
			      if(next == "$" )index1 = (int)table[0].size() - 1;
				  else index1 = findTerIndex( vec,next );
			int index = table[findIndex( top )][index1];//findTerIndex( vec,next )];
			if( index != -1 )
			{
				parseStack.pop(); parseVec.pop_back();
				
				for( int j = (int)productions[index].size() - 1;j > 0 && productions[index][j] != "ε";j-- )
				{parseStack.push( productions[index][j] );parseVec.push_back(productions[index][j]);}
				//action = POP;
				string pro = productions[index][0] + " → ";
				for(int j = 1;j < (int)productions[index].size();j++)
					pro += productions[index][j] + " ";
				cout<<right<<setw(30)<<pro<<endl;
				pro.clear();
			}
			else 
			{
				cout<<right<<setw(30)<<"error\n";
				break;
			}
		} //else if
		else 
		{
			cout<<right<<setw(30)<<"error\n";
			break;
		}
//-----------------------------------------------------------------------------------------------------------------------
		output.clear();
		for(int k = 0;k < (int)parseVec.size();k++)		
			output += parseVec[k] + " ";
	cout<<left<<setw(30)<<output;output.clear();
		for(int k = i;k < (int)inputStr.size();k++)
			output += inputStr[k] + " ";
		cout<<right<<setw(30)<<output;
//------------------------------------------------------------------------------------------------------------------------
	}//while

	if( parseStack.top() == "$" && next == "$" )//action = ACCEPT;//accept = true;
		cout<<right<<setw(30)<<"accept\n";
	system("pause");
}

⌨️ 快捷键说明

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