编程实现计算FIRST集和FOLLOW集C++之(三)处理候选式:把空加入非终结符的First集
来源:互联网 发布:怎么上传源码到空间 编辑:程序博客网 时间:2024/06/06 23:50
之前删去了右部为空的候选式。现在要实现把空加入到当前非终结符的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();}
0 0
- 编程实现计算FIRST集和FOLLOW集C++之(三)处理候选式:把空加入非终结符的First集
- 编程实现计算FIRST集和FOLLOW集C++之(二)处理候选式:右部为空
- 编程实现计算FIRST集和FOLLOW集C++之(一)处理候选式:多个右部
- 如何求非终结符的FIRST集合FOLLOW集
- first集和follow集的计算
- FIRST集和FOLLOW集
- FIRST集和FOLLOW集
- FIRST集和FOLLOW集
- FIRST集和FOLLOW集
- First集和Follow集
- First集和Follow集的求法
- First集和Follow集的求法
- 求解FIRST & FOLLOW集
- FIRST集合FOLLOW集
- First, Follow 集
- First & Follow 集
- First集和Follow集,FirstVt集和LastVt集的计算
- 编译原理之--FIRST集、FOLLOW集 和 SELECT集
- APP开发实战40-MD5介绍
- Uva 103 Stacking Boxes
- 单片机学习笔记(四)——中断
- 自定义dialog
- echarts柱状图显示数值
- 编程实现计算FIRST集和FOLLOW集C++之(三)处理候选式:把空加入非终结符的First集
- python web框架cherrypy小demo
- Visual Studio发布应用未能创建默认证书的问题解决方法
- java String
- Sicily1566——4SUM
- python爬虫:案例四:新浪微指数
- 如何检查和解决Java虚拟机内存溢出的问题?
- C++作业6
- 1. python入门杂记