title: Implementing Elimination of Immediate Left-Recursion in Java
body: <p>I am working on implementing a generic code to solve left recursion problem in a grammar using java so my code is working as follows I am reading an input like this as each line goes to the next line:</p>
  
  <pre><code>E
  E+T|T
  T
  T*F|F
  F
  (E)|id|number
  </code></pre>
  
  <p>and the required output is supposed to be like this one :</p>
  
  <pre><code>E-&gt;[TE']
  T-&gt;[FT']
  F-&gt;[(E), id, number]
  E'-&gt;[+TE', !]
  T'-&gt;[*FT', !]
  </code></pre>
  
  <p>I wrote that code which is storing input in Arraylists to iterate over them and produce the output:</p>
  
  <pre><code>    import java.io.BufferedReader;
      import java.io.FileReader;
      import java.io.IOException;
      import java.util.ArrayList;
  
      public class IleftRecursion {
            //storing each line in its corresponding Arraylist
          static ArrayList&lt;String&gt; leftRules = new ArrayList&lt;&gt;();
          static ArrayList&lt;String&gt; rightRules = new ArrayList&lt;&gt;();
          public static void read_file(String file) throws IOException {
              FileReader in = new FileReader(file);
              BufferedReader br = new BufferedReader(in);
              String line;
              while ((line = br.readLine()) != null) {
                  leftRules.add(line);
                  rightRules.add(br.readLine());
              }
              br.close();
  
          }
  
          public static void ss() {
  
              for (int i = 0; i &lt; leftRules.size(); i++) {
                  for (int j = 1; j &lt;= i - 1; j++) {
                               //splitting inputs on bars "|" to iterate through them
                      for (String x : rightRules.get(i).split("\\|")) {
                          if (x.contains(leftRules.get(j))) {
                              String f = "";
                              String ff = "";
                              for (int k=0; k&lt;rightRules.get(k).split("\\|").length;k++) {
  
                                  f = x;
                                  f = f.replaceAll(leftRules.get(i), rightRules.get(k).split("\\|")[k]);
  
                                  ff += f;
  
                              }
                              rightRules.remove(i);
                              rightRules.add(i, ff);
                          }
  
  
                      }
                  }
                  //Recursive or Not boolean
                  boolean isRec = false;
  
                  for (String z : rightRules.get(i).split("\\|")) {
                      if (z.startsWith(leftRules.get(i))) {
                          isRec = true;
                          break;
                      }
                  }
  
                  if (isRec) {
                      String a = "";
                      String b = "";
                      for (String s : rightRules.get(i).split("\\|")) {
  
                          if (s.startsWith(leftRules.get(i))) {
                              b += s.replaceAll(leftRules.get(i), "") + leftRules.get(i) + "',";
                          } else {
                              a += s + leftRules.get(i) + "'";
  
  
                          }
                      }
                      b += "!";
  
                      if(a.length()&gt;=1)
                      a.substring(1, a.length() - 1);
                      rightRules.add(i, a);
                      rightRules.add(i + 1, b);
                      leftRules.add(leftRules.get(i) + "'");
                  }
  
              }
          }
  
          public static void main(String[] args) throws IOException {
              read_file("Sample.in"); 
              ss();
              for (int i=0;i&lt;leftRules.size();i++)
              {
                  System.out.print(leftRules.get(i)+"-&gt;");
                  System.out.println("["+rightRules.get(i)+"]");
              }
  
          }
  
  
      }
  </code></pre>
  
  <p>I debugged the code many times trying to figure out why Am I getting output like this </p>
  
  <pre><code>E-&gt;[TE']
  T-&gt;[+TE',!]
  F-&gt;[T]
  E'-&gt;[T*F]
  </code></pre>
  
  <p>Which is missing One rule and also not all the new productions generated in the right way but I couldn't fix could anyone help me through that ?</p>