Example #1
0
func (self *Grammar) Follow(sym int) (s *set.Set) {
	has := func(slice []int, i int) (bool, int) {
		for j, v := range slice {
			if v == i {
				return true, j
			}
		}
		return false, 0
	}

	s = set.New()
	if sym == self.ORDER[0] {
		s.Add(end)
	}
	for _, nt := range self.ORDER {
		for _, p := range self.NTP[nt] {
			if h, i := has(self.P[p], sym); h {
				if i+1 < len(self.P[p]) {
					f := self.First(self.P[p][i+1])
					if f.Has(e) {
						f.Remove(e)
						s.Union(self.Follow(nt))
					}
					s.Union(f)
				} else if i+1 == len(self.P[p]) && sym != nt {
					s.Union(self.Follow(nt))
				}
			}
		}
	}
	return s
}
Example #2
0
func (self *Grammar) First(i int) (s *set.Set) {
	s = set.New()
	if self.ALL[i].Terminal {
		s.Add(i)
		return
	}
	for _, p := range self.NTP[i] {
		all_e := true
		for _, sym := range self.P[p] {

			syms := self.First(sym)
			s.Union(syms)
			if !syms.Has(e) {
				all_e = false
				break
			}
		}
		if all_e {
			s.Add(e)
		}
	}
	return
}