-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab4.cpp
More file actions
84 lines (68 loc) · 1.45 KB
/
Copy pathLab4.cpp
File metadata and controls
84 lines (68 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Problem: Build a CFG that accepts or rejects a
given input based on the following grammar-
E -> E + E
E -> E * E
E -> (E)
E -> 0|1
*/
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
string input;
int n;
string production[3]={"E+E","E*E","(E)"};
string temp;
bool check(int start)
{
for(int i=start; i<start+3; i++)
temp += input[i];
int flag=0;
for(int i=0;i<3;i++)
{
if(production[i]==temp){
flag = 1;
break;
}
}
temp.clear();
if(flag==1)
return true;
else
return false;
}
void subStringRemover(int start){
string temp1, temp2;
for(int i=0; i<start; i++)
temp1 += input[i];
for(int i=start+3; i<input.length(); i++)
temp2 += input[i];
input = temp1 + "E" + temp2;
}
int main()
{
cout<<"Enter input as string: ";
cin>>input;
n = input.length();
for(int i=0;i<n;i++)
{
if((input[i]=='0')||(input[i]=='1'))
input[i]='E';
}
for(int i=0;i<input.length()-1; )
{
if(check(i))
{
subStringRemover(i);
i=0; // traverse the string again!
}
else
i++;
if(input.length()<=1)
break;
}
if((input.length()==1)||(input.length()==0))
cout<<"Accepted!"<<endl;
else
cout<<"Not accepted!"<<endl;
return 0;
}