Search
 
SCRIPT & CODE EXAMPLE
 

CPP

left recursion program in c++

#include <iostream>
#include <string>
using namespace std;
int main()
{
int n, j, l, i, k;
int length[10] = {};
string d, a, b, flag;
char c;
cout<<"Enter Parent Non-Terminal: ";
cin >> c;
d.push_back(c);
a += d + "'->";
d += "->";
b += d;
cout<<"Enter productions: ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout<<"Enter Production ";
cout<<i + 1<<" :";
cin >> flag;
length[i] = flag.size();
d += flag;
if (i != n - 1)
{
d += "|";	
}
}
cout<<"The Production Rule is: ";
cout<<d<<endl;
for (i = 0, k = 3; i < n; i++)
{
if (d[0] != d[k])
{
cout<<"Production: "<< i + 1;
cout<<" does not have left recursion.";
cout<<endl;
if (d[k] == '#')
{
b.push_back(d[0]);
b += "'";
}
else
{
for (j = k; j < k + length[i]; j++)
{
b.push_back(d[j]);	
}
k = j + 1;
b.push_back(d[0]);
b += "'|";
}
}
else
{
cout<<"Production: "<< i + 1 ;
cout<< " has left recursion";
cout<< endl;
if (d[k] != '#')
{
for (l = k + 1; l < k + length[i]; l++)
{
a.push_back(d[l]);	
}
k = l + 1;
a.push_back(d[0]);
a += "'|";
}
}
}
a += "#";
cout << b << endl;
cout << a << endl;
return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: Problems in your to-do list codechef solution in c++ 
Cpp :: c++ loop 
Cpp :: assert warning c++ 
Cpp :: librerias matematicas en c++ para numeros aleatorios 
Cpp :: can derived class access base class non-static members without object of the base class 
Cpp :: this is my p phone number in punjabi 
Cpp :: Arduino Access Point ESP8266 
Cpp :: unity decompile il2cpp 
Cpp :: calculate number of edges of graph in data structure c++ 
Cpp :: Code Example of Switch Statement in C++/Java 
Cpp :: Structure of s void function 
Cpp :: get array size 
Cpp :: std::string(size_t , char ) constructor: 
Cpp :: c++ rainbow text 
Cpp :: cpp super 
Cpp :: how to call subclass override method in c++ 
Cpp :: c++ cout int 
Cpp :: type casting in cpp 
Cpp :: delete a head node in link list 
Cpp :: c++ define constant 
Cpp :: c++ filesystem remove file 
Cpp :: nazi crosshair c++ 
C :: stop redis server 
C :: arduino wifi ip address to string 
C :: octave square each element matrix 
C :: sdl_renderfillrect 
C :: windeployqt example 
C :: how to read space separated words in c 
C :: what is strikethrough in markdown 
C :: c check if character is a digit 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =