#!/usr/bin/python
from graph import Node

eingabe = raw_input("Bitte einen Ausdruck eingeben: ").strip().replace(" ", "")
next = eingabe[0]
def curr():
	global next
	return next
def shift():
	global next, eingabe
	eingabe = eingabe[1:]
	if len(eingabe):
		next = eingabe[0]
	else:
		next = ""
def debug(text):
	print text, " (", eingabe, ")"


# S -> F
def S():
	debug("S")
	return F()

# F -> T G
def F():
	debug("F")
	synth = T()
	return G(synth)

# G -> + T G
# G -> - T G
# G -> eps
def G(inherit):
	debug("G")
	if curr() == "+":
		shift()
		action = Node("+")
		action.addChild(inherit)
		action.addChild(T())
		return G(action)
	elif curr() == "-":
		shift()
		action = Node("-")
		action.addChild(inherit)
		action.addChild(T())
		return G(action)
	else:
		return inherit

# T -> E U
def T():
	debug("T")
	synth = E()
	return U(synth)

# U -> * E U
# U -> / E U
def U(inherit):
	debug("U")
	if curr() == "*":
		shift()
		action = Node("*")
		action.addChild(inherit)
		action.addChild(E())
		return U(action)
	elif curr() == "/":
		shift()
		action = Node("/")
		action.addChild(inherit)
		action.addChild(E())
		return U(action)
	else:
		return inherit

# E -> num
# E -> (F)
def E():
	debug("E")
	if curr() == "(":
		shift()
		ret = F()
		assert(curr() == ")")
		shift()
		return ret
	elif curr().isdigit():
		val = ""
		while curr().isdigit():
			val += curr()
			shift()
		return Node(val)

pst = S()
plot = pst.plotNode()
plot.show()
