Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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> productions = readCFGFromFile(filePath);

simplifyEpsilonRules(productions); simplifyUselessRules(productions);

printSimplifiedCFG(productions); }

private static Map> readCFGFromFile(String filePath) throws Exception { Map> productions = new HashMap<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { String[] rule = line.split("-"); String lhs = rule[0].trim(); String[] rhsList = rule[1].trim().split("\\|"); List rhs = new ArrayList<>(Arrays.asList(rhsList)); productions.put(lhs, rhs); } } return productions; }

private static void simplifyEpsilonRules(Map> productions) { Set epsilonProducibleVariables = new HashSet<>(); do { epsilonProducibleVariables.clear(); for (Map.Entry> entry : productions.entrySet()) { String variable = entry.getKey(); List variableProductions = entry.getValue();

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> entry : productions.entrySet()) { String variable = entry.getKey(); List variableProductions = entry.getValue(); List newProductions = new ArrayList<>();

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> productions) { Set reachableVariables = new HashSet<>(); Set terminals = new HashSet<>();

Queue queue = new LinkedList<>(); queue.add("S"); // Assuming S is the start variable

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 uselessVariables = new HashSet<>(productions.keySet()); uselessVariables.removeAll(reachableVariables); for (String variable : uselessVariables) { productions.remove(variable); }

// Remove productions containing useless terminals for (Map.Entry> entry : productions.entrySet()) { List variableProductions = entry.getValue(); variableProductions.removeIf(production -> { for (char symbol : production.toCharArray()) { if (Character.isLowerCase(symbol) && !terminals.contains(String.valueOf(symbol))) { return true; } } return false; }); } }

private static void printSimplifiedCFG(Map> productions) { for (Map.Entry> entry : productions.entrySet()) { String variable = entry.getKey(); List variableProductions = entry.getValue(); System.out.print(variable + " - "); System.out.println(String.join(" | ", variableProductions)); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are the methods to calculate retained earnings?

Answered: 1 week ago