Left

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.8 kB
0
Indexable
Never
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cout<<"Enter the number of productions: ";
    cin>>n;
    string s[n];
    cout<<"Enter the productions: ";
    for(int i=0;i<n;i++){
        cin>>s[i];
    }
    for(int i=0;i<n;i++){
        int flag=0;
        for(int j=0;j<s[i].length();j++){
            if(s[i][j]==s[i][0] && s[i][j+1]=='-'){
                flag=1;
                break;
            }
        }
        if(flag==1){
            cout<<"Left recursion found in production "<<i+1<<endl;
            string temp="";
            for(int j=0;j<s[i].length();j++){
                if(s[i][j]=='-'){
                    temp=s[i].substr(j+1,s[i].length()-j-1);
                    break;
                }
            }
            string temp1="";
            for(int j=0;j<s[i].length();j++){
                if(s[i][j]=='-'){
                    temp1=s[i].substr(0,j);
                    break;
                }
            }
            string temp2="";
            for(int j=0;j<s[i].length();j++){
                if(s[i][j]=='-'){
                    temp2=s[i].substr(j+1,s[i].length()-j-1);
                    break;
                }
            }
            string temp3="";
            for(int j=0;j<s[i].length();j++){
                if(s[i][j]=='-'){
                    temp3=s[i].substr(0,j);
                    break;
                }
            }
            cout<<"The new productions are: "<<endl;
            cout<<temp1<<"->"<<temp<<temp1<<"'"<<endl;
            cout<<temp1<<"'->"<<temp2<<temp1<<"'|"<<'e'<<endl;
        }
        else{
            cout<<"No left recursion found in production "<<i+1<<endl;
        }
    }
    return 0;
}