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

📄 extend.cpp

📁 C编译器
💻 CPP
字号:
#include "globals.h"
#include "util.h"

//extern ifstream grammarfile;
extern DVec_str productions;
extern vecStr terminals;
extern vecNter nonterminals;

void makePro( ifstream &inf )
{
		string str,strline;
		
		while( getline(inf,strline) )
		{
			vecStr production; 
			istringstream sin( strline );
			while( sin >> str )
			{
				if( str !="→" && str != "|"  )
					production.push_back( str );
				else if( str == "→" ) ;
				else 
				{
					productions.push_back( production );
					str = production[0];
					production.clear();
					production.push_back(str );
				}	
			}
			productions.push_back( production );
		}
}

void makeNter(void)                                   // make the nonterminals
{
	Nonterminal nonterminal;

	for(size_t i = 0;i < productions.size();i++)                                              
	{
		nonterminal.token = productions[i][0];
		if( !hasexisted( nonterminals,nonterminal ) ) nonterminals.push_back( nonterminal );
	}
}

void mekeTer(void)
{
	    Nonterminal nonterminal;

		for(size_t i = 0;i < productions.size();i++)                                               //make the terminals
		for(size_t j = 1;j < productions[i].size() ;j++)
		{
			nonterminal.token = productions[i][j];
		    if( !hasexisted(terminals,productions[i][j]) &&  !hasexisted(nonterminals,nonterminal)  )
				terminals.push_back(productions[i][j] );
		}
}


//------------------------------------------------------------------------------------------------------------
vector<string> makeFirst(vector<string> vecstr )
{
	vector<string> first;int i;
	for( i = 0;i < (int)vecstr.size();i++)
	{
		if( findIndex(vecstr[i] ) == -1 )
		{
			if ( !hasexisted( first,vecstr[i] ) ) 
				first.push_back(vecstr[i]);
			break;
		}//if
		else
		{
			int index = findIndex( vecstr[i] );                
			add( nonterminals[index].first,first );
			if( !hasexisted( nonterminals[ index ].first,  "ε" ) )break;
		}//else
	}//for

	if( i == vecstr.size() )first.push_back("ε");
	return first;
}

void add(vector<string>  &v1,vector<string> &v2)
{
	for(int i = 0;i <(int) v1.size();i++)
		if( v1[i] != "ε" && !hasexisted(v2,v1[i]) )v2.push_back(v1[i]);
}

⌨️ 快捷键说明

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