# Help with postfix expression

• 11-12-2010, 09:10 AM
javajavajava
Help with postfix expression
I have a question about postfix expressions. Is there a way to tell if the expression is a valid postfix expression without actually evaluating it? For example, the expression 12+345+** is a valid postfix expression but 12+345+*** is not. If someone could help shed some light i'd appreciate it!
• 11-12-2010, 09:41 AM
JosAH
Quote:

Originally Posted by javajavajava
I have a question about postfix expressions. Is there a way to tell if the expression is a valid postfix expression without actually evaluating it? For example, the expression 12+345+** is a valid postfix expression but 12+345+*** is not. If someone could help shed some light i'd appreciate it!

Scan the expression (string) from left to right and keep track of a counter. If you have scanned an operand add one to the counter; if you have scanned an operator subtract the arity of the operator from the counter and add one to it again. If the counter reaches a value below one the expression is invalid. If the counter isn't one after you have scanned the entire expression, it is also invalid.

kind regards,

Jos
• 11-12-2010, 11:47 AM
Eranga
Basically it's the combination evaluation of operands and operators.

Scan the expression from left to right, each character at a time. If the scanned character is operand then move to the next. While the next is an operator proceed. Say you've comes with an operator, then there should be atleast two operands of the left. This is the continuous pattern to check.
• 11-12-2010, 12:11 PM
JosAH
Quote:

Originally Posted by Eranga
Say you've comes with an operator, then there should be atleast two operands of the left.

Only if the operator is a binary operator; that's why I wrote that the count shoud be at least as large as the arity of the operator (see my previous reply).

kind regards,

Jos