Shunting-Yard Validate Expression

后端 未结 2 1729
误落风尘
误落风尘 2020-12-06 19:50

We use the Shunting-Yard algorithm to evaluate expressions. We can validate the expression by simply applying the algorithm. It fails if there are missing operands, miss-m

2条回答
  •  旧巷少年郎
    2020-12-06 20:19

    A nice discussion on Shunting Yard algorithms is http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm The algorithm presented there uses the key idea of the operator stack but has some grammar to know what should be expected next. It has two main functions E() which expects an expression and P() which is expecting either a prefix operator, a variable, a number, brackets and functions. Prefix operators always bind tighter than binary operators so you want to deal this any of then first.

    If we say P stands for some prefix sequence and B is a binary operator then any expression will be of the form

    P B P B P
    

    i.e. you are either expecting a a prefix sequence or a binary operator. Formally the grammar is

    E -> P (B P)*
    

    and P will be

    P -> -P | variable | constant | etc.
    

    This translates to psudocode as

    E() {
        P()
        while next token is a binary op:
             read next op
             push onto stack and do the shunting yard logic
             P()
        if any tokens remain report error
        pop remaining operators off the stack
    }
    
    P() {
        if next token is constant or variable:
             add to output
        else if next token is unary minus: 
             push uminus onto operator stack
             P()
    }
    

    You can expand this to handle other unary operators, functions, brackets, suffix operators.

提交回复
热议问题