设计一个程序,删除文法中的无用字符
如下程序所示,在第一行输入文法初始符号,下一行输入非终结符号,以空格分割,再下一行输入终结符号,以空格分隔。下一行输入规则数量,之后分行输入规则,规则左侧右侧用空格分隔。经处理后程序输出不包含无用字符的规则。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 import javafx.util.Pair;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;import java.util.*;public class WipeOff { private LinkedList<String> Vn = new LinkedList <>(); private LinkedList<String> Vt = new LinkedList <>(); private LinkedList<Pair<String, String>> P = new LinkedList <>(); private HashMap<String, Boolean> map = new HashMap <>(); private String Dad; private void removeSelf () { P.removeIf(p -> { if (p.getKey().equals(p.getValue())) { System.out.println("remove: " + p.getKey() + " ::= " + p.getValue()); return true ; } return false ; }); } private void removeInactive () { LinkedList<String> inActive = new LinkedList <>(Vn); for (int j = 0 ; j < P.size(); j++) { for (Pair<String, String> p : P) { String k = p.getKey(); if (map.get(k)) { continue ; } String str = p.getValue(); int length = str.length(); boolean flag = true ; for (int i = 0 ; i < length; i++) { if (!map.get(str.substring(i, i + 1 ))) { flag = false ; break ; } } map.replace(p.getKey(), flag); if (flag) { inActive.remove(p.getKey()); } } } P.removeIf(p -> { if (inActive.contains(p.getKey())) { System.out.println("remove: " + p.getKey() + " ::= " + p.getValue()); return true ; } for (int i = 0 ; i < p.getValue().length(); i++) { if (inActive.contains(p.getValue().substring(i, i + 1 ))) { System.out.println("remove: " + p.getKey() + " ::= " + p.getValue()); return true ; } } return false ; }); } private void removeUnreachable () { Iterator<Pair<String, String>> iterator = P.iterator(); P.removeIf(p -> { if (p.getKey().equals(Dad)) { return false ; } boolean flag = false ; for (Pair<String, String> p2 : P) { if (p2.getValue().contains(p.getKey())) { flag = true ; break ; } } if (!flag) { System.out.println("remove: " + p.getKey() + " ::= " + p.getValue()); return true ; } return false ; }); } public static void main (String[] args) throws IOException { System.setIn(new FileInputStream ("in2.txt" )); WipeOff wipeOff = new WipeOff (); Scanner sc = new Scanner (System.in); System.out.println("Please input Dad" ); wipeOff.Dad = sc.nextLine(); System.out.println("Please input the list of Vn" ); List<String> list = Arrays.asList(sc.nextLine().split(" " )); wipeOff.Vn = new LinkedList <>(list); Iterator<String> iterator = wipeOff.Vn.iterator(); while (iterator.hasNext()) { wipeOff.map.put(iterator.next(), false ); } System.out.println("Please input the list of Vt" ); list = Arrays.asList(sc.nextLine().split(" " )); wipeOff.Vt = new LinkedList <>(list); iterator = wipeOff.Vt.iterator(); while (iterator.hasNext()) { wipeOff.map.put(iterator.next(), true ); } System.out.println("Please input the num of P" ); int numRules = sc.nextInt(); String[] str; System.out.println("Please input the list of P" ); sc.nextLine(); while (numRules-- > 0 ) { str = sc.nextLine().split(" " ); wipeOff.P.push(new Pair <>(str[0 ], str[1 ])); } wipeOff.removeSelf(); wipeOff.removeInactive(); wipeOff.removeUnreachable(); System.setOut(new PrintStream (new FileOutputStream ("out.txt" ))); for (Pair<String, String> p : wipeOff.P) { System.out.println(p.getKey()+" ::= " +p.getValue()); } } }