编程实现计算FIRST集和FOLLOW集C++之(一)处理候选式:多个右部
2016-05-24 17:42
441 查看
当前目录下有这三个文件
houxuanshi.txt的内容
开始houxuanshi_processed.txt 内容是空的或者直接没有这个文件。
main.cpp
执行
houxuanshi_processed.txt被创建,并写入了内容
这里主要是split函数,使用了c++的string的各种字符串操作。
这样处理了候选式之后,就便于程序进一步分析每一个候选式的特征,从而计算非终结符的FIRST集和FOLLOW集。
houxuanshi_processed.txt houxuanshi.txt main.cpp
houxuanshi.txt的内容
E->TE' E'->+TE'|ε T->FT' T'->*FT|ε F->(E)|id
开始houxuanshi_processed.txt 内容是空的或者直接没有这个文件。
main.cpp
#include<fstream> #include<iostream> #include<string> #include<vector> /* *实现编译原理语法分析中计算非终结符的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(); } int main(){ split(); }
执行
g++ main.cpp ./a.out
houxuanshi_processed.txt被创建,并写入了内容
E->TE' E'->+TE' E'->ε T->FT' T'->*FT T'->ε F->(E) F->id
这里主要是split函数,使用了c++的string的各种字符串操作。
这样处理了候选式之后,就便于程序进一步分析每一个候选式的特征,从而计算非终结符的FIRST集和FOLLOW集。
相关文章推荐
- 使用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++中派生的概念以及派生类成员的访问属性