import hjzgg.first.First;import java.util.LinkedHashMap;import java.util.Map;import java.util.Set;import java.util.TreeMap;import java.util.TreeSet;public class Follow { private Map> first = null; private Map > follow = new TreeMap >(); private Map mp = null; public Follow(Map mp, Map > first) { super(); this.first = first; this.mp = mp; } public Map > getFollowSet(){ return follow; } private void getFirstSet(Set st, String node, int k){ if(k >= node.length()) return; if(node.charAt(k)=='\'') --k; String nextNode = "" + node.charAt(k); if(k+1 st = null; for(String leftNode : mp.keySet()){ String rightNodes[] = mp.get(leftNode); for(int i=0; i @B if(follow.get(leftNode) == null) findFollow(leftNode); if(follow.get(curNode) == null){ st = new TreeSet (); st.addAll(follow.get(leftNode)); follow.put(curNode, st); } else follow.get(curNode).addAll(follow.get(leftNode)); } else { String nextNode = ""+rightNodes[i].charAt(index); if(index+1 < rightNodes[i].length() && rightNodes[i].charAt(index+1)=='\''){ nextNode += '\''; ++index; } if(mp.containsKey(nextNode)){ //非终结符 if(first.get(nextNode).contains(new Character('$'))){ //A->@B&, 而 &->$ if(follow.get(leftNode) == null) findFollow(leftNode); if(follow.get(curNode) == null){ st = new TreeSet (); st.addAll(follow.get(leftNode)); follow.put(curNode, st); } else follow.get(curNode).addAll(follow.get(leftNode)); } //好特殊的情况啊.... { //A->@B&, First(&)^$ 放入follow(B) Set tmpSt = new TreeSet (); getFirstSet(tmpSt, rightNodes[i], index); tmpSt.remove('$'); if(follow.get(curNode) == null){ st = new TreeSet (); st.addAll(tmpSt); follow.put(curNode, st); } else follow.get(curNode).addAll(tmpSt); } } else { //终结符 if(follow.get(curNode) == null){ st = new TreeSet (); st.add(nextNode.charAt(0)); follow.put(curNode, st); } else follow.get(curNode).add(nextNode.charAt(0)); } } index = rightNodes[i].indexOf(curNode, nextIndex); } } } } public String followKernealCode(){ String content = ""; boolean flag = true; for(String leftNode : mp.keySet()){ if(flag){ Set st = new TreeSet (); st.add('#'); follow.put(leftNode, st); flag = false; } findFollow(leftNode); } //打印first集合 System.out.println("Follow 集合如下:"); for(Map.Entry > entry : follow.entrySet()){ content += entry.getKey() + " : " + entry.getValue() + "\n"; System.out.println(entry.getKey() + " : " + entry.getValue()); } return content; } public static void main(String[] args) { String[] rightLinearGrammar = { "E->TE\'", "E\'->+TE\'|$", "T->FT\'", "T\'->*FT\'|$", "F->(E)|i" }; // String[] rightLinearGrammar = {// "S->ABc",// "A->a|$",// "B->b|$"// }; Map mp = new LinkedHashMap (); try{ for(int i=0; i "); String split2[] = split1[1].split("\\|"); mp.put(split1[0], split2); } } catch(Exception e){ e.printStackTrace(); System.out.println("右线性文法错误!"); } First first = new First(mp); first.firstKernealCode(); new Follow(mp, first.getFirstSet()).followKernealCode(); }}