Results 1 to 4 of 4
  1. #1
    javajavajava is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default 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!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by javajavajava View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Eranga View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Infix to Postfix using array
    By Franneldort in forum New To Java
    Replies: 0
    Last Post: 10-11-2010, 06:57 PM
  2. Postfix this!!
    By hasysf in forum New To Java
    Replies: 4
    Last Post: 09-07-2009, 07:44 PM
  3. Postfix into prefix and vice versa
    By sfe23 in forum New To Java
    Replies: 9
    Last Post: 02-19-2009, 11:37 PM
  4. Postfix Evaluation using a Stack, Help?
    By dalangley in forum New To Java
    Replies: 2
    Last Post: 02-16-2009, 11:43 PM
  5. Postfix-Notation
    By little_polarbear in forum New To Java
    Replies: 9
    Last Post: 09-09-2008, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •