-
Notifications
You must be signed in to change notification settings - Fork 0
/
q4.cpp
77 lines (74 loc) · 2.03 KB
/
q4.cpp
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
//Write a program to convert left recursive grammar to right recursive gramma
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int NT, i, j, k, n;
string str;
char non_terminal[100];
vector<string> Production[100];
vector<string> alpha, beta;
cout<<"=== Converting given left recursive grammar to right recursive grammar ==="<<endl;
cout<<"Enter the total number of non terminals:";
cin>>NT;
cout<<"Enter the Non Terminals :"<<endl;
for(i=0;i<NT;i++)
{
cout<<i+1<<" : ";
cin>>non_terminal[i];
}
for(i=0;i<NT;i++)
{
cout<<"Enter the total number of production for "<<non_terminal[i]<<": ";
cin>>n;
cout<<"Enter Productions :"<<endl;
for(j=0; j<n; j++)
{
cout<<"Production "<<i+1<<" : ";
cin>>str;
Production[i].push_back(str);
}
}
for(i=0; i<NT; i++)
{
for(j=0; j<Production[i].size(); j++)
{
if(Production[i][j][0] == non_terminal[i])
{
alpha.push_back(Production[i][j].substr(1, Production[i][j].length()-1 ));
}
else
{
beta.push_back(Production[i][j]);
}
}
if(alpha.size() > 0)
{
cout<<non_terminal[i]<<" -> ";
for(j=0; j<beta.size(); j++)
{
cout<<beta[j]<<non_terminal[i]<<"'"<<"|";
}
cout<<endl;
cout<<non_terminal[i]<<"' -> ";
for(j=0;j<alpha.size();j++)
{
cout<<alpha[j]<<non_terminal[i]<<"'|";
}
cout<<"e"<<endl;
alpha.clear();
beta.clear();
}
else
{
cout<<non_terminal[i]<<" -> ";
for(j=0;j<beta.size();j++)
{
cout<<beta[j]<<"|";
}
cout<<endl;
}
}
return 0;
}