编程实现计算FIRST集和FOLLOW集C++之(三)处理候选式:把空加入非终结符的First集
2016-05-25 22:31
701 查看
之前删去了右部为空的候选式。现在要实现把空加入到当前非终结符的First集中。
主要使用了boost的string的有关算法,trim处理字符串前后的空格。
main.cpp
主要使用了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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性