Mercurial > hg > Members > shinya > pyrect
view src/dotTranslator.py @ 12:41391400fe68
add GREPTranslator(Translator) and implement jit-compile-grep,
which faster than grep!! in case of regular expression search in large files.
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 04 Jul 2010 08:40:59 +0900 |
parents | 94984eaa03e2 |
children | 55684cb51347 |
line wrap: on
line source
#!/usr/bin/env python import re from translator import Translator from dfareg import CallGraph, Regexp class DotTranslator(Translator): """ DotToranslator This Class can translate from DFA or NFA into Dot Dot is Graph-generater using TeX. --code/graph/makepdf.sh is generate graph script. >>> string = \"(A|B)*C\" >>> reg = Regexp(string) >>> dfacg = CallGraph(reg.dfa) >>> nfacg = CallGraph(reg.nfa) >>> DotTranslator(string, dfacg).translate() >>> DotTranslator(string, nfacg).translate() """ def __init__(self, regexp, cg): Translator.__init__(self, regexp, cg) if self.cg.type is "DFA": self.name_hash = self.create_name_hash() def modify_state_name(self, state_name): if self.cg.type is "DFA": return "s"+self.name_hash[state_name] else: return "s"+state_name def emit_from_callgraph(self): self.emit(''' digraph G{ \td2tdocpreamble = "\\usetikzlibrary{automata}"; \td2tfigpreamble = "\\tikzstyle{every state}= \\ \t[draw=blue!50,very thick,shape=circle, fill=blue!20]"; \tnode [style="state"]; \tedge [lblstyle="fill=blue!20", style="arrows=->", topath="bend left"]; ''') self.emit("\t%s [style=\"state, initial\"]\n" % (self.modify_state_name(self.cg.start))) for accept in self.cg.accepts: self.emit("\t%s [style=\"state, accepting\"]\n" % (self.modify_state_name(accept))) for cur_state, trans in self.cg.map.iteritems(): for input, next_states in trans.iteritems(): if input is "" : input = "$\\varepsilon$" for next_state in next_states: self.emit("\t%s -> %s [texlbl=\"%s\"]\n" % (self.modify_state_name(cur_state), self.modify_state_name(next_state), input)) self.emit("}") def test(): import doctest doctest.testmod() ''' reg = Regexp("(A|B)*C") ct = CTranslator(reg.regexp, CallGraph(reg.dfa)) ct.translate() ''' if __name__ == '__main__' : test()