Checking and evaluating expressions
sharkquik (0)

Hi,

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
mwilki7 (184)

You could try implementing a symbol stack.
Every time you see an opening symbol ( [ {
put it on the stack

Every time you see a closing symbol } ] )
remove the top element of the stack

If the closing symbol does not match with what's on top of the stack, then there is an error.
For instance, ( on top of the stack and you see a ] there is a problem because ] should have been a )