Mercurial > hg > Members > shinya > pyrect
changeset 9:973b596bd124
remove Regexp.emitDot()
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 03 Jul 2010 03:49:41 +0900 |
parents | a28f87d353bb |
children | 2d00b46ce34d |
files | code/graph/regdfa.dot.pdf src/cTranslator.py src/dfareg.py |
diffstat | 3 files changed, 11 insertions(+), 44 deletions(-) [+] |
line wrap: on
line diff
--- a/src/cTranslator.py Sat Jul 03 01:40:36 2010 +0900 +++ b/src/cTranslator.py Sat Jul 03 03:49:41 2010 +0900 @@ -4,8 +4,7 @@ from translator import Translator class CTranslator(Translator): - """ - CTranslator + """CTranslator This Calss can translate from DFA or NFA into C source code. DFA: A simple state transition as tail call (also can implement with CbC). NFA: using stack, deepening depth-first search. @@ -45,14 +44,16 @@ \tprintf(\"string: %%s\\n\", argv[1]); \t%s%s(argv[1]); """ % (self.regexp, len(self.cg.states), self.callType, self.modifyStateName(self.cg.start))) - if self.cg.type is "NFA" : self.emit("\treject(argv[1]);\n") + if self.cg.type is "NFA": + self.emit("\treject(argv[1]);\n") self.emit(""" \treturn 0; }\n\n""") for k, v in self.cg.map.iteritems(): self.emit(self.funType + self.modifyStateName(k) + "(char* s) {\n") - if self.debug: self.emit("\tprintf(\"state: %s, input: %%s\\n\", s);\n" % (k)) + if self.debug: + self.emit("\tprintf(\"state: %s, input: %%s\\n\", s);\n" % (k)) if self.cg.type is "NFA": sLocal = "s_local" self.emit("\tchar* %s = s;\n" % (sLocal)) @@ -70,9 +71,9 @@ self.emit("\t\tcase '%s': \n" % (input)) for nextState in nextStates: self.emit("\t\t\t%s%s(%s);\n" % (self.callType, self.modifyStateName(nextState), sLocal)) - if self.breakStatement != '' : self.emit(self.breakStatement+'\n') + if self.breakStatement != '': self.emit(self.breakStatement+'\n') - if k in self.cg.accepts : + if k in self.cg.accepts: self.emit( """\t\tcase '\\0':\n\t\t\t%saccept(%s);\n%s\n""" % (self.callType, sLocal, self.breakStatement)) if self.cg.type is "DFA": self.emit("\t\tdefault: %sreject(%s);\n\t}\n" % (self.callType, sLocal)) @@ -98,4 +99,4 @@ ct.translate() ''' -if __name__ == '__main__' : test() +if __name__ == '__main__': test()
--- a/src/dfareg.py Sat Jul 03 01:40:36 2010 +0900 +++ b/src/dfareg.py Sat Jul 03 03:49:41 2010 +0900 @@ -83,8 +83,7 @@ else: return Token(ch, Token.CHARACTER) -""" - Context-free Grammer: +"""Context-free Grammer: (A) expression -> subexpr EOF (B) subexpr -> seq '|' subexpr | seq (C) seq -> subseq | '' @@ -324,8 +323,7 @@ # create call graph (as dictionary) class CallGraph(object): - """ - CallGraph is State Transition Diagram. + """CallGraph is State Transition Diagram. it's can be create from DFA or DFA. >>> reg = Regexp(\"AA*|B\") >>> dfacg = CallGraph(reg.dfa) @@ -417,30 +415,6 @@ callGraph[self.set2name(state)] = caseMap return (callGraph, accepts) -def dfa2Dot(cg, regexp): - print """ -digraph G { - d2ttikzedgelabels = true; - d2tstyleonly = true; - d2tdocpreamble = \"\usetikzlibrary{automata}\"; - d2tfigpreamble = \"\tikzstyle{every state}=\ - [draw=blue!50, shape=circle, very thick,fill=blue!20]\"; - edge [lblstyle=\"fill=blue!20\", style=\"arrows=->\", topath=\"bend left\"]; - node [shape=\"circle\", style=\"state\"];\n -""" - def escape(string): - return re.sub("_", "", string) - - print " %s [style=\"state, initial\"]" % (escape(cg.start)) - for accept in cg.accepts: - print " %s [style=\"state, accepting\"]" % (escape(accept)) - - for cur_state, trans in cg.map.iteritems(): - for case, next_state in trans.iteritems(): - if next_state != "accept": - print " %s -> %s [texlbl=\"%s\"];\n" % (escape(cur_state), escape(next_state), case) - print "}" - class Regexp(object): def __init__(self, regexp): @@ -459,18 +433,10 @@ runtime = self.dfa.getRuntime() return runtime.doesAccept(string) - def emitCbC(self, emitCFlag=False): - cg = CallGraph(self.dfa) - dfa2CbC(cg, self.regexp, emitCFlag) - - def emitDot(self): - cg = CallGraph(self.dfa) - dfa2Dot(cg, self.regexp) - def compile(regexp): return Regexp(regexp) -def test(a): +def test(): import doctest doctest.testmod()