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

之前删去了右部为空的候选式。现在要实现把空加入到当前非终结符的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();
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值