Example #1
0
def dottree_rec(d, sym, rules, syms, printcover):
    "helper for dottree()"
    if syms.has_key(sym):
        return
    syms[sym] = 1

    name = str(sym.sym)
    if printcover:
        if sym.cover:
            name += "  cover %d,%d" % sym.cover
    d.add("    sym_%d [label=\"%s\", color=red];" %
          (hash(sym), printable(name, 1, 0)))
    for p in sym.possibilities:
        d.add("    sym_%d -> rule_%d;" % (hash(sym), hash(p)))
        if not rules.has_key(p):
            rules[p] = 1
            name = str(p.rule)
            if printcover:
                if p.cover:
                    name += "  cover %d,%d" % p.cover
            d.add("    rule_%d [label=\"%s\"];" %
                  (hash(p), printable(name, 1, 0)))
            for e in p.elements:
                d.add("    rule_%d -> sym_%s;" % (hash(p), hash(e)))
                dottree_rec(d, e, rules, syms, printcover)
Example #2
0
	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()
Example #3
0
    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()
Example #4
0
File: nfa.py Project: sprymix/pyggy
	def chsetname(self, ccl, quoted = 0) :
		if ccl == EPSILON :
			return "epsilon"
		set = []
		idx = 0
		while idx < 256 :
			if self.chsetcontains(ccl, idx) :
				start = idx
				idx += 1
				while idx < 256 and self.chsetcontains(ccl, idx) :
					idx += 1
				if idx > start + 1 :
					set.append("%s-%s" % (printable(chr(start), quoted), printable(chr(idx - 1), quoted)))
				else :
					set.append("%s" % printable(chr(start), quoted))
			idx += 1
		return string.join(set, ", ")
Example #5
0
 def chsetname(self, ccl, quoted=0):
     if ccl == EPSILON:
         return "epsilon"
     set = []
     idx = 0
     while idx < 256:
         if self.chsetcontains(ccl, idx):
             start = idx
             idx += 1
             while idx < 256 and self.chsetcontains(ccl, idx):
                 idx += 1
             if idx > start + 1:
                 set.append("%s-%s" % (printable(
                     chr(start), quoted), printable(chr(idx - 1), quoted)))
             else:
                 set.append("%s" % printable(chr(start), quoted))
         idx += 1
     return string.join(set, ", ")
Example #6
0
	def prodstr(self, prodno, quoted = 0, bracketted=1) :
		str = ""
		if bracketted :
			str += "["
		str += "(%d) %s ->" % (prodno, self.gram[prodno][0])
		for el in self.gram[prodno][1] :
			str += " "
			str += printable(el, quoted)
		if bracketted :
			str += "]"
		return str
Example #7
0
 def prodstr(self, prodno, quoted=0, bracketted=1):
     str = ""
     if bracketted:
         str += "["
     str += "(%d) %s ->" % (prodno, self.gram[prodno][0])
     for el in self.gram[prodno][1]:
         str += " "
         str += printable(el, quoted)
     if bracketted:
         str += "]"
     return str
Example #8
0
def dot_list_rec(level_list, sym, rules, syms):
    #mostly taken from pyggy.glt.dottree_rec
    "helper for dottree()"
    if syms.has_key(sym) :
        return
    syms[sym] = 1

    name = util.printable(str(sym.sym), 1, 0)
    string = 'sym_%d [label="%s", color=red];\n' % (hash(sym), name)
    for p in sym.possibilities :
        #string += "sym_%d -> rule_%d;\n" % (hash(sym), hash(p))
        if not rules.has_key(p):
            rules[p] = 1
            name = util.printable(str(p.rule), 1, 0)
            #string += 'rule_%d [label="%s"];\n' % (hash(p), name)
            for e in p.elements:
                if isinstance(e.sym, tuple):
                    string += 'sym_%d [label="%s"];\n' % (hash(e), e.sym[1])
                string += "sym_%d -> sym_%d;\n" % (hash(sym), hash(e))
                dot_list_rec(level_list, e, rules, syms)
        level_list.append(string)
Example #9
0
File: glr.py Project: sprymix/pyggy
def dottree_rec(d, sym, rules, syms, printcover) :
	"helper for dottree()"
	if syms.has_key(sym) :
		return
	syms[sym] = 1

	name = str(sym.sym)
	if printcover :
		if sym.cover :
			name += "  cover %d,%d" % sym.cover
	d.add("    sym_%d [label=\"%s\", color=red];" % (hash(sym), printable(name, 1, 0)))
	for p in sym.possibilities :
		d.add("    sym_%d -> rule_%d;" % (hash(sym), hash(p)))
		if not rules.has_key(p) :
			rules[p] = 1
			name = str(p.rule)
			if printcover :
				if p.cover :
					name += "  cover %d,%d" % p.cover
			d.add("    rule_%d [label=\"%s\"];" % (hash(p), printable(name, 1, 0)))
			for e in p.elements :
				d.add("    rule_%d -> sym_%s;" % (hash(p), hash(e)))
				dottree_rec(d, e, rules, syms, printcover)
Example #10
0
	def printtab(self) :
		print "Shifts"
		for idx in range(len(self.states)) :
			print "State %3d:" % idx,
			for (st,tr),ns in self.goto.items() :
				if st != idx :
					continue
				if isinstance(tr, str) :
					name = printable(tr)
				else :
					name = self.prodstr(tr)
				print "  on %s goto %d" % (name, ns)
			print
		print
Example #11
0
 def printtab(self):
     print "Shifts"
     for idx in range(len(self.states)):
         print "State %3d:" % idx,
         for (st, tr), ns in self.goto.items():
             if st != idx:
                 continue
             if isinstance(tr, str):
                 name = printable(tr)
             else:
                 name = self.prodstr(tr)
             print "  on %s goto %d" % (name, ns)
         print
     print
Example #12
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()
Example #13
0
File: dfa.py Project: 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()