Importhjzgg.analysistable.AnalysisTable;ImportHjzgg.first.First;ImportHjzgg.follow.Follow;ImportHjzgg.treenode.TreeNode;Importjava.awt.BorderLayout;ImportJava.awt.Color;Importjava.awt.Dimension;ImportJava.awt.Font;ImportJava.awt.Graphics;Importjava.awt.HeadlessException;ImportJava.awt.Rectangle;Importjava.util.ArrayList;ImportJava.util.LinkedHashMap;ImportJava.util.Map;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JScrollPane; Public classtreegraphic {Private intFathernode;//Treegraphic Search is the start node Privatetreenode[] Treegraphic =NULL; Private Final intDistnode = 50;//the distance between nodes Private Final intHeightnode = 50;//the height of the node Private Final intWidthnode = 50;//width of the node Private Final intLevelheight = 100;//the height between the layer and the layer PrivateArraylist<rectangle> line =NewArraylist<rectangle>(); Private intCurY = 0; Private intCurX = 10; PublicTreegraphic (intFathernode, treenode[] treegraphic) { Super(); This. Fathernode =Fathernode; This. treegraphic =treegraphic; Preparegraphic (); } Public classTreeframeextendsjframe{PrivateJPanel Panel =NewJPanel () {@Overrideprotected voidpaintcomponent (Graphics g) {Super. paintcomponent (g); for(Rectangle rect:line) {g.drawline (Rect.x, Rect.y, Rect.width, rect.height); } } }; PrivateJScrollPane ScrollPane =NewJScrollPane (panel); PublicTreeframe ()throwsheadlessexception {Super(); Init (); } PublicTreeframe (String title)throwsheadlessexception {Super(title); Init (); } Private voidinit () {setlayout (NewBorderLayout ()); Panel.setlayout (NULL); Drawtree (Fathernode); Add (ScrollPane, borderlayout.center); intwidth = CurX + 50; intHeight = CurY + 50; //here to set the preferredsize of the panel, if the current frame size cannot be displayed preferredsize then the scroll bar will appearPanel.setpreferredsize (NewDimension (width, height)); if(width >) width = 600; if(Height > height) = 500; SetBounds (400, 100, width, height); SetVisible (true); } Public voidDrawtree (intCurnode) {JLabel label=NewJLabel (treegraphic[curnode].content, Jlabel.center); Label.setbounds (Treegraphic[curnode].getrect ()); Label.setfont (NewFont ("Arial", Font.Bold, 32)); Label.setopaque (true); Label.setbackground (color.red); Panel.add (label); if(Treegraphic[curnode].child.size () ==0)return; intx =treegraphic[curnode].getrect (). x; inty = Treegraphic[curnode].getlevel () *levelheight+Heightnode; intdist = Widthnode/treegraphic[curnode].child.size ();//the distance from the parent node to the child node lines for(inti=0; I<treegraphic[curnode].child.size (); ++i) {Drawtree (Treegraphic[curnode].child.get (i)); intxx = Treegraphic[treegraphic[curnode].child.get (i)].getrect (). x + WIDTHNODE/2; intyy = y+levelheight-Heightnode; Line.add (NewRectangle (x, y, xx, yy)); X+=Dist; } } } Private voidPreparenodelevel (intCurnode,intLevel) {//determine the hierarchy of each nodetreegraphic[curnode].setlevel (level); for(inti=0; I<treegraphic[curnode].child.size (); ++i) Preparenodelevel (Treegraphic[curnode].child.get (i), level+1); if(CurY < (level+1) *levelheight) CurY = (level+1) *Levelheight; } Private voidPreparenodesize (intCurnode) {//Determining the coordinate position of a node if(treegraphic[curnode].child.size () = = 0) {//End Point intx = CurX; Curx+=distnode+Widthnode; inty = treegraphic[curnode].getlevel () *Levelheight; Treegraphic[curnode].setrect (NewRectangle (x, Y, Widthnode, Heightnode)); return; } for(inti=0; I<treegraphic[curnode].child.size (); ++i) preparenodesize (Treegraphic[curnode].child.get (i)); intLeftchildx=treegraphic[treegraphic[curnode].child.get (0)].getrect (). x; intRightchildx=treegraphic[treegraphic[curnode].child.get (Treegraphic[curnode].child.size ()-1)].getrect (). x; //determine the coordinate size of the parent node based on the child's node on both left and right intParentx = (LEFTCHILDX+RIGHTCHILDX)/2; intParenty = Treegraphic[curnode].getlevel () *Levelheight; Treegraphic[curnode].setrect (NewRectangle (Parentx, Parenty, Widthnode, Heightnode)); } Private voidpreparegraphic () {preparenodelevel (Fathernode,0); Preparenodesize (Fathernode); } Public Static voidMain (string[] args) {//string[] Rightlineargrammar ={//"S->ictsa|a",//"A->$|es",//"C->b"//};string[] Rightlineargrammar= {//"E->te\ '",//"e\ '->+te\ ' |$",//"T->ft\ '",//"t\ '->*ft\ ' |$",//"f-> (E) |i""E->te\ '", "E\ '->ate\ ' |$", "T->ft\ '", "T\ '->mft\ ' |$", "F-> (E) |i", "A->+|-", "M->*|/" }; //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 (); Follow follow=NewFollow (MP, First.getfirstset ()); Follow.followkernealcode (); Analysistable analysistable=Newanalysistable (First.getfirstset (), Follow.getfollowset (), MP); Analysistable.analysistablekernealcode (); Analysistable.predictiveanalysis ("I+i* (i/i)-i#");
//Through the Analysis table, analyze the sentence generated by the analysis stack to establish an analysis tree, and return the analysis tree, using the program to draw tree Analysistable.analysistree (); Treegraphic treegraphic=Newtreegraphic (Analysistable.getfathernode (), analysistable.gettreegraphic ()); Treegraphic.NewTreeframe ("Parsing tree"); }}
Compilation principle LL1 Grammar Analysis Tree (drawing process) algorithm implementation