Mercurial > hg > Members > shinya > pyrect
view src/grep_translator.py @ 16:100efeeb2ad9
remove unnecessary variable "s_local" in cTranslator.
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 05 Jul 2010 06:25:44 +0900 |
parents | 55684cb51347 |
children | 5149b48b22a9 |
line wrap: on
line source
from cTranslator import CTranslator from dfareg import Regexp, CallGraph class GREPTranslateExeption(Exception): pass class GREPTranslator(CTranslator): """GREPTranslator >>> string = \"(A|B)*C\" >>> reg = Regexp(string) >>> dfacg = CallGraph(reg.dfa) >>> tje = GREPTranslator(string, dfacg) >>> tje.translate() """ def __init__(self, regexp, cg): if cg.type == "NFA": raise GREPTranslateExeption("can't translate grep from NFA") CTranslator.__init__(self, regexp, cg) self.funType = 'int ' self.callType = 'return ' self.breakStatement = '' def emit_accept_state(self): self.emit (""" %saccept(char* s) { \treturn 1; }\n""" % self.funType) def emit_reject_state(self): self.emit (""" %sreject(char* s) { \treturn 0; }\n""" % self.funType) def emit_initialization(self): self.emit("#include <stdio.h>\n") self.emit("#include <stdlib.h>\n") self.emit("#include <string.h>\n\n") self.emit("#define BUFSIZE 1024\n\n") for state in self.cg.map.iterkeys(): self.emit(self.funType + self.modify_state_name(state) + "(char* s);\n") self.emit(self.funType + 'accept(char* s);\n') self.emit(self.funType + 'reject(char* s);\n') def emit_driver(self): self.emit(""" int match(char *text) { do { if (%s(text)) return 1; } while (*text++ != '\\0'); return 0; }\n\n""" % (self.modify_state_name(self.cg.start))) self.emit(open("template/grep.template", "r").read()) self.emit("\n") def emit_state(self, cur_state, transition): self.emit(self.funType + self.modify_state_name(cur_state) + "(char* s) {\n") if cur_state in self.cg.accepts: self.emit("\treturn accept(s);\n") else: if transition: if self.cg.type == "DFA": self.emit_switch(transition, default="reject") else: self.emit_switch(transition) self.emit("}\n\n") def test(): import doctest doctest.testmod() if __name__ == '__main__': test()