ll1parse.cpp

来自「C编译器」· C++ 代码 · 共 63 行

CPP
63
字号
#include <stack>

#include "globals.h"
#include "util.h"
#include "parser.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 LL1parse(ifstream & sfile)
{
		string str;
		while( sfile >> str )
		{
			inputStr.push_back( str );
		}

	parseStack.push("$");
	inputStr.push_back("$");
	parseStack.push(productions[0][0]);

	int i = 0;string next = inputStr[i];
	bool error = false;bool accept = false;

	while(parseStack.top() != "$"  || next !="$" )
	{
		string top = parseStack.top();
		if( hasexisted(terminals, top ) && top == next )
		{
			parseStack.pop();
			next = inputStr[++i];
		}
		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();
				for( int j = (int)productions[index].size() - 1;j > 0 && productions[index][j] != "ε";j-- )
					parseStack.push( productions[index][j] );
			}
			else 
			{
				error = true;
				break;
			}
		} //else if
		else 
		{
			error = true;
			break;
		}
	}//while
}

⌨️ 快捷键说明

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