免责声明:您的问题是非常普遍的,因此这个答案也是如此。请注意,我什么都不是TM的专家,而且这种方法通常效率不高(我甚至不能保证它总是有效)。 我只是在这里记下一些想法。
我会建议尝试这样的方法:拿你的伪代码,并减少它,使它只包含一个)布尔变量和b)if
-陈述。 例如:
if THIS_LETTER_IS("A"):
found_an_even_number_of_A = not found_an_even_number_of_A
if THIS_LETTER_IS("Q") and previous_letter_was_X and found_an_even_number_of_A
and not checking_for_alternative_2:
# can't be a word of alternative 1, so check for alternative 2
going_back_to_start_to_check_for_alternative_2 = True
if going_back_to_start_to_check_for_alternative_2:
MOVE_TO_PREVIOUS
else:
MOVE_TO_NEXT
if going_back_to_start_to_check_for_alternative_2 and THIS_LETTER_IS(EMPTY):
# reached the beginning, so let's check for alternative 2
going_back_to_start_to_check_for_alternative_2 = False
checking_for_alternative_2 = True
当你嵌套if
年代,随着and
小号取代它们;通过使用not
除去else
块:
if something:
if something_else:
do_a
else:
do_b
变得
if something and something_else:
do_a
if something and not something_else:
do_b
然后每个if
块应该只包含一个MOVE_TO_PREVIOUS
或MOVE_TO_NEXT
,可能是WRITE
命令和变量赋值的任何数目 。
完成所有if
条款,使得每一个你的布尔值和当前信的一个始终检查,重复 块,其中neccessary。例如:
if something and something_else:
do_a
成为
if THIS_LETTER_IS("A") and something and something_else and something_i_dont_care_about_here:
do_a
if THIS_LETTER_IS("A") and something and something_else and not something_i_dont_care_about_here:
do_a
if THIS_LETTER_IS("Q") and something and something_else and something_i_dont_care_about_here:
do_a
if THIS_LETTER_IS("Q") and something and something_else and not something_i_dont_care_about_here:
do_a
现在,如果你有ñ布尔和米信,你应该有米 * 2 ňif
秒。 想象一下,您已将布尔值存储在一个位域中,因此每个可能的布尔值组合代表一个整数 。因此上述变
if THIS_LETTER_IS("A") and bitfield[0] and bitfield[1] and bitfield[2]:
do_a
if THIS_LETTER_IS("A") and bitfield[0] and bitfield[1] and not bitfield[2]:
do_a
# ...
然后成为
if THIS_LETTER_IS("A") and bitfield == 7:
do_a
if THIS_LETTER_IS("A") and bitfield == 3:
do_a
# ...
这对位域的整数值是图灵机状态。 do_a
部分只是一个布尔值赋值(即位域,所以它是你的新状态),一个写命令(如果没有的话,只写一个已经是 的字母)和一个移动命令,因此显式地是一个图灵机过渡。
我希望以上任何一条都有道理。
你是什么意思“构建语言的图灵机”? – balpha 2010-01-20 07:28:09
你需要将手放在无限长的磁带上。 – 2010-01-20 07:32:01
@balpha:通常我会遇到像“提出决定语言{w属于{a,b} *:w至少包含一个a}的语言的图灵机”。只是我书中的一个随机例子。我重新提出了我的问题,使其更清楚。 – 2010-01-20 07:33:19