免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3259 | 回复: 3
打印 上一主题 下一主题

一个sed写的图灵机 [复制链接]

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-29 20:42 |只看该作者 |倒序浏览
还在加班,不断修改、编译数字电路
无聊中在这里看一个图灵机
该图灵机用sed编写,源码如下:

  1. #! /bin/sed -f
  2. #
  3. # turing.sed -- emulate a Turing machine
  4. #
  5. # Christophe Blaess <[email]ccb@club-internet.fr[/email]>
  6. # [url]http://perso.club-internet.fr/ccb/[/url]

  7. # See text file for information about Turing Machine script.

  8. # Read all the instructions, and add a final newline.
  9. :read
  10. N
  11. $!b read
  12. G

  13. # Delete comments extending from a '#' to the end of the line.
  14. s/#[^\n]*\n//g
  15. s/#.*$//g

  16. # Use a colon to separate the instructions.
  17. s/\n/:/g

  18. # Is there an initial tape ?
  19. /|/ s/\(.*\)|\([^:]\)\([^:]*\):\(.*\)/|\2|\3:\1\4/

  20. # else insert a blank one.
  21. /|/!s/\(.*\)/| |:\1/

  22. # Reserve the storage place at the beginning of the pattern space,
  23. # then set the current state to zero.
  24. s/\(.*\)/0x\1/

  25. # Start the machine !
  26. :loop
  27.         # Display only the tape and the state.
  28.         h
  29.         # (comment out the next two lines to see internal data when
  30.         #  debuging TM script)
  31.         s/:.*//
  32.         s/^\(.\)./(\1) /
  33.         p
  34.         g

  35.         # Check the content of the current cell.
  36.         /|[^:|]|/!{
  37.                 s/.*/Internal error in the Turing machine/
  38.                 q
  39.         }

  40.         # Store in second position the symbol read on the tape
  41.         s/^\(.\).\(.*\)|\(.\)|\(.*\)/\1\3\2|\3|\4/

  42.         # Have we reached a final state ?
  43.         /^\(.\).*:\1/!{
  44.                 s/\(.\).*/Final state \1 reached... end of processing/
  45.                 q
  46.         }

  47.         # Is there an instruction for this state and this cell content ?
  48.         /^\(..\).*:\1/!{
  49.                 s/^\(.\)\(.\).*/No instruction for state \1 and cell \2/
  50.                 q
  51.         }

  52.         # Look for the new content to write.
  53.         /^\(..\).*:\1[^:|]/!{
  54.                 s/.*/I can't write this symbol on the tape!/
  55.                 q
  56.         }
  57.         s/^\(..\)\(.*\)|.|\(.*\):\1\(.\)/\1\2|\4|\3:\1\4/

  58.         # Look for the direction of movement.
  59.         /^\(..\).*:\1.[ LRlr]/!{
  60.                 s/.*/Movement must be specified as L, R or space/
  61.                 q
  62.         }
  63.         # Clear the substitute flag that we will use later.
  64.         t nop
  65.         :nop
  66.         /^\(..\).*:\1. / {
  67.                 # Direction = ' ' -> Don't move the head
  68.                 b end_move
  69.         }
  70.         /^\(..\).*:\1.[Ll]/ {
  71.                 # Move the head to the left if the tape is long enough,
  72.                 s/^\(..\)\(.*\)\(.\)|\(.\)|/\1\2|\3|\4/
  73.                 t end_move
  74.                 # else extend the tape with an empty cell.
  75.                 s/|\(.\)|/| |\1/
  76.                 b end_move
  77.         }

  78.         # Move the head to the right, if the tape is long enough,
  79.         s/|\(.\)|\([^:]\)/\1|\2|/
  80.         t end_move
  81.         # else extend the tape with an empty cell.
  82.         s/|\(.\)|/\1| |/

  83.         :end_move

  84.         # Check the new state,
  85.         /^\(..\).*:\1..[^:|]/!{
  86.                 s/.*/I can't use this symbol as new state/
  87.                 q
  88.         }
  89.         # then switch the machine state
  90.         s/^\(.\)\(.\)\(.*\):\1\2\(..\)\(.\)/\5\2\3:\1\2\4\5/

  91.         # Garbage collector : cut the blank portions of the tape,
  92.         # on the left,
  93.         s/\(..\) */\1/
  94.         # then on the right.
  95.         s/\(..\)\([^:]\) *:/\1\2:/

  96.         b loop

  97. ###### end of the script
复制代码

[ 本帖最后由 cjaizss 于 2008-5-29 21:39 编辑 ]

评分

参与人数 1可用积分 +3 收起 理由
prolj + 3

查看全部评分

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
2 [报告]
发表于 2008-05-29 21:39 |只看该作者
改一下标题,呵呵

论坛徽章:
0
3 [报告]
发表于 2008-05-30 23:32 |只看该作者
曲高和寡

论坛徽章:
0
4 [报告]
发表于 2011-12-25 00:48 |只看该作者
我使用的sed,一般也不超过3、5行。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP