Checking and evaluating expressions
sharkquik (0)


Anyone knows how to check for unclosed parentheses in an expression? Basically I'm writing a program to check the expressions and provide helpful error message.

You are viewing a single comment. View All
eankeen (605)


as a simple solution, you can have a variable counter the starts at 0. For every open parenthesis it finds, it increases counter by 1. For every closed parenthesis it finds, it decreases counter by 1. If counter is positive, then there are more open parenthesis than closed parenthesis. If counter is negative, then there are more closed parenthesis than open parenthesis.

if you are looking for a more complete solution, i'd recommend looking into abstract syntax trees (AST). they might seem complicated, but you can definitely find some very simple example and implementations online that you can model.

hope i helped :)