view pyrect/regexp/dfa.py @ 43:83c69d42faa8

replace converting-flow, module dfareg with module regexp. it's is substantial changing in implimentation.
author Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
date Tue, 03 Aug 2010 05:35:38 +0900
parents
children 20a1c25d1fc9
line wrap: on
line source

#!/usr/bin/env python

import curses.ascii as ascii
import copy
from fa import FA

class DFA(FA):
    def __init__(self, start, accepts, map_, states):
        def transition(state, input):
            return map_[state, input]
        FA.__init__(self, transition, start, accepts, map_, states)

    def get_runtime(self):
        return DFARuntime(self)

class DFARuntime(object):
    def __init__(self, DFA):
        self.DFA = DFA
        self.curState = self.DFA.start

    def do_transition(self, char):
        self.curState = self.DFA.transition(
            self.curState, char)

    def is_accept_state(self):
        return self.curState in self.DFA.accepts

    def accept(self, string):
        try:
            for alphabet in string:
                self.do_transition(alphabet)
        except KeyError:
            return False
        return self.is_accept_state()