Changeset 21058
- Timestamp:
- 10/09/08 21:37:53 (5 years ago)
- Location:
- lang/python/dfareg/branches/codezine200809
- Files:
-
- 9 modified
-
dfareg/__init__.py (modified) (2 diffs)
-
dfareg/dfa.py (modified) (3 diffs)
-
dfareg/lexer.py (modified) (2 diffs)
-
dfareg/nfa.py (modified) (2 diffs)
-
dfareg/nfa2dfa.py (modified) (3 diffs)
-
dfareg/nfabuilder.py (modified) (3 diffs)
-
dfareg/parser.py (modified) (2 diffs)
-
dfareg/test/dfaregtest.py (modified) (2 diffs)
-
setup.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/python/dfareg/branches/codezine200809/dfareg/__init__.py
r20647 r21058 1 #!/usr/bin/env python2.52 1 # -*- coding: utf-8 -*- 3 from dfa import *4 2 from nfa2dfa import nfa2dfa 5 from parser import Parser 6 from lexer import Lexer 7 8 def strict_match_nfa(regexp, string): 9 nfa = lexer.compile_to_nfa(regexp) 10 return nfa.get_runtime().does_accept(string) 11 12 def strict_match_dfa(regexp, string): 13 nfa = lexer.compile_to_nfa(regexp) 14 dfa = nfa2dfa(nfa) 15 return dfa.get_runtime().does_accept(string) 16 17 strict_match = strict_match_dfa 18 19 def print_nfa(regexp): 20 nfa = lexer.compile_to_nfa(regexp) 21 print "[START]" 22 print nfa.start 23 print "[STATES]" 24 for s in nfa.states: 25 print s 26 print "[ACCEPTS]" 27 for s in nfa.accepts: 28 print s 29 print "[TRANSITION]" 30 print "not printable" 31 3 from parser import Parser 4 from lexer import Lexer 32 5 33 6 class Regexp(object): … … 48 21 def compile(regexp): 49 22 return Regexp(regexp) 50 51 52 # for k, v in nfafrag.map.iteritems():53 # print k, v54 55 # cd /Users/tarara/Documents/技術関係/開発合宿/20080524/python56 57 # >>> import dfareg58 # >>> dfareg.strict_match("(p(erl|ython|hp)|ruby)", "python")59 # True60 61 # >>> dfareg.print_nfa("A*B")62 63 # >>> import re64 # >>> re.match("^x*x*x*x*x*x*x*x*x*x*x*x*x*x*xxxxxxxxxxxxxx$", "xxxxxxxxxxxxxx")65 # <_sre.SRE_Match object at 0x2581a8>66 # >>> dfareg.strict_match("x*x*x*x*x*x*x*x*x*x*x*x*x*x*xxxxxxxxxxxxxx", "xxxxxxxxxxxxxx")67 # True -
lang/python/dfareg/branches/codezine200809/dfareg/dfa.py
r19696 r21058 1 #!/usr/bin/env python2.52 1 # -*- coding: utf-8 -*- 3 """4 An joke deterministic finite automaton implementation.5 6 """7 2 8 3 class DFARuntime(object): … … 18 13 19 14 def does_accept(self, input): 20 """21 Check whether an input is accepted by this automaton.22 """23 15 for alphabet in input: 24 16 self.do_transition(alphabet) 25 17 return self.is_accept_state() 26 18 19 27 20 class DeterministicFiniteAutomaton(object): 28 """29 An deterministic finite automaton implementation.30 It's just for fun. So, you shouldn't use this in your products.31 """32 21 def __init__(self, 33 transition , # a transition function34 start , # a start state35 accepts , # a set of accept states22 transition , # 遷移関数 23 start , # 開始状態 24 accepts , # 受理状態の集合 36 25 ): 37 """38 Instanciate new deterministic finite automaton39 from 5-tuple.40 """41 26 self.transition = transition 42 27 self.start = start … … 45 30 def get_runtime(self): 46 31 return DFARuntime(self) 47 48 if __name__ == '__main__':49 pass50 51 52 -
lang/python/dfareg/branches/codezine200809/dfareg/lexer.py
r20730 r21058 1 #!/usr/bin/env python2.52 1 # -*- coding: utf-8 -*- 3 4 5 2 6 3 class Talken(object): … … 48 45 # 通常の文字 49 46 return Talken(ch, Talken.CHARACTER) 50 51 if __name__ == '__main__':52 lexer = Lexer(u"あ(い\|う|え*(かき|くけこ))*お")53 while True:54 talken = lexer.scan()55 if not talken: break56 print talken -
lang/python/dfareg/branches/codezine200809/dfareg/nfa.py
r20841 r21058 1 #!/usr/bin/env python2.52 1 # -*- coding: utf-8 -*- 3 2 4 3 class NondeterministicFiniteAutomaton(object): 5 """6 An nondeterministic finite automaton implementation.7 It's just for fun. So, you shouldn't use this in your products.8 """9 4 def __init__(self, 10 5 transition , # a transition function … … 12 7 accepts , # a set of accept states 13 8 ): 14 """15 Instanciate new deterministic finite automaton16 from 5-tuple.17 """18 9 self.transition = transition 19 10 self.start = start -
lang/python/dfareg/branches/codezine200809/dfareg/nfa2dfa.py
r20841 r21058 1 #!/usr/bin/env python2.52 1 # -*- coding: utf-8 -*- 3 4 2 from dfa import DeterministicFiniteAutomaton 5 3 6 4 class SubsetsIncludingElem(object): 7 """8 the set of subsets of "super_" including an element of "sub".9 10 >>> from dfareg import algebra11 >>> s = algebra.subsets_including_elem(set([1,2,3]), set([1,3]))12 >>> for i in s:13 ... print i14 ...15 frozenset([3])16 frozenset([1, 2])17 frozenset([2, 3])18 frozenset([1])19 frozenset([1, 3])20 frozenset([1, 2, 3])21 >>> set([1,2]) in s22 True23 >>> set([2]) in s24 False25 """26 5 def __init__(self, sub): 27 6 self.sub = sub … … 29 8 return a_set & self.sub 30 9 10 31 11 def nfa2dfa(nfa): 32 33 12 def transition(set_, alpha): 34 13 ret = set() … … 42 21 SubsetsIncludingElem(nfa.accepts) 43 22 ) 44 45 if __name__ == '__main__':46 regexp = u"あ(い\|う|え*(かき|くけこ))*お"47 regexp = u"A*(B|C)*"48 nfastate = lexer.compile_to_nfa(regexp)49 for s in nfastate.all_states():50 print s51 52 nfa2dfa(nfastate) -
lang/python/dfareg/branches/codezine200809/dfareg/nfabuilder.py
r21057 r21058 1 #!/usr/bin/env python2.52 1 # -*- coding: utf-8 -*- 3 2 import nfa … … 5 4 6 5 class NFAFragment(object): 7 """8 >>> b = nfa.NFABuilder("abcde")9 >>> b.start10 111 >>> s1 = b.new_state()12 >>> s2 = b.new_state()13 >>> b.connect(b.start, None, s2)14 >>> b.connect(b.start, "b", s1)15 >>> b.accepting_state(b.start)16 >>> b.accepting_state(s2)17 >>> am = b.build()18 >>> am.start19 120 >>> am.accepts21 frozenset([1, 3])22 >>> am.states23 frozenset([1, 2, 3])24 >>> am.transition(1, None)25 frozenset([3])26 >>> am.transition(3, None)27 frozenset([])28 >>> am.transition(1, "b")29 frozenset([2])30 >>>31 """32 6 def __init__(self): 33 7 self.start = None # 整数型 … … 144 118 145 119 return frag 146 147 148 149 150 if __name__ == '__main__':151 b = NFABuilder()152 f = b.new_fragment()153 print f.start154 s1 = f.new_state()155 s2 = f.new_state()156 f.connect(f.start, "", s2)157 f.connect(f.start, "b", s1)158 f.accepts.add(f.start)159 f.accepts.add(s2)160 am = b.build(f)161 print am.start162 print am.accepts163 # print am.states164 print am.transition(1, "")165 print am.transition(3, "")166 print am.transition(1, "b") -
lang/python/dfareg/branches/codezine200809/dfareg/parser.py
r20822 r21058 1 #!/usr/bin/env python2.52 1 # -*- coding: utf-8 -*- 3 2 from lexer import Talken … … 80 79 self.match(Talken.CHARACTER); 81 80 return node 82 83 84 if __name__ == '__main__':85 from nfabuilder import NFABuilder86 lexer_ = lexer.Lexer(u"あ(い\|う|え*(かき|くけこ))*お")87 parser = Parser(lexer_, NFABuilder() )88 parser.expression() -
lang/python/dfareg/branches/codezine200809/dfareg/test/dfaregtest.py
r21057 r21058 2 2 # -*- coding: utf-8 -*- 3 3 import dfareg 4 from dfareg.dfa import DeterministicFiniteAutomaton 4 5 import unittest 5 6 … … 11 12 if state == 1 and char == u"う": return 3 12 13 return 4 13 self.dfa = dfareg.DeterministicFiniteAutomaton(14 self.dfa = DeterministicFiniteAutomaton( 14 15 transition, 15 16 0, -
lang/python/dfareg/branches/codezine200809/setup.py
r21057 r21058 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 1 4 from setuptools import setup, find_packages 2 5 setup(
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)