下面是书中给出的算法, 简单的描述了两句。但是实际上实现起来没有那么简单。
在百度搜索提取左部公因算法时也只有理论上的知识,并没有实际的算法。
https://github.com/hixiaosan/dragon_algorithm.git
type Tree struct { Root *Node } type Node struct { Child []*Node // 子节点 Pros []*Production // 产生式索引 Sym Symbolic // 当前符号 } // 生成分析树 func InitProductionTree(parent *Node, pros []*Production, idx int) { fmt.Println("InitProductionTree") groupPros := make(map[Symbolic][]*Production) for i := 0; i < len(pros); i++ { if len(pros[i].body) > idx { sym := *pros[i].body[idx] if _, ok := groupPros[sym]; !ok { groupPros[sym] = make([]*Production, 0); } groupPros[sym] = append(groupPros[sym], pros[i]) } } for k, v := range groupPros { fmt.Println(k) if len(v) > 1 { // 重复前缀大于 1个的 加入子节点 child := &Node{Child: make([]*Node, 0), Pros:v, Sym: k} parent.Child = append(parent.Child, child) InitProductionTree(child, groupPros[k], idx + 1