-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleft-recursion.java
71 lines (56 loc) · 1.14 KB
/
left-recursion.java
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
import java.util.*;
class LeftRecursion
{
String end,newEnd[];
char start;
Scanner S;
LeftRecursion()
{
newEnd=new String[2];
S=new Scanner(System.in);
System.out.print("Enter Starting Symbol:");
start=S.next().charAt(0);
System.out.print("Enter Ending Symbol:");
end=S.next();
}
boolean checkLeftrecursion(char Tstart,String Tend)
{
if((Tend.charAt(0)<'A'&&Tend.charAt(0)>'Z')||(Tend.charAt(0)!=Tstart))
{
return(false);
}
return(true);
}
void removeLeftRecursion()
{
int index;
if(checkLeftrecursion(start,end))
{
if((index=end.indexOf('|'))==-1)
{
index=end.length()-1;
}
String alpha=end.substring(1,index),beta=end.substring(index+1,end.length());
if(beta.equals(""))
{
beta=beta+'!';
}
System.out.print("Alpha:"+alpha+" beta:"+beta+"\n");
newEnd[0]=beta+'X';
newEnd[1]=alpha+"X|!";
System.out.print(start+"->"+newEnd[0]+"\nX->"+newEnd[1]);
}
else
{
System.out.print("Left recursion is not present!");
}
}
}
class LeftRecursionMain
{
public static void main(String[] a)
{
LeftRecursion L=new LeftRecursion();
L.removeLeftRecursion();
}
}