package leftrecursion;
import java.util.Scanner;
public class LeftRecursion {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter number of production");
int prod=sc.nextInt();
String leftprod[]=new String[prod];
String rightprod[]= new String[prod];
int flag=0;
System.out.println("Enter Non-Teminals of left production");
for (int i = 0; i < prod; i++) {
leftprod[i]=sc.next();
}
System.out.println("Enter Non-Teminals/terminals of right production");
for (int i = 0; i < prod; i++) {
rightprod[i]=sc.next();
}
System.out.println("The productions are:");
for (int i = 0; i < prod; i++) {
System.out.println(""+leftprod[i]+"-->"+rightprod[i]);
}
for (int i = 0; i < leftprod.length; i++) {
for (int j = 0; j < rightprod.length; j++) {
if(leftprod[i].charAt(0)==rightprod[j].charAt(0))
{
flag=1;
}
}
}
if(flag==1)
{
System.out.println("left recursive");
}
else
System.out.println("non left recursive");
}
}
2
Enter Non-Teminals of left production
A
A
Enter Non-Teminals/terminals of right production
Ad
g
The productions are:
A-->Ad
A-->g
grammer left recursive
import java.util.Scanner;
public class LeftRecursion {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter number of production");
int prod=sc.nextInt();
String leftprod[]=new String[prod];
String rightprod[]= new String[prod];
int flag=0;
System.out.println("Enter Non-Teminals of left production");
for (int i = 0; i < prod; i++) {
leftprod[i]=sc.next();
}
System.out.println("Enter Non-Teminals/terminals of right production");
for (int i = 0; i < prod; i++) {
rightprod[i]=sc.next();
}
System.out.println("The productions are:");
for (int i = 0; i < prod; i++) {
System.out.println(""+leftprod[i]+"-->"+rightprod[i]);
}
for (int i = 0; i < leftprod.length; i++) {
for (int j = 0; j < rightprod.length; j++) {
if(leftprod[i].charAt(0)==rightprod[j].charAt(0))
{
flag=1;
}
}
}
if(flag==1)
{
System.out.println("left recursive");
}
else
System.out.println("non left recursive");
}
}
OUTPUT
Enter number of production2
Enter Non-Teminals of left production
A
A
Enter Non-Teminals/terminals of right production
Ad
g
The productions are:
A-->Ad
A-->g
grammer left recursive
No comments:
Post a Comment