Code Helper

Data Structures and Algorithms

Hello Everyone,

The Code give below is a solution of a problem, where you have been given set of grammars and you have to draw first and follow table from it. You can learn about first and follow from here: https://www.jambe.co.nz/UNI/FirstAndFollowSets.html

            
              
import java.util.*;
class Solution{
    public static void main(String[] args){

        String grammar = "S->ABCDE, A->a|(-, B->b|(-, C->c, D->d|(-, E->e|(-";
        findFollow(grammar);
        System.out.println("Variable\tFirst() \t\t\t Variable \t Follow()");
        for(String key : productionTable.keySet()){
            System.out.printf("%5s %19s",key,firstTable.get(key));
            System.out.printf("\t\t\t%5s %20s\n",key,followTable.get(key));
        }
    }

    private static LinkedHashMap> productionTable = new LinkedHashMap<>();
    private static LinkedHashMap> firstTable = new LinkedHashMap<>();
    private static LinkedHashMap> followTable = new LinkedHashMap<>();
    private static String startSymbol = "S";

    public static void findFirst(String grammar){
        String[] productions = grammar.split(", ");
        List row1 = new ArrayList<>();
        for(int j=0; j");
            String[] right = leftAndRight[1].split("\\|");
            for(String val : right){
                row1.add(val.trim());
            }
            productionTable.put(leftAndRight[0],new ArrayList(row1));
            row1.clear();
        }

        Set basket = new HashSet();
        for(String key : productionTable.keySet()){
            List row = productionTable.get(key);
            for(String value : row){
                isNull = false;
                value = value.trim();

                if(value.charAt(0) >= 97 && value.charAt(0) <= 122){
                    basket.add(String.valueOf(value.trim().charAt(0)));
                }else if(value.equals("(-")){
                    basket.add("(-");
                }else if(value.charAt(0) >= 65 && value.charAt(0) <= 90){
                    int index = 1;
                    returnFirst(productionTable,String.valueOf(value.charAt(0)),basket);
                        if(isNull && value.length() - 1 >= index && Character.isUpperCase(value.charAt(index))){
                            while(isNull && value.length()-1>=index && Character.isUpperCase(value.charAt(index))){
                            isNull = false;
                            if(value.length()>index){
                                basket.remove("(-");
                                returnFirst(productionTable,String.valueOf(value.charAt(index)),basket);
                                index++;
                            }
                        }
                    }else if(value.length()>index && isNull && !Character.isUpperCase(value.charAt(index))){
                        basket.add(String.valueOf(value.charAt(index)).trim());
                    }
                }
            }
            firstTable.put(key,new HashSet(basket));
            basket.clear();   
        }
    }
    static boolean isNull = false;


    private static void returnFirst(LinkedHashMap> table,String start,Set basket){
        for(String key : table.keySet()){
            if(key.equals(start.trim())){
                List row = table.get(key);
                for(String value : row){
                    if(value.charAt(0)>=97 && value.charAt(0) <= 122){
                        basket.add(String.valueOf(value.charAt(0)).trim());
                    }else if(value.charAt(0) >= 65 && value.charAt(0) <= 90){
                        returnFirst(table, String.valueOf(value.charAt(0)), basket);
                    }else if(value.trim().equals("(-")){
                        basket.add("(-");
                        isNull = true;
                    }
                }
            }
        }   
    }

    public static void findFollow(String grammar){
        findFirst(grammar);
        Set basket = new HashSet<>();
        for(String pro : productionTable.keySet()){
            followOf(pro, basket);
            followTable.put(pro, new HashSet(basket));
            basket.clear();
        }
    }

    private static void followOf(String var, Set basket){
        if(var.equals(startSymbol)){
            basket.add("$");
        }

        for(String key : productionTable.keySet()){
            List list = productionTable.get(key);
            Iterator listIt = list.listIterator();
            while(listIt.hasNext()){
                String next = listIt.next();
                if(next.contains(var)){
                    int in = next.indexOf(var);
                    if(next.indexOf(var) == next.length() - 1 && var != key){
                        if(followTable.containsKey(key)){
                            for(String val : followTable.get(key)){
                                basket.add(val);
                            }
                        }else{
                            followOf(key, basket);
                        }
                        
                    }else if((in + 1) < next.length() && Character.isLowerCase(next.charAt(in + 1))){
                        basket.add(String.valueOf(next.charAt(in + 1)));
                        continue;
                    }else if((in + 1) < next.length() && Character.isUpperCase(next.charAt(in + 1))){
                        int index = next.indexOf(var) + 1;
                            for(int i = index; i < next.length(); i++){
                                if(i == next.length() - 1){
                                    if(Character.isLowerCase(next.charAt(i))){
                                        basket.add(String.valueOf(next.charAt(i)));
                                    }else{
                                        firstOf(Character.toString(next.charAt(i)), basket);
                                        if(basket.contains("(-")){
                                        basket.remove("(-");
                                        followOf(key,basket);
                                        }else{
                                            
                                            break;
                                        }
                                    }

                                }else if(i < next.length() && Character.isUpperCase(next.charAt(i))){
                                    firstOf(Character.toString(next.charAt(i)), basket);
                                    if(basket.contains("(-")){
                                        basket.remove("(-");
                                        continue;
                                    }else{
                                        break;
                                    }
                                }else if(i < next.length() && Character.isLowerCase(next.charAt(i))){
                                    basket.add(String.valueOf(next.charAt(i)));
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

    private static void firstOf(String pro, Set basket){
           Set s = firstTable.get(pro);
           for(String val : s){
                basket.add(val);
       } 
    }
}