0

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:

E
E+T|T
T
T*F|F
F
(E)|id|number

and the required output is supposed to be like this one :

E->[TE']
T->[FT']
F->[(E), id, number]
E'->[+TE', !]
T'->[*FT', !]

I wrote that code which is storing input in Arraylists to iterate over them and produce the output:

    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<String> leftRules = new ArrayList<>();
        static ArrayList<String> rightRules = new ArrayList<>();
        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 < leftRules.size(); i++) {
                for (int j = 1; j <= 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<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()>=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<leftRules.size();i++)
            {
                System.out.print(leftRules.get(i)+"->");
                System.out.println("["+rightRules.get(i)+"]");
            }

        }


    }

I debugged the code many times trying to figure out why Am I getting output like this

E->[TE']
T->[+TE',!]
F->[T]
E'->[T*F]

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 ?

Bassem
  • 43
  • 2
  • 9
  • See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. Also, please look into the concept of *incremental programming*: Write one tiny block of code at a time and debug that before going on to the next. – Prune Mar 06 '17 at 18:31
  • Your posting doesn't give us much to go on: perhaps 60 lines of active code, no algorithm description, all too many meaningless identifiers, and no debugging output. If nothing else, use your main close-in weapon: print out useful variable values at critical points to see where your code starts to deviate from expectations. *I* can't tell, since you've given us all too few clues about how this is supposed to work. – Prune Mar 06 '17 at 18:34
  • The good news is that you *did* give us the code needed to reproduce the problem. – Prune Mar 06 '17 at 18:35
  • @Prune I just find it an easy code which doesn't need more illustration if you can help me and need more illustration for something tell me, please! – Bassem Mar 06 '17 at 18:44

0 Answers0