# How to construct and write down a Turing machine for a given language?

In my class we just started learning about Turing machines. I think I understand the concept but am unsure how to syntactically solve any problem related to one. I am presented with the problem:

Build a Turing machine accepting $(b + c)^+$$\#a^+$ (Please comment your code. Any uncommented solutions will not be graded.)

I am unsure of how to actually begin devising this machine? Could someone please help get me started?

• What sort of format are you expected to produce the machine in? As a state machine? Or a high level description? – Luke Mathieson Apr 25 '13 at 0:02
• if you state what the language consists of in your own words, thats a long ways toward a solution... – vzn Apr 25 '13 at 0:58
• Browse questions tagged turing-machines and give us your best shot! – Raphael Apr 25 '13 at 7:36

What all strategies to devise a program for a Turing machine - or for any other machine, for that matter - boil down to is this: learn how to write programs for easy languages, and then use these programs and rules of composition to figure out more complicated ones. Programming languages - including Turing machine programs - expose an interface to the underlying computer, which allows you to perform some limited number of operations. Programming is the art and science of finding a sequence of such operations to solve your problem.

1. Can you write a Turing machine to recognize $a^+$?
2. Change the Turing machine in (1) to recognize $\#$ only. Hint: it's a different symbol and you only want one of them.
3. Change the Turing machine in (1) to recognize $(b + c)^*$. Hint: you'll now take either of two symbols instead of one symbol.
4. Take the machines from steps (3), (2) and (1). Create a new machine that does what (3) does, but instead of accepting, it does what (2) does, but instead of accepting, it does what (1) does.

In TM pseudocode, what you get is something along the following lines:

1. Move one cell to the right.
2. If the cell is $b$ or $c$, move to the right; else, halt reject.
3. If the cell is $b$ or $c$, move to the right, and repeat this step. Otherwise, if the cell is $\#$, move to the right and go to the next step. Halt reject in any other case.
4. If the cell is $a$, move to the right; else, halt reject.
5. If the cell is $a$, move to the right. Else, if the cell is blank, halt accept. Halt reject in any other case.

Those would serve as decent comments, but coming up with the TM and verifying that it works should also accompany any correct answer.

• I understand your logic, however I am still not sure what it exactly means to "write a Turing machine to recognize $a^+$" Does this mean just write the transition functions? How would that look? – Matt Hintzke Apr 25 '13 at 20:10
• @MattHintzke Take some semi-formal definition of Turing Machine and come up with something that satisfies that definition. For instance, I think TMs can be defined by listing the following things: $Q$, the set of states; $q_0$, the initial state; $\Sigma$, the input alphabet; $\Gamma$, the tape alphabet; and $\delta$, the transition function from configurations to new configurations. Typically, a transition will say (1) What the state is; (2) What the tape symbol is; (3) What symbol to write to the tape cell; (4) What state to move to; (5) How to move the tape head. – Patrick87 Apr 25 '13 at 21:39
• did you take a look at my answer to my own question? I think I got it – Matt Hintzke Apr 25 '13 at 22:06

ok I think I got it. does this look right?

$\delta(q_0,*) = (q_1, *, R)$ where $* = c,b$ // Read at least one c or b

$\delta(q_1,*) = (q_1,*,R)$ where $* = c,b$ // Continue reading c or b while moving right

$\delta(q_1,\#) = (q_2,\#,R)$ // End of block, skip right to next block

$\delta(q_2,a) = (q_3,a,R)$ // Read at least 1 a

$\delta(q_3, a) = (q_3, a,R)$ // Continue reading a's and moving right

$\delta(q_3, B) = (q_4, B, S)$ // When we reach the end of the tape, halt

• @Patrick87 hows this look? – Matt Hintzke Apr 25 '13 at 21:35
• Looks good. Note that the definition I'm familiar with starts with $q_0$ and a blank symbol at the front of the tape; you seem to be assuming the tape head is looking at the first input symbol. That should be fine here. Also, the "halt accept" state is usually denoted $h_a$. The rest looks good to me! – Patrick87 Apr 26 '13 at 13:06

What I believe you're asking is essentially you want to build a finite state machine (FSM). These machines are built up using many states (generally represented by circles). Given some input, they then react to that input and either the current state changes, or it remains the same.

Here's a nice example taken from wikipedia:

This shows that we begin first at state $S_1$, represented by the arrow pointing to it. From $S_1$, there are two inputs that could be received: a 0, or a 1 (our input language would then be $\{0,1\}^*$). If $S_1$ reads a 1, then it remains there. If it receives a 0, then it moves to state $S_2$.

What this machine does is checks for odd or even amounts of 0's in the given string, ie $L = \{w\ | w\ \in \{0,1\}^*,\ w\ has\ an\ even\ amount\ of\ 0's\}$.

So some strategies on making yours; First, build up your turing machine so that it handles the base case, which for you could be simply (b + c)#a. Then, write out some other cases that could happen, such as (b + c)(b + c)#a, and see how you could handle that, until you match the language you are creating. Try splitting the language into sections, and create a machine for those, ie one section is (a + b)$^+$, while another would be #.

In the case of regular expressions, there are algorithms to convert them to finite automata systematically. You can check a textbook on automata or, for example, this link. Note that in simple cases like yours, it's probably easier to write down an automaton directly.

• i already know what a FSM is. I have been doing them all semester in class. We learned context-free grammar as well and am now on Turing Machines – Matt Hintzke Apr 25 '13 at 19:56