func parse(s []string) {
	s = append(s, EOF)
	stk := new(stack.Stack)
	stk.Push(EOF)
	stk.Push(startSymbol)
	for {
		time.Sleep(time.Second)
		top := stk.Top()
		fmt.Println(stk)
		if top == "epsilon" {
			stk.Pop()
		}
		if terminals.has(top) {
			if top == EOF {
				break
			}
			if top == s[0] {
				stk.Pop()
				s = s[1:]
			} else {
				fmt.Println("invalid string")
				break
			}
		}
		if nonTerminals.has(top) {
			stk.Pop()
			for i := len(table[s[0]][top]) - 1; i >= 0; i-- {
				stk.Push(table[s[0]][top][i])
			}
		}
	}
}
Exemple #2
0
func main() {

	var haystack stack.Stack
	haystack.Push("Hay")
	haystack.Push(-15)
	haystack.Push([]string{"Pin", "Clip", "Needle"})
	haystack.Push(81.52)

	fmt.Printf("The length of the stack is %d\n", haystack.Len())
	item, err := haystack.Top()
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf("Top element of the stack is %v\n", item)
	fmt.Println("Items in the stack are listed below:")
	for {

		item, err = haystack.Pop()
		if err != nil {
			break
		}
		fmt.Println(item)
	}

}