PLEASE FIX THIS ERROR IN THE 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
PLEASE FIX THIS ERROR IN THE 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:78) at CFGSimplifier.main(CFGSimplifier.java:16)
CHECK IF IT WORK BY USING A TXT FILE WITH THE FOLLOWING IN IT:
S-AaB|aaB
A-0
B-baA|0
THE EXPECTED ASWER SHOULD BE:
S-aB|aaB|a|aa
B-ba
import java.io.BufferedReader; import java.io.FileReader; import java.util.*; import java.io.*;
public class CFGSimplifier {
public static void main(String[] args) throws IOException { 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.containsKey(String.valueOf(symbol))) { List
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
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