How to calculate whether a parenthesis string is valid using a 2-tape Turing machine?
As others have pointed out, you can use a stack to check for matching parentheses. Such manipulation is the base idea of the shunting yard algorithm by Dijkstra.