c++实现无用产生式的消除
来源:互联网 发布:rds数据库 编辑:程序博客网 时间:2024/06/07 10:19
过程有点麻烦,在判断string是否含有某个字符的时候使用迭代器重复了太多
其实可以用一个函数来包装的
/*
the program is for algorithm of “编译原理 无关文法的消除 ” by-Li
*/
#include<iostream>
#include<string>
using namespace std;
class P{
public:
char left;
string right;
};
class G{
private:
P p[20];//最多有20条产生式
P p1[20];
P p2[20];
string Vn;//非终结符号集
string Vt;//终结符号集
string Vt2;
string Vn1;
string Vn2;
int len;
int len1;
int len2;
public:
void create(string s);
void algorithm2_1();
void algorithm2_2();
void displaybegin();
void displaymid();
void displayend();
void display();
G(string a,string b);
};
G::G(string a,string b){
len=0;
len1=0;
len2=0;
Vn=a;
Vt=b;
Vt2="";
Vn1="";
Vn2="S";
}
void G::create(string s){
int sl=s.length();
p[len].left=s[0];
p[len].right=s.substr(3,sl-3);
len++;
}
void G::algorithm2_1(){
string com;
do{
com=Vn1;
for(int i=0;i<len;i++){
int j=0;
int jend=p[i].right.length();
while(j<jend){
string::size_type idx=Vt.find(p[i].right[j]);
string::size_type idx2=Vn1.find(p[i].right[j]);
if(idx!=string::npos||idx2!=string::npos){
j++;
}
else{
break;
}
}
if(j==jend){
string::size_type idx11=Vn1.find(p[i].left);
if(idx11==string::npos){
Vn1.append(1,p[i].left);
}
}
}
}while(Vn1.compare(com)!=0);
for(int i=0;i<len;i++){//--------------------实现p1
string::size_type idx14=Vn1.find(p[i].left);
if(idx14!=string::npos) {
int j=0;
int jend=p[i].right.length();
while(j<jend){
string::size_type idx4=Vt.find(p[i].right[j]);
string::size_type idx5=Vn1.find(p[i].right[j]);
if(idx4!=string::npos||idx5!=string::npos){
j++;
}
else{
break;
}
}
if(j==jend){
p1[len1].left=p[i].left;
p1[len1].right=p[i].right;
len1++;
}
}
}
cout<<"end of algorithm2_1"<<endl;
}
void G::algorithm2_2(){
string Vn2temp;
string Vt2temp;
do{
Vn2temp=Vn2;
Vt2temp=Vt2;
for(int i=0;i<len1;i++){
string::size_type idx3= Vn2.find(p1[i].left);
if(idx3!=string::npos){
int j=0;
int jend=p1[i].right.length();
while(j<jend){
string::size_type idx6=Vt.find(p1[i].right[j]);
if(idx6!=string::npos){
string::size_type idx12=Vt2.find(p1[i].right[j]);
if(idx12==string::npos){
Vt2.append(1,p1[i].right[j]);
}
j++;
}
else{
string::size_type idx13=Vn2.find(p1[i].right[j]);
if(idx13==string::npos){
Vn2.append(1,p1[i].right[j]);
}
j++;
}
}
}
}
}while(Vn2.compare(Vn2temp)!=0||Vt2.compare(Vt2temp)!=0);
for(int i=0;i<len1;i++){
string::size_type idx7=Vn2.find(p1[i].left);
if(idx7!=string::npos){
int j=0;
int jend=p1[i].right.length();
while(j<jend){
string::size_type idx8=Vn2.find(p1[i].right[j]);
string::size_type idx9=Vt2.find(p1[i].right[j]);
if(idx8!=string::npos||idx9!=string::npos){
j++;
}
else{
break;
}
}
if(j==jend){
p2[len2]=p1[i];
len2++;
}
}
}
cout<<"end of algorithm2_2"<<endl;
}
void G::displayend(){
for(int i=0;i<len2;i++){
cout<<p2[i].left<<"->";
cout<<p2[i].right<<endl;
}
}
int main(){
string a;
string b;
cout<<"please input Vn and Vt:";
cin>>a>>b;
G one(a,b);
cout<<"please input the number of createlist:";
string list;
int num;
cin>>num;
cout<<"please input list:"<<endl;
for(int n=0;n<num;n++){
cin>>list;
one.create(list);
}
one.algorithm2_1();
one.algorithm2_2();
cout<<"afterprocess:"<<endl;
one.displayend();
return 0;
}
- c++实现无用产生式的消除
- shell编程,消除c/c++无用变量告警的小脚本
- 消除C++工程中生成的无用文件
- 消除Connect Devices中无用的设备信息
- c实现字符串消除
- 消除位图缩放产生的锯齿
- 自激的产生与消除
- 自激的产生与消除
- 20170925_死锁的产生和消除
- 补充:消除epsilon产生式等
- 【C/EasyX】十字消除游戏的实现方法
- 【Effective Java】Ch2_创建销毁对象:Item6_消除无用的对象引用
- 无用的
- WPS中三个等号产生的横线的消除
- 消除VO.xml.rdf文件产生的致命错误
- 消除div中ul上部产生的空白
- 一个消除引用类名产生警告的方法
- 消除a标签点击后产生的虚线框
- 安卓报java.lang.NoClassDefFoundError
- 通过线程写文件的例子
- 笔记:编译和运行Bundler (在cygwin下)
- C#中char[]与string之间的转换
- windows 系统 ---窗口篇
- c++实现无用产生式的消除
- #define用法总结
- listView嵌套button或者其它clickable控件时会出现listView的onitemClick事件不响应的问题
- 测试Fragment(跳转回传onActivityResult问题)
- EF6增改删等常用基类
- ubuntu 网络仿真 段错误的一种可能的处理方法
- FindBugs 恶意代码(may expose internal representation),序列化(defines non-transient non-serializable )错误解决方法
- struts 搭建成功
- 关于 Linux C 中的头文件引用可能出现的问题解答