check whether an expression is balanced