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

📄 parsetable.cpp

📁 C编译器
💻 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];
	//bool error = false;bool accept = false;
	//Action action;
//------------------------------------------------------------------------------------------------
	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( k = 0;k < (int)inputStr.size() &&inputStr[k] != next;k++);
		for(int k = i;k < (int)inputStr.size();k++)
			output += inputStr[k] + " ";
		cout<<right<<setw(30)<<output;
		//cout<<endl;
//-----------------------------------------------------------------------------------------------
	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();//cout<<"Pop the nonterminal "<<top<<" from the parsing stack"<<endl;
				//cout<<"push the right of the production to the parsing stack"<<endl;
				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 
			{
				//error = true;
				//action = ERROR;
				cout<<right<<setw(30)<<"error\n";
				break;
			}
		} //else if
		else 
		{
			//error = true;
			//action = ERROR;
			cout<<right<<setw(30)<<"error\n";
			break;
		}//cout<<parseStack.top();
//-----------------------------------------------------------------------------------------------------------------------
		output.clear();
		for(int k = 0;k < (int)parseVec.size();k++)		
			output += parseVec[k] + " ";
	cout<<left<<setw(30)<<output;output.clear();
		//int k;
		//for( k = 0;k < (int)inputStr.size() &&inputStr[k] != next;k++);
		for(int k = i;k < (int)inputStr.size();k++)
			output += inputStr[k] + " ";
		cout<<right<<setw(30)<<output;
		//cout<<endl;

//------------------------------------------------------------------------------------------------------------------------
	}//while

	if( parseStack.top() == "$" && next == "$" )//action = ACCEPT;//accept = true;
		cout<<right<<setw(30)<<"accept\n";
	//else// action = ERROR;//error = true;
	//	cout<<right<<setw(30)<<"error\n";
	/*
	ofstream tfile("table.txt");
	if(!tfile) cout << "Cannot open the table file\n";
	else
	{
		cout<<"\naccept = "<<accept<<endl;
	}*/
}

⌨️ 快捷键说明

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