编程实现计算FIRST集和FOLLOW集C++之(二)处理候选式:右部为空

之前处理了把右部有多个候选式的分割成了单独一行。
这里写图片描述
这其中第3、6行,候选式的右部为空,由求解First集的算法可知,此时可以直接把“ε”加入当前候选式左部非终结符的First集中。并且可以在操作的文件中删去这一行。算是预处理把。
实现思路就是,利用C++的fstream对象,设置打开文件的方式为读写(一般的操作是只能读或者只能写),每次处理一行候选式之前,记录当前的输入流的位置,如果当前的候选式中包含“ε”,把输出流的位置定位到当前,并使用当前候选式长度个“ ”(空格)填充。
处理完之后,
这里写图片描述
这里编译的时候,需要使用C++11标准。

g++ main.cpp -std=c++11
//预处理函数,先把候选式中由非终结符推导出空的候选式处理掉
//先把原来的文件复制一份,操作新文件,
//在用读写方式打开新文件,删除文件中候选式右部为空的候选式
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集中
        //此处涉及文件的随机读写
            preprocess.seekp(mark_now-line.length()-1);//把输出流定位到当前位置
            string placeholder(line.length(),' ');
            preprocess << placeholder  << endl; 
            cout << "把空加入"+line.substr(0,1)+"的First集" << endl;
        }   
    }
    preprocess.close(); 
/* *********************************以上是处理右部为空的候选式************************  */
}

main.cpp

#include<fstream>
#include<iostream>
#include<string>
#include<vector>
#include<regex>
/*
*实现编译原理语法分析中计算非终结符的First集Follow集
*候选式存于文件中
*
*/
using namespace std;
//定义候选式类
class HXS{
    public:
        string left;
        vector<string> right;
};
/*
*把有多个右部的候选式切分开,便于下一步进行处理
*/
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集中
        //此处涉及文件的随机读写
            preprocess.seekp(mark_now-line.length()-1);//把输出流定位到当前位置
            string placeholder(line.length(),' ');
            preprocess << placeholder  << endl; 
            cout << "把空加入"+line.substr(0,1)+"的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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值