ImportHjzgg.first.First;ImportJava.util.LinkedHashMap;ImportJava.util.Map;ImportJava.util.Set;ImportJava.util.TreeMap;ImportJava.util.TreeSet; Public classFollow {PrivateMap<string, set<character>> first =NULL; PrivateMap<string, set<character>> follow =NewTreemap<string, set<character>>(); PrivateMap<string, string[]> MP =NULL; PublicFollow (Map<string, String[]> MP, Map<string, set<character>>First ) { Super(); This. First =First ; This. MP =MP; } PublicMap<string, set<character>>Getfollowset () {returnfollow; } Private voidGetfirstset (set<character> St, String node,intk) { if(k >= node.length ())return; if(Node.charat (k) = = ' \ ')--K; String NextNode= "" +Node.charat (k); if(K+1<node.length () && node.charat (k+1) = = ' \ ') {NextNode+ = ' \ '; ++K; } if(!mp.containskey (NextNode)) {//End PointSt.add (Nextnode.charat (0)); } Else{St.addall (First.get (NextNode)); if(First.get (NextNode). Contains (' $ '))) Getfirstset (St, node, K+1); } } Private voidFindfollow (String curnode) {Set<Character> st =NULL; for(String LeftNode:mp.keySet ()) {string rightnodes[]=Mp.get (Leftnode); for(inti=0; i<rightnodes.length; ++i) { intindex = rightnodes[i].indexof (curnode, 0); while(Index! =-1){ intNextindex = index + 1; if(Curnode.length () ==1 && index+1<rightnodes[i].length () && rightnodes[i].charat (index+1) = = ' \ ') {Index=Rightnodes[i].indexof (Curnode, Nextindex); Continue; } Index= index+curnode.length (); if(Index = = Rightnodes[i].length ()) {//end of non-endpoint, a->@b if(Follow.get (leftnode) = =NULL) Findfollow (Leftnode); if(Follow.get (curnode) = =NULL) {St=NewTreeset<character>(); St.addall (Follow.get (Leftnode)); Follow.put (Curnode, ST); } ElseFollow.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)) {//Non-Terminator if(First.get (NextNode). Contains (NewCharacter (' $ '))) {//A->@b&, while &->$ if(Follow.get (leftnode) = =NULL) Findfollow (Leftnode); if(Follow.get (curnode) = =NULL) {St=NewTreeset<character>(); St.addall (Follow.get (Leftnode)); Follow.put (Curnode, ST); } ElseFollow.get (Curnode). AddAll (Follow.get (Leftnode)); } //It 's a very special situation .{//A->@b&, First (&) ^$ put in follow (B)Set<character> Tmpst =NewTreeset<character>(); Getfirstset (Tmpst, rightnodes[i], index); Tmpst.remove ($); if(Follow.get (curnode) = =NULL) {St=NewTreeset<character>(); St.addall (TMPST); Follow.put (Curnode, ST); } ElseFollow.get (Curnode). AddAll (TMPST); } } Else{//Terminator if(Follow.get (curnode) = =NULL) {St=NewTreeset<character>(); St.add (Nextnode.charat (0)); Follow.put (Curnode, ST); } ElseFollow.get (Curnode). Add (Nextnode.charat (0)); }} Index=Rightnodes[i].indexof (Curnode, Nextindex); } } } } Publicstring Followkernealcode () {string content= ""; BooleanFlag =true; for(String leftNode:mp.keySet ()) {if(flag) {Set<Character> st =NewTreeset<character>(); St.add (‘#‘); Follow.put (Leftnode, ST); Flag=false; } findfollow (Leftnode); } //Print First collectionSystem.out.println ("Follow collection is as follows:"); for(Map.entry<string, set<character>>Entry:follow.entrySet ()) {Content+ = Entry.getkey () + ":" + entry.getvalue () + "\ n"; System.out.println (Entry.getkey ()+ " : " +Entry.getvalue ()); } returncontent; } Public Static voidMain (string[] args) {string[] Rightlineargrammar= { "E->te\ '", "E\ '->+te\ ' |$", "T->ft\ '", "T\ '->*ft\ ' |$", "F-> (E) |i" }; //string[] Rightlineargrammar = {//"S->abc",//"a->a|$",//"b->b|$"// };Map<string, string[]> MP =NewLinkedhashmap<string, string[]>(); Try{ for(inti=0; i<rightlineargrammar.length; ++i) {String split1[]= Rightlineargrammar[i].split ("-"); String split2[]= Split1[1].split ("\\|"); Mp.put (split1[0], split2); } } Catch(Exception e) {e.printstacktrace (); System.out.println ("Right linear grammar error!"); } First First=NewFirst (MP); First.firstkernealcode (); NewFollow (MP, First.getfirstset ()). Followkernealcode (); }}
Implementation of compiling principle LL1 grammar follow set algorithm