Professional Documents
Culture Documents
Determining Well Parenthesis
Determining Well Parenthesis
Determining Well Parenthesis
Well-Formed Expressions
(xxx[])
()[]{}
{(xxxxx)()}
Ill-Formed Expressions
(xxx[)
((
{xxxxx)()}
nested components (such as for loops, if-then statements, and while loops). The
compiler checks for matching {} pairs to denote blocks of code. If the pairs do not
match, a compiler error will occur.
mathmatical expressions. For example a = b - {(x + y) * z + 3};
array notation [ ].
function calls and function definitions. Specifically, the parameters must be
surrounded by balanced "(" ")".
If the character is an opening symbol (characters "(", "{", or "["), then push it
onto the stack
Else if the character is a closing symbol (characters ")", "}", or "]"), then:
o Check if the stack is empty. If it is, then there is no matching opening
symbol. The expression is not well formed.
o Else,
get the character at the top of the stack
pop the stack
compare the current closing symbol to the top symbol. If they
balance each other, then the expression is still balanced and
continue looping (reading the next character)
If the end of the expression is reached, and it is still balanced, then print
"expression is well formed"
Otherwise, print "expression is not well formed"