ã¯ããã«
ãããã«ã¡ã¯ãhirataraã§ãã
ãååã¯ãæ£è¦è¡¨ç¾æååãã³ã³ãã¤ã«ããæ§ææ¨ãä½æããé¨åã¾ã§ãå®è£ ãã¾ãããä»åã¯åæ£è¦è¡¨ç¾ã®æ¼ç®ãã©ã®ããã«NFAã§è¡¨ç¾ããããã解説ãããã®æ§ææ¨ãInterpreterãã¿ã¼ã³ã«å¯¾å¿ããã¾ãã
対象èªè
- æ£è¦è¡¨ç¾ããã£ã¨ç¥ãããæ¹
- æ å ±ç§å¦åéã«èå³ãããæ¹
- æ£è¦è¡¨ç¾ã¨ã³ã¸ã³ãå®è£ ããå¿ è¦ãããæ¹
NFAå¤æã®ããã®éå ·ä½ã
ãååã®è¨äºã«ãããå ¥åæååããæ§æ解æãçµããæ§ææ¨ãä½ããã¨ãã§ãã¾ãããä»æã«ãã¦ããæ§ææ¨ã¯ãä¾ãã°ä»¥ä¸ã®ãããªç©ã§ãã
Union( Character('a'), Star( Concat( Character('b'), Character('c') ) ) )
ã次ã®ã¹ãããã§ã¯ããã®æ§ææ¨ãInterpreterãã¿ã¼ã³ã«å¯¾å¿ãããããåãã¼ãã«assemble
ã¡ã½ãããå®è£
ãã¾ããããªã¼ãæ½ããªãããã®ã¡ã½ãããå帰çã«å¼ã¶ãã¨ã§ãNFAã¸ã®å¤æãå®ç¾ãã¾ãã
ãå®è£
ã«å
¥ãåã«ãã¾ãã¯çµã¿ç«ã¦ã«å¿
è¦ãªéå
·ãæãã¾ããããåãã¼ãããå
±éãã¦ä½¿ãããã¯ã©ã¹ã§ãããContext
ã¯ã©ã¹ã¨NFAFragment
ã¯ã©ã¹ãå°å
¥ãã¾ãã
Contextã¯ã©ã¹
ãContext
ã¯ã©ã¹ã¯ãNFAã®å¤ææã«å
±æããæ
å ±ã®ã³ã³ããã§ããNFAãä½æããã«ã¯ãç¶æ
ã表ãæ´æ°å¤ãä¸æã«çæããå¿
è¦ãããã¾ããããã§ãContext
ãªãã¸ã§ã¯ãå
ã®_state_count
ãã£ã¼ã«ãã«æ´æ°å¤ãä¿æããgenerate_state
ã¡ã½ããã§ã«ã¦ã³ãã¢ããããªããä¸æã®å¤ãçæãã¾ãã
class Context(object): def __init__(self): self._state_count = 0 def new_state(self): self._state_count += 1 return self._state_count
NFAFragmentã¯ã©ã¹
ãNFAFragmentã¯ãNFAã®æ§æé¨åã¨ãªãã¯ã©ã¹ã§ããNFAã®ä¸é¨åã表ç¾ãã¦ãããèªç±ã«çµã¿åããã¦ç¹ããããé¨åã§ããã¾ããbuild
ã¡ã½ãããå¼ã¶ãã¨ã§ã第ï¼åã§å®è£
ããNondeterministicFiniteAutomaton
ãªãã¸ã§ã¯ãã«å¤æãããã¨ãå¯è½ã§ãã
ã以å¾ã®èª¬æã§ã¯ã以ä¸ã®å³ãNFAFragmentã¤ã³ã¹ã¿ã³ã¹ã®ã¤ã¡ã¼ã¸ã¨ãã¦å©ç¨ãã¾ãã
ãåè§ã§å²ã¾ãã¦ããé¨åãNFAFragmentã表ãã¾ããå¤ããç¢å°ãåãã¦ãã丸ãåæç¶æ ã§ãäºé丸ã§ç¤ºããã¦ããã®ãåçç¶æ ã§ããåæç¶æ ã¯ä¸ã¤ã§ãããåçç¶æ ã¯è¤æ°åå¨ãããã®ã§ãå³ä¸ã§ã¯2ã¤æ¸ãã¦ãã¾ãããã®ä»ã®ç¶æ ã®ä¸¸ãé·ç§»ã®ç¢å°ã¯ããã®è¡¨è¨ã«ããã¦ã¯çç¥ãã¦ãã¾ãã
ã³ã³ã¹ãã©ã¯ã¿ã¨ãã£ã¼ã«ãå®ç¾©
ã以ä¸ãNFAFragment
ãæã¤ãã£ã¼ã«ãã§ãã
class NFAFragment(object): def __init__(self): self.start = None # æ´æ°å self.accepts = None # frozensetå self.map = dict()
ãNondeterministicFiniteAutomaton
ã¯ã©ã¹ã¨åæ§ã«ãåæç¶æ
ã表ãstart
ã¨åçç¶æ
ã®éåã表ãaccepts
ããã£ã¼ã«ãã¨ãã¦æã£ã¦ãã¾ããNFAã表ãã«ã¯ãã¨ããä¸ã¤ãé·ç§»é¢æ°ãå¿
è¦ã§ãããNFABuilderã¯NFAã®çµã¿ç«ã¦ã«ä½¿ãé¨åãªã®ã§ãèªç±ã«å¤æ´ããããããã¨ãæ±ãããã¾ããããã§ãå¤æ´ããã«ããfunctionåã¨ãã¦ã§ã¯ãªãã代ããã«å¤æ´ã容æãªdictåãå©ç¨ãã¦é·ç§»é¢æ°ã表ãã¾ããself.map
ãããã®é·ç§»é¢æ°ã«è©²å½ãã¾ãã
ãself.map
ã¯ã(ç¶æ
, å
¥åæå)
ã®ã¿ãã«ããã¼ã¨ãã次ã«é·ç§»ããç¶æ
ã®éåãå¤ã¨ãã¦æã¡ã¾ããããã¯ã¡ããã©NFAã®é·ç§»é¢æ°ã®å¼æ°ã¨æ»ãå¤ã¨ä¸è´ããå®ç¾©ã«ãªã£ã¦ã¾ãã®ã§ãself.map
ãnfa.transition
ã®é¢æ°ã®å½¢ã«å¤æ´ããã®ã¯ç°¡åã§ãã
ãã©ã°ã¡ã³ãã®æ¼ç®
ã次ã«ããã©ã°ã¡ã³ãã®çµã¿ç«ã¦ã«å¿ è¦ãªã¡ã½ãããä½ãã¾ãã
ãã¾ãã¯ãconnect
ã¡ã½ããã§ãããã®ã¡ã½ããã¯ã2ã¤ã®ç¶æ
from_
ã¨to
ãåãåããæåchar
ã«ãã£ã¦ãã®2ã¤ã®ç¶æ
ãæ¥ç¶ãã¾ãããã£ã¨å
·ä½çã«è¨ãã¨ããã©ã°ã¡ã³ãã®é·ç§»é¢æ°ã«ãfrom_
ããchar
ã«ãã£ã¦to
ã¸é·ç§»ããé·ç§»ãä»ãå ãããã¨è¨ããã¨ã§ãã
ãé·ç§»é¢æ°ã¯ dictåã®map
ãã£ã¼ã«ãã«ãã£ã¦è¡¨ç¾ããã¦ãã¾ãã®ã§ãããã«è©²å½ããé·ç§»ã表ãã¨ã³ããªã¼ã追å ãããã¨ã§é·ç§»ç¢å°ã®è¿½å ãå®ç¾ã§ãã¾ãã
def connect(self, from_, char, to): slot = self.map.setdefault( (from_, char), set() ) slot.add(to)
ã次ã«ãnew_skelton
ã¡ã½ããã§ããããã®ã¡ã½ããã¯ãã©ã°ã¡ã³ãã®æ°ãã骨çµã¿ãä½ãã®ã«ä½¿ãã¾ãããã®ã¡ã½ãããå¼ã¶ã¨ãç¾å¨ã®ãã©ã°ã¡ã³ãã¨åãç¶æ
ã¨é·ç§»é¢æ°ãæã¤ãã©ã°ã¡ã³ããçæããã¾ãããã ããã®ã¡ã½ããã§ã¯ãåæç¶æ
ï¼start
ï¼ã¨åçç¶æ
ï¼accepts
ï¼ã¯ã³ãã¼ãã¾ããã®ã§ãèªåã§è¨å®ããå¿
è¦ãããã¾ãã
def new_skelton(self): # ã³ãã¼ãã¦è¿ã new_frag = NFAFragment() new_frag.map = deepcopy(self.map) return new_frag
ãæå¾ã«ã ç¹æ®ã¡ã½ããã§ãã__or__
ãå®ç¾©ãã¦ãã¾ãããã®å®ç¾©ã«ããã2ã¤ã®ãã©ã°ã¡ã³ãfragment1
ã¨fragment2
ãã | æ¼ç®åã« ãã£ã¦fragment1 | fragment2
ã®ããã«åæãããã¨ãã§ãã¾ããåæããã¦ã§ããæ°ããNFAFragment
ã¯ãfragment1
ã¨fragment2
ã®é·ç§»é¢æ°ãä½µããé·ç§»é¢æ°ãæã¡ã¾ãããã ãããã®æ¼ç®ãå
ã»ã©ã®new_fragment
ã¡ã½ããã¨åæ§ã«ãåæç¶æ
ã¨åçç¶æ
ã¯ã³ãã¼ãã¾ããã
def __or__(self, frag): new_frag = self.new_skelton() for k, v in frag.map.iteritems(): new_frag.map[k] = v.copy() return new_frag
ããã®æ¼ç®ãé·ç§»å³ã§èããã¨ãç¢å°ãä¿æããã¾ã¾2ã¤ã®ãã©ã°ã¡ã³ãã1ã¤ã®é·ç§»å³ä¸ã«ç§»ãã¨è¨ããã¨ã表ãã¾ãããã ããã®æç¹ã§ã¯ã¾ã ã2ã¤ã®ãã©ã°ã¡ã³ããçµã¶ç¢å°ã¯åå¨ãã¦ãã¾ããã | æ¼ç®åã§åæããå¾ãããããã®ãã©ã°ã¡ã³ãéãã¾ããï¼ã¤ã®ç¶æ
ã«å¯¾ãã¦connect
ã¡ã½ãããå¼ã¶ãã¨ã§ã2ã¤ã®ãã©ã°ã¡ã³ããåä½ããããã¨ãã§ãã¾ãã
ãã©ã°ã¡ã³ãããNFAãä½æ
ãæå¾ã«ãçµã¿ç«ã¦çµãã£ã¦ããNFAãä½æããã¡ã½ããã§ããbuild
ã¡ã½ãã ã®å®è£
ã§ãã
def build(self): map_ = self.map def transition(state, char): return frozenset(map_.get( (state, char), [])) return nfa.NondeterministicFiniteAutomaton( transition, self.start, self.accepts )
ãNFAFragment
ãªãã¸ã§ã¯ãã¯ãNondeterministicFiniteAutomaton
ãªãã¸ã§ã¯ãã¨åæ§ã«start
ãaccepts
ãæã£ã¦ãã¾ãã®ã§ãããã¯ãã®ã¾ã¾NondeterministicFiniteAutomaton
ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¸æ¸¡ãã¾ããé·ç§»é¢æ°ã¯ãdictåããfunctionåã«å¤æããå¿
è¦ãããã¾ããããã¯ãself.map
ãã£ã¼ã«ããå
é¨ã«ä¿æããtransition
ã¨è¨ãååã®ã¯ãã¼ã¸ã£ã¨ãã¦å®è£
ãã¦ãã¾ããä¸ãè¦ã¦ãããããã«ãå¼æ°ããã®ã¾ã¾map
ãã£ã¼ã«ãã®ãã¼ã¨ãã¦ä½¿ããmap
ãã£ã¼ã«ãã®ããªã¥ã¼å¤ãæ»ãå¤ã¨ãã¦è¿å´ããã ãã®åç´ãªé¢æ°ã§ãã