Question
CAN YOU FIX THE FOLLOWING ERROR IN MY CODE PLEASE: Exception in thread main java.lang.NullPointerException: Cannot invoke java.util.List.iterator() because the return value of java.util.Map.get(Object) is
CAN YOU FIX THE FOLLOWING ERROR IN MY CODE PLEASE:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because the return value of "java.util.Map.get(Object)" is null at CFGSimplifier.simplifyEpsilonRules(CFGSimplifier.java:72) at CFGSimplifier.main(CFGSimplifier.java:15)
USE THIS EXAMPLE TO SEE IF IT WORKS PLEASE
S-AaB|aaB
A-0
B-baA|0
import java.io.BufferedReader; import java.io.FileReader; import java.util.*;
public class CFGSimplifier {
public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); System.out.println("Enter a text file: "); String filePath = scanner.nextLine(); scanner.close();
Map
simplifyEpsilonRules(productions); simplifyUselessRules(productions);
printSimplifiedCFG(productions); }
private static Map
private static void simplifyEpsilonRules(Map
if (variableProductions.contains("0")) { epsilonProducibleVariables.add(variable); }
for (String production : variableProductions) { for (char symbol : production.toCharArray()) { if(Character.isUpperCase(symbol) && productions.get(String.valueOf(symbol)) != null){ for (String epsilonProduction : productions.get(String.valueOf(symbol))){ if (epsilonProducibleVariables.contains(String.valueOf(symbol))) { epsilonProducibleVariables.add(variable); break; } } } } } }
for (Map.Entry
for (String production : variableProductions) { if (!production.equals("0")) { newProductions.add(production); }
for (String otherProduction : productions.get(production)) { if (!newProductions.contains(otherProduction)) { newProductions.add(variable + otherProduction); } } } productions.put(variable, newProductions); }
} while (!epsilonProducibleVariables.isEmpty()); }
private static void simplifyUselessRules(Map
Queue
while (!queue.isEmpty()) { String variable = queue.poll(); reachableVariables.add(variable);
for (String production : productions.getOrDefault(variable, Collections.emptyList())) { for (char symbol : production.toCharArray()) { if (Character.isUpperCase(symbol) && !reachableVariables.contains(String.valueOf(symbol))) { queue.add(String.valueOf(symbol)); } else if (Character.isLowerCase(symbol)) { terminals.add(String.valueOf(symbol)); } } } }
Set
// Remove productions containing useless terminals for (Map.Entry
private static void printSimplifiedCFG(Map
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started