예제 #1
0
파일: slrgram.py 프로젝트: sprymix/pyggy
	def dot(self) :
		d = dot.dot("LR0")
		for idx in range(len(self.states)) :
			label = "State %d" % idx
			for itnum in self.states[idx] :
				label += "\\n" + self.itemstr(itnum)
			d.add("    %d [label=\"%s\",shape=box];" % (idx, label))

			# make names for transitions to each next state
			name = ["" for st in range(len(self.states))]
			for (st,tr),ns in self.goto.items() :
				if st != idx :
					continue
				if name[ns] != "" :
					name[ns] += ", "
				if isinstance(tr, str) :
					name[ns] += printable(tr, 1)
				else :
					name[ns] += self.prodstr(tr, 1)

			# print all transitions that have names
			for ns in range(len(name)) :
				if name[ns] != "" :
					d.add("    %d -> %d [label=\"%s\"];" % (idx, ns, name[ns]))
		d.end()
		d.show()
예제 #2
0
파일: slrgram.py 프로젝트: hosford42/pyggy
    def dot(self):
        d = dot.dot("LR0")
        for idx in range(len(self.states)):
            label = "State %d" % idx
            for itnum in self.states[idx]:
                label += "\\n" + self.itemstr(itnum)
            d.add("    %d [label=\"%s\",shape=box];" % (idx, label))

            # make names for transitions to each next state
            name = ["" for st in range(len(self.states))]
            for (st, tr), ns in self.goto.items():
                if st != idx:
                    continue
                if name[ns] != "":
                    name[ns] += ", "
                if isinstance(tr, str):
                    name[ns] += printable(tr, 1)
                else:
                    name[ns] += self.prodstr(tr, 1)

            # print all transitions that have names
            for ns in range(len(name)):
                if name[ns] != "":
                    d.add("    %d -> %d [label=\"%s\"];" % (idx, ns, name[ns]))
        d.end()
        d.show()
예제 #3
0
파일: glr.py 프로젝트: sprymix/pyggy
def dotstack(heads) :
	"print out a tomita stack graphically"
	d = dot.dot("ParseStacks")
	visited = dict()
	for h in heads :
		dotstack_rec(d, h, visited, heads)
	d.end()
	d.show()
예제 #4
0
def dotstack(heads):
    "print out a tomita stack graphically"
    d = dot.dot("ParseStacks")
    visited = dict()
    for h in heads:
        dotstack_rec(d, h, visited, heads)
    d.end()
    d.show()
예제 #5
0
파일: glr.py 프로젝트: sprymix/pyggy
def dottree(tree, printcover = 0) :
	"""
	print out a tree (rooted with a symnode) graphically
	if printcover is true, print the cover of each node.
	"""
	if tree == None :
		return
	d = dot.dot("ParseTree")
	d.add("    start -> sym_%s;" % hash(tree))
	dottree_rec(d, tree, dict(), dict(), printcover)
	d.end()
	d.show()
예제 #6
0
def dottree(tree, printcover=0):
    """
	print out a tree (rooted with a symnode) graphically
	if printcover is true, print the cover of each node.
	"""
    if tree == None:
        return
    d = dot.dot("ParseTree")
    d.add("    start -> sym_%s;" % hash(tree))
    dottree_rec(d, tree, dict(), dict(), printcover)
    d.end()
    d.show()
예제 #7
0
파일: nfa.py 프로젝트: sprymix/pyggy
	def dot(self, mach) :
		d = dot.dot("nfa")
		d.add("    rankdir = LR;");
		for idx in range(mach.min, mach.max + 1) :
			trset, tr1, tr2, acc = self.states[idx]
			name = "%d" % idx
			if acc != NOACCEPT :
				name = "accept %d" % acc
			extra = ""
			if idx == mach.start :
				extra = ",color=red"
			d.add("    n%d [label=\"%s\"%s];" % (idx, name, extra))

			name = self.chsetname(trset, 1)
			if tr1 != NOSTATE :
				d.add("    n%d -> n%d [label=\"%s\"];" % (idx, tr1, name))
			if tr2 != NOSTATE :
				d.add("    n%d -> n%d [label=\"%s\"];" % (idx, tr2, name))
		d.end()
		d.show()
예제 #8
0
파일: nfa.py 프로젝트: hosford42/pyggy
    def dot(self, mach):
        d = dot.dot("nfa")
        d.add("    rankdir = LR;")
        for idx in range(mach.min, mach.max + 1):
            trset, tr1, tr2, acc = self.states[idx]
            name = "%d" % idx
            if acc != NOACCEPT:
                name = "accept %d" % acc
            extra = ""
            if idx == mach.start:
                extra = ",color=red"
            d.add("    n%d [label=\"%s\"%s];" % (idx, name, extra))

            name = self.chsetname(trset, 1)
            if tr1 != NOSTATE:
                d.add("    n%d -> n%d [label=\"%s\"];" % (idx, tr1, name))
            if tr2 != NOSTATE:
                d.add("    n%d -> n%d [label=\"%s\"];" % (idx, tr2, name))
        d.end()
        d.show()
예제 #9
0
    def dot(self, shownfastates=0, showccls=0):
        if showccls:
            # make names for the character classes so we dont repeat this often
            cclname = ["" for idx in range(len(self.uccls))]
            ch = 0
            while ch < self.maxchar + 1:
                minch = ch
                curccl = self.chr2uccl[chr(ch)]
                while ch < self.maxchar + 1 and self.chr2uccl[chr(
                        ch)] == curccl:
                    ch += 1
                maxch = ch - 1
                if cclname[curccl] != "":
                    cclname[curccl] += ", "
                cclname[curccl] += printable(chr(minch), 1)
                if minch < maxch:
                    cclname[curccl] += "-" + printable(chr(maxch), 1)

        d = dot.dot("dfa")
        for idx in range(1, len(self.rows)):
            xtra = ""
            name = "%d" % idx
            if shownfastates:
                name += ": %s" % self.dfastate[idx][0]
            if len(self.acc[idx]) > 0:
                name += " acc%s" % self.acc[idx]
                xtra += ", color=red"
            cnt = 0
            for s1, s2 in self.starts:
                if idx == s1 or idx == s2:
                    xtra += ", color=red"
                if idx == s1:
                    name += "\\nstart %d" % cnt
                if idx == s2:
                    name += "\\nstart ^%d" % cnt
                cnt += 1
            d.add("    n%d [label=\"%s\"%s];" % (idx, name, xtra))

            # make one arc to each reachable next state
            # this graph most accurately portrays the table we build, but
            # is sometimes hard to read.  perhaps we should have an option
            # for collecting the character classes for the edge into a single
            # character class and then converting that to a string.
            for ns in range(1, len(self.rows)):
                ccls = []
                for ccl in range(len(self.rows[idx])):
                    if self.rows[idx][ccl] == ns:
                        ccls.append(ccl)
                if ccls == []:
                    continue

                name = ""
                if showccls:  # build up name out of each ccl
                    for ccl in ccls:
                        if name != "":
                            name += "\\n"
                        name += "%d: %s" % (ccl, cclname[ccl])
                else:  # build up name out of the combined ccl
                    ch = 0
                    while ch < self.maxchar + 1:
                        minch = ch
                        while ch < self.maxchar + 1 and self.chr2uccl[chr(
                                ch)] in ccls:
                            ch += 1
                        maxch = ch - 1
                        if maxch < minch:
                            ch += 1
                            continue
                        if name != "":
                            name += ", "
                        name += printable(chr(minch), 1)
                        if maxch > minch:
                            name += "-" + printable(chr(maxch), 1)
                d.add("    n%d -> n%d [label=\"%s\"];" % (idx, ns, name))
        d.show()
예제 #10
0
파일: dfa.py 프로젝트: sprymix/pyggy
	def dot(self, shownfastates = 0, showccls = 0) :
		if showccls :
			# make names for the character classes so we dont repeat this often
			cclname = ["" for idx in range(len(self.uccls))]
			ch = 0
			while ch < self.maxchar + 1 :
				minch = ch
				curccl = self.chr2uccl[chr(ch)]
				while ch < self.maxchar + 1 and self.chr2uccl[chr(ch)] == curccl :
					ch += 1
				maxch = ch - 1
				if cclname[curccl] != "" :
					cclname[curccl] += ", "
				cclname[curccl] += printable(chr(minch), 1)
				if minch < maxch :
					cclname[curccl] += "-" + printable(chr(maxch), 1)

		d = dot.dot("dfa")
		for idx in range(1, len(self.rows)) :
			xtra = ""
			name = "%d" % idx
			if shownfastates :
				name += ": %s" % self.dfastate[idx][0]
			if len(self.acc[idx]) > 0 :
				name += " acc%s" % self.acc[idx]
				xtra += ", color=red"
			cnt = 0
			for s1,s2 in self.starts :
				if idx == s1 or idx == s2 :
					xtra += ", color=red"
				if idx == s1 :
					name += "\\nstart %d" % cnt
				if idx == s2 :
					name += "\\nstart ^%d" % cnt
				cnt += 1
			d.add("    n%d [label=\"%s\"%s];" % (idx, name, xtra))

			# make one arc to each reachable next state
			# this graph most accurately portrays the table we build, but
			# is sometimes hard to read.  perhaps we should have an option
			# for collecting the character classes for the edge into a single
			# character class and then converting that to a string.
			for ns in range(1, len(self.rows)) :
				ccls = []
				for ccl in range(len(self.rows[idx])) :
					if self.rows[idx][ccl] == ns :
						ccls.append(ccl)
				if ccls == [] :
					continue

				name = ""
				if showccls : # build up name out of each ccl
					for ccl in ccls :
						if name != "" :
							name += "\\n"
						name += "%d: %s" % (ccl, cclname[ccl])
				else : # build up name out of the combined ccl
					ch = 0
					while ch < self.maxchar + 1 :
						minch = ch
						while ch < self.maxchar + 1 and self.chr2uccl[chr(ch)] in ccls :
							ch += 1
						maxch = ch - 1
						if maxch < minch :
							ch += 1
							continue
						if name != "" :
							name += ", "
						name += printable(chr(minch), 1)
						if maxch > minch :
							name += "-" + printable(chr(maxch), 1)
				d.add("    n%d -> n%d [label=\"%s\"];" % (idx, ns, name))
		d.show()