How to desgin a turing machine that can recognize the strings of balanced parenthesis? For example (())().

- 21
- 1
- 2
-
Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Also check the [help/on-topic] to see what questions you can ask. – Progman Jan 26 '20 at 19:38
-
You don't even need a Turing machine; a pushdown automaton would be sufficient. – chepner Jan 26 '20 at 19:50
1 Answers
To recognize the strings of balanced parentheses, we just have to make sure there is no prefix which has more closing parentheses than opening parentheses and that the string ends after seeing an equal number of opening and closing parentheses. Clearly, both of these conditions must hold of valid strings; these are necessary requirements. I will leave showing these conditions are sufficient as an exercise or, at any rate, suggest you ask a separate question on that.
So, all we need to do is the following:
- read the input left to right
- on an auxiliary tape (if multitape) or to the right of the input (if single-tape), keep track of the current difference (#open - #closed) seen so far
- halt-reject if the difference ever dips below zero
- halt-reject if you get to the end of the input and the difference not zero
- halt-accept if you get to the end of the input and the difference equals zero
Assuming a single-tape TM, you can read a symbol, then write over it with some marker, then move to the end of the tape in a state depending on whether you saw an opening or closing parenthesis; then, either add a new symbol to the end (if opening) or delete one off the end (if closing); then, enter a new state to go back to the first unmarked symbol of input, and repeat.

- 27,682
- 3
- 38
- 73