The two examples you give are regular expressions, and there are indeed easy algorithmic ways of turning regular expressions into automata - namely, NFAs. Once you have NFAs, you can turn them into TMs using a simple construction.
The algorithm to turn regular expressions into NFAs goes like this:
Rule 1: If r = a
for some alphabet symbol a, then the NFA for r
is:
a
--->q0--->(q1)
Rule 2: If r = st
for regular expressions s, t
, and M1
and M2
are NFAs for s
and t
, respectively, then the NFA for r
is:
e
--->M1--->M2
That is, the initial state is that of M1
, the accepting states are those of M2
, and there are new empty transitions from all the (formerly) accepting states of M1
to the (formerly) initial state of M2
.
Rule 3: if r = s + t
, for regular expressions s, t
, and M1
and M2
are NFAs for s
and t
, respectively, then the NFA for r
is:
e
--->q0--->M1
| e
+--->M2
That is, the initial state is a new state q0
, the final states are those of both M1
and M2
, and two empty transitions are added leading from the new initial state to the (formerly) initial states of both M1
and M2
.
Rule 4: If r = s*
for regular expression s
, and M
is a NFA for s
, then a NFA for r
is:
+------+
| e |
V |
--->(q0)--->M
That is, a new initial state q0
is added, the accepting states are those of M
along with q0
, and new empty transitions are added from the accepting states of M
to q0
.
In your examples, we derive NFAs as follows:
a: Rule 1 a
--->q0--->(q1)
b: Rule 1 b
--->q0--->(q1)
a*: Rule 4 +-------------+
| e |
V |
--->(q0)--->q0'--->(q1)
e a
b*: Rule 4 +-------------+
| e |
V |
--->(q0)--->q0'--->(q1)
e b
a*b*: Rule 2 +-----------+
| | e
V e a |
--->q0--->q0'--->q1
| |
e | | e
+-----------+--+
| | e
V e b |
(q2)--->q2'--->(q3)
a*b*: Rule 2 +-----------+
| | e
V e a |
--->q0--->q0'--->q1
| |
e | | e
+-----------+
| | e
V e b |
q2--->q3'--->q3
| |
e | | e
+-----------+--+
| | e
V e a |
(q4)--->q5'--->(q5)
With a NFA in hand, a TM can be constructed as follows:
- Same states as the NFA
- Transitions which consume an input move the tape head right.
- Transitions which are empty do not move the tape head at all.
- The TM never overwrites its input.
- When the TM encounters a blank after moving right far enough, it enters halt-accept if it's in an accepting state for the DFA, or halt-reject otherwise.