免責聲明:您的問題是非常普遍的,因此這個答案也是如此。請注意,我什麼都不是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