Mercurial > hg > Members > shinya > pyrect
changeset 103:abe36c207b23
remove unnecessarily files.
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 14 Dec 2010 04:09:46 +0900 |
parents | a38b57592d45 |
children | f228eb779162 |
files | pyrect/translator/cbc_translator.py |
diffstat | 1 files changed, 0 insertions(+), 161 deletions(-) [+] |
line wrap: on
line diff
--- a/pyrect/translator/cbc_translator.py Tue Dec 14 04:08:56 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,161 +0,0 @@ -#!/usr/bin/env python - -from pyrect.regexp import Regexp -from pyrect.regexp.ast import * -from translator import Translator -from c_translator import CTranslator - -class CbCTranslator(CTranslator): - """ - CbCTranslator - >>> string = \"(A|B)*C\" - >>> reg = Regexp(string) - >>> ct = CbCTranslator(reg) - >>> ct.translate() - """ - def __init__(self, regexp): - Translator.__init__(self, regexp) - self.special_rule = (Range, BegLine, MBCharacter) - self.cg = regexp.dfacg - self.debug = False - self.interface = "unsigned char *s" - self.args = "s" - self.trans_stmt = self._trans_stmt(self.emit, self.args) - - def emit_accept_state(self): - self.emiti("__code accept(%s) {" % self.interface) - self.emit( "return;") - self.emitd("}", 2) - - def emit_reject_state(self): - self.emiti("__code reject(%s) {" % self.interface) - self.emit( "return;") - self.emitd("}", 2) - - def emit_driver(self): - self.emiti("int main(int argc, unsigned char* argv[]) {") - self.emit( 'buf = argv[1];') - self.emit( 'puts("regexp: %s");' % self.regexp.regexp) - self.emit( 'puts("number of state: %d");' % len(self.cg.states)) - self.emit( r'printf("string: %s\n", argv[1]);') - self.emit0( "goto %s((unsigned char*)argv[1]);" % self.state_name(self.cg.start)) - self.emit( "return 0;") - self.emitd("}", 2) - - def emit_switch(self, case, default=None): - if not case: - if default: - self.emit("goto %s(%s);" % (default, self.args)) - return - self.emiti("switch(*s++) {") - for case, next_ in case.iteritems(): - self.trans_stmt.emit(case, self.state_name(next_)) - if default: - self.emit("default: goto %s(%s);" % (default, self.args)) - self.emitd("}") - - def emit_state(self, cur_state, transition): - self.emiti("__code %s(%s) {" % (self.state_name(cur_state), self.interface)) - - if self.debug: - self.emit(r'printf("state: %s, input: %%s\n", s);' % cur_state) - if self.cg.type == "NFA": - default = None - if '' in transition: - epsilon_transition = transition.pop('') - for n in epsilon_transition: - self.emit("goto %s(%s);\n" % (self.state_name(n), self.args)) - else: - default = "reject" - - any_ = None - - for input_ in transition.keys(): - if type(input_) in self.special_rule: - self.trans_stmt.emit(input_, self.state_name(transition.pop(input_))) - elif type(input_) is AnyChar: - any_ = (input_, self.state_name(transition.pop(input_))) - default = None - - if cur_state in self.cg.accepts: - eol = Character('\0') - transition[eol] = "accept" - - self.emit_switch(transition, default) - - if any_: - self.trans_stmt.emit(any_[0], any_[1]) - - self.emitd("}", 2) - - def emit_initialization(self): - self.emit("#include <stdio.h>") - for state in self.cg.map.keys() + ["accept", "reject"]: - self.emit("__code %s(%s);" % (self.state_name(state), self.interface)) - self.emit('unsigned char* buf;') - self.emit_skip() - - def emit_from_callgraph(self): - # self.emit C-source code - self.emit_initialization() - self.emit_driver() - - for cur_state, transition in self.cg.map.iteritems(): - self.emit_state(cur_state, transition) - - self.emit_accept_state() - self.emit_reject_state() - - class _trans_stmt(CTranslator._trans_stmt): - def __init__(self, emit, args): - CTranslator._trans_stmt.__init__(self, emit) - self.args = args - - def visit_Character(self, char): - self._emit("case %d: /* match %s */" % (char.char, chr(char.char))) - self._emit(" goto %s(%s);" % (self.next, self.args)) - - def visit_EndLine(self, endline): - self._emit(r"case '\0':") - self._emit(" goto %s($s);" % (self.next, self.args)) - - # Special Rule - - def visit_MBCharacter(self, mbchar): - self._emit("/* match %s */" % mbchar) - bytes = mbchar.bytes - self._emit(" if(%s)" % \ - " && ".join(["*(s+%d) == 0x%x" % (d, x) for d, x in enumerate(bytes)])) - self._emit(" s += %d;" % len(bytes)) - self._emit(" goto %s(%s);" % (self.next, self.args), 2) - - def visit_BegLine(self, begline): - self._emit("if (s == buf)") - self._emit(" goto %s(%s);" % (self.next, self.args), 2) - - def visit_Range(self, range): - if isinstance(range.lower, MBCharacter) and not \ - isinstance(range.upper, MBCharacter) or \ - isinstance(range.upper, MBCharacter) and not \ - isinstance(range.lower, MBCharacter): - return - - if isinstance(range.lower, MBCharacter): - self.visit(range) - else: - self._emit("if ('%s' <= *s && *s <= '%s')" % (range.lower.char, range.upper.char)) - self._emit(" s++;") - self._emit(" goto %s(%s);" % (self.next, self.args), 2) - - def visit_AnyChar(self, anychar): - self._emit(r"if (*s != '\0') {") - self._emit(" s = SKIP(s);") - self._emit(" goto %s(%s);" % (self.next, self.args), 2) - self._emit("}") - self._emit("goto reject(%s);" % self.args) - -def test(): - import doctest - doctest.testmod() - -if __name__ == '__main__' : test()