Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

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

simplifyEpsilonRules(productions); simplifyUselessRules(productions);

printSimplifiedCFG(productions); }

private static Map> readCFGFromFile(String filePath) throws IOException { 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.containsKey(String.valueOf(symbol))) { List epsilonProductions = productions.get(String.valueOf(symbol)); if (epsilonProductions != null) { for (String epsilonProduction : epsilonProductions) { 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 to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Database Systems For Advanced Applications Dasfaa 2023 International Workshops Bdms 2023 Bdqm 2023 Gdma 2023 Bundlers 2023 Tianjin China April 17 20 2023 Proceedings Lncs 13922

Authors: Amr El Abbadi ,Gillian Dobbie ,Zhiyong Feng ,Lu Chen ,Xiaohui Tao ,Yingxia Shao ,Hongzhi Yin

1st Edition

3031354141, 978-3031354144

More Books

Students explore these related Databases questions