简体   繁体   中英

Turing Machine For balanced parenthesis

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

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:

  1. read the input left to right
  2. 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
  3. halt-reject if the difference ever dips below zero
  4. halt-reject if you get to the end of the input and the difference not zero
  5. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM