您的位置:首页 > 编程语言 > C语言/C++

编程实现计算FIRST集和FOLLOW集C++之(三)处理候选式:把空加入非终结符的First集

2016-05-25 22:31 701 查看
之前删去了右部为空的候选式。现在要实现把空加入到当前非终结符的First集中。



主要使用了boost的string的有关算法,trim处理字符串前后的空格。

string fzhongjiefu = line.substr(0,line.find("-"));
trim(fzhongjiefu);
preprocess.seekp(mark_now-line.length()-1);//把输出流定位到当前位置
string placeholder(line.length(),' ');
preprocess << placeholder  << endl;
addToFirstSet(fzhongjiefu,"ε");


void addToFirstSet(string fzhongjiefu,string element){
fstream firstSet("first.txt", fstream::ate | fstream::in | fstream::out );
if(!firstSet){
cerr << "Can't Open file " << endl;
}
firstSet << fzhongjiefu+ ":" + element << endl;
firstSet.close();
}


main.cpp

#include<fstream>
#include<iostream>
#include<string>
#include<vector>
#include<regex>
#include<cstdlib>
#include <boost/algorithm/string.hpp>
/*
*实现编译原理语法分析中计算非终结符的First集Follow集
*候选式存于文件中
*
*/
using namespace std;
using namespace boost;
//定义候选式类
class HXS{
public:
string left;
vector<string> right;
};
/*
*把第二个参数加入第一个参数的First集中
*/
void addToFirstSet(string fzhongjiefu,string element){ fstream firstSet("first.txt", fstream::ate | fstream::in | fstream::out ); if(!firstSet){ cerr << "Can't Open file " << endl; } firstSet << fzhongjiefu+ ":" + element << endl; firstSet.close(); }

/*
*把有多个右部的候选式切分开,便于下一步进行处理
*/
void split(){
string line;
string symor("|");
size_t found;
ifstream infile("houxuanshi.txt");
ofstream processed("houxuanshi_processed.txt");
if(!infile.is_open()){
cout << "Error Open File!" << endl;
}
cout << "------------------------" << endl;
cout << "候选式:" << endl;
line = "";
while(getline(infile,line)){
cout << line << endl;
found = line.find(symor);
if(found == string::npos){
processed << line << endl;
continue;
}else{
cout << "Has | " << endl;
processed << line.substr(0,found) << endl;
processed << line.substr(0,line.find(">")+1)+line.substr(found+1,line.length()-found) << endl;
}
line = "";
}
cout << "------------------------" << endl;
infile.close();
processed.close();
}
//预处理函数,先把候选式中由非终结符推导出空的候选式处理掉
//先把原来的文件复制一份,操作新文件,
//在用读写方式打开新文件,删除文件中候选式右部为空的候选式
void preprocess_right_is_null(){
string line;
ifstream processed("houxuanshi_processed.txt");
ofstream preprocess_temp("preprocess.txt");
if(!processed.is_open()){
cerr << "Can't Open File" << endl;
}
preprocess_temp << processed.rdbuf();
processed.close();
preprocess_temp.close();

fstream preprocess("preprocess.txt",fstream::in | fstream::out);
if(!preprocess){
cerr << "Open File Error!" << endl;
}
while(getline(preprocess,line)){
//读取并记录文件流的位置
auto mark_now = preprocess.tellg();
if(string::npos==line.find("ε")){
cout << " 进入下一行" << endl;
continue;
}else{
//要把候选式集合中的右部为空的候选式删除(覆盖成空格)
//并且把空加入当前非终结符的First集中
//此处涉及文件的随机读写
string fzhongjiefu = line.substr(0,line.find("-")); trim(fzhongjiefu); preprocess.seekp(mark_now-line.length()-1);//把输出流定位到当前位置 string placeholder(line.length(),' '); preprocess << placeholder << endl; addToFirstSet(fzhongjiefu,"ε");
cout << "把空加入"+ fzhongjiefu +"的First集" << endl;
}
}
preprocess.close();
/* *********************************以上是处理右部为空的候选式************************ */
}
//处理候选式左部为终结符的情况
void preprocess_left(){

}
void FIRST(){
regex feizhongjiefu("[A-Z]");
cout << regex_match("E",feizhongjiefu) << endl;
}
void FOLLOW(){
}
int main(){
// split();
// FIRST();
preprocess_right_is_null();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编译原理 C++