作为python生成器的turing机器。
turing_machine的Python项目详细描述
这是一个图灵机器的模拟器,有一个单独的无限长的磁带。这个 模拟器允许一个人一步一步地执行一台机器,检查一台机器 接受或拒绝特定输入并查看其执行情况。
内容
Installation
有几种方法可以得到模拟器。
pypi:如果要将包用作 应用程序:
pip install turing_machine
git:如果要获取所有文件,最重要的是the notebook:
git clone https://github.com/dimazest/turing_machine.git
github:如果您不想使用git:
wget https://github.com/dimazest/turing_machine/archive/master.zip
Using the simulator in the IPython notebook
The notebook(Turing machine.ipynb)是运行机器的好方法 互动的。您需要安装新版本的ipython(>;=3.0)。 看看these IPython installation instructions using miniconda如果你 还没有安装。
Using the simulator inside of a Python script
如果你不想打扰ipython,你可以在 您自己的脚本,请参见w_hash_w.py,例如:
python w_hash_w.py q0 [1]0#10 FindDelimiter1 X[0]#10 FindDelimiter1 X0[#]10 ... MANY OTHER CONFIGURATIONS ... qa XX#XX[]
The API
包提供了`TuringMachine类,该类用 特殊过渡。一旦一个图灵机被实例化,它可以 执行。下面是一个接受语言的机器的例子(两个 用#分隔的相同单词。
>>> from turing_machine import TuringMachine
>>> w_hash_w = TuringMachine( ... { ... ('q0', '#'): ('End', '#', 'R'), ... ('End', ''): ('qa', '', 'R'), ... ... ('q0', '0'): ('FindDelimiter0', 'X', 'R'), ... ('FindDelimiter0', '#'): ('Check0', '#', 'R'), ... ('Check0', '0'): ('FindLeftmost', 'X', 'L'), ... ... ('q0', '1'): ('FindDelimiter1', 'X', 'R'), ... ('FindDelimiter1', '#'): ('Check1', '#', 'R'), ... ('Check1', '1'): ('FindLeftmost', 'X', 'L'), ... ... ('FindLeftmost', '0'): ('FindLeftmost', '0', 'L'), ... ('FindLeftmost', '1'): ('FindLeftmost', '1', 'L'), ... ('FindLeftmost', 'X'): ('FindLeftmost', 'X', 'L'), ... ('FindLeftmost', '#'): ('FindLeftmost', '#', 'L'), ... ('FindLeftmost', ''): ('FindNext', '', 'R'), ... ... ('FindNext', 'X'): ('FindNext', 'X', 'R'), ... ('FindNext', '0'): ('FindDelimiter0', 'X', 'R'), ... ('FindNext', '1'): ('FindDelimiter1', 'X', 'R'), ... ('FindNext', '#'): ('End', '#', 'R'), ... ... ('FindDelimiter0', '0'): ('FindDelimiter0', '0', 'R'), ... ('FindDelimiter0', '1'): ('FindDelimiter0', '1', 'R'), ... ('FindDelimiter1', '0'): ('FindDelimiter1', '0', 'R'), ... ('FindDelimiter1', '1'): ('FindDelimiter1', '1', 'R'), ... ... ('Check0', 'X'): ('Check0', 'X', 'R'), ... ('Check1', 'X'): ('Check1', 'X', 'R'), ... ... ('End', 'X'): ('End', 'X', 'R'), ... } ... )
Input acceptance
一旦我们有了机器,我们就可以检查它是否接受特定的字符串:
>>> w_hash_w.accepts('#') True >>> w_hash_w.accepts('1#1') True >>> w_hash_w.accepts('1#10') False
或拒绝:
>>> w_hash_w.rejects('##') True >>> w_hash_w.rejects('#') False
Step by step execution
.run()方法返回一个执行机器并生成 配置和验收决定:
>>> execution = w_hash_w.run('1#1') >>> action, context = next(execution) >>> context['state'] 'q0'
Infinite execution
因为执行是在生成器中完成的,所以可能有 执行,但验收检查受步骤数的限制 允许执行。
>>> go_right = TuringMachine( ... { ... ('q0', ''): ('q0', '', 'R'), ... } ... )
如果达到步进限制,则返回None:
>>> go_right.accepts('') is None True
执行2000步:
>>> go_right.accepts('', step_limit=2000) is None True
Debugging
另一个很好的特性是通过观察 配置。
>>> w_hash_w.debug('1#1') q0 [1]#1 FindDelimiter1 X[#]1 Check1 X#[1] FindLeftmost X[#]X FindLeftmost [X]#X FindLeftmost []X#X FindNext [X]#X FindNext X[#]X End X#[X] End X#X[] qa X#X[]