【问题标题】:Shunting-Yard Validate Expression调车场验证表达式
【发布时间】:2015-06-20 12:21:07
【问题描述】:

我们使用 Shunting-Yard 算法来评估表达式。我们可以通过简单地应用算法来验证表达式。如果缺少操作数、不匹配的括号和其他东西,它会失败。然而,Shunting-Yard 算法具有比人类可读的中缀更大的支持语法。例如,

1 + 2
+ 1 2
1 2 +

提供“1+2”作为Shunting-Yard 算法的输入的所有可接受的方式。 '+ 1 2' 和 '1 2 +' 不是有效的中缀,但标准的 Shutting-Yard 算法可以处理它们。该算法并不真正关心顺序,它按优先顺序应用运算符,获取“最近”操作数。

我们希望将输入限制为有效的人类可读中缀。我正在寻找一种方法来修改 Shunting-Yard 算法以使其因无效中缀而失败,或者在使用 Shunting-Yard 之前提供中缀验证。

是否有人知道任何已发布的技术可以做到这一点?我们必须同时支持基本运算符、自定义运算符、括号和函数(带有多个参数)。除了在线基本操作符之外,我还没有看到任何可以使用的东西。

谢谢

【问题讨论】:

  • 您可以只使用中缀运算符解析器。这当然不会利用您现有的 Shutting Yard 解析器,但它会起作用。
  • @rici 也许,我现在正在研究其他问题和答案。
  • @denver:我正要直接回答这个问题,但我记得我已经回答过了,所以我只是把你的答案推荐给你。我建议的状态机也是“如何在调车场算法中处理一元减号”问题的答案,因此您可能已经实现了类似的东西。链接的答案还试图区分用于分组的 () 和用于函数调用的 ();如果它对你没有用,你可以忽略它,但它实际上并不比一元减号问题更复杂。
  • @rici 看来状态机(2 个状态,期望操作员和期望操作数)是我的问题的解决方案。在我们支持的每种类型的标记(常量、变量、函数、二元运算符、一元运算符、左括号、右括号和参数分隔符)的 if 语句中,如果在读取令牌,然后我们将状态设置为我们期望的下一步。

标签: c# algorithm parsing expression shunting-yard


【解决方案1】:

关于调车场算法的一个很好的讨论是http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm 那里介绍的算法使用运算符堆栈的关键思想,但有一些语法可以知道接下来应该预期什么。它有两个主要函数E() 需要一个表达式,P() 需要一个前缀运算符、一个变量、一个数字、括号和函数。前缀运算符总是比二元运算符绑定得更紧密,所以你想先处理这个。

如果我们说 P 代表某个前缀序列,而 B 是二元运算符,那么任何表达式都将具有以下形式

P B P B P

即您要么期待前缀序列,要么期待二元运算符。正式的语法是

E -> P (B P)*

P 将是

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

这转换为伪代码

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()
}

您可以扩展它来处理其他一元运算符、函数、括号、后缀运算符。

【讨论】:

  • 感谢您的回答。 rici 引用的答案提供了类似的方法。
【解决方案2】:

我的问题的解决方案是使用state machine recommended by Rici 增强发布在Wikipedia 上的算法。我在这里发布伪代码是因为它可能对其他人有用。

Support two states, ExpectOperand and ExpectOperator.

Set State to ExpectOperand
While there are tokens to read:
    If token is a constant (number)
        Error if state is not ExpectOperand.
        Push token to output queue.
        Set state to ExpectOperator.
    If token is a variable.
        Error if state is not ExpectOperand.
        Push token to output queue.
        Set state to ExpectOperator.
    If token is an argument separator (a comma).
        Error if state is not ExpectOperator.
        Until the top of the operator stack is a left parenthesis  (don't pop the left parenthesis).
            Push the top token of the stack to the output queue.
            If no left parenthesis is encountered then error.  Either the separator was misplaced or the parentheses were mismatched.
        Set state to ExpectOperand.
    If token is a unary operator.
        Error if the state is not ExpectOperand.
        Push the token to the operator stack.
        Set the state to ExpectOperand.
    If the token is a binary operator.
        Error if the state is not ExpectOperator.
        While there is an operator token at the top of the operator stack and either the current token is left-associative and of lower then or equal precedence to the operator on the stack, or the current token is right associative and of lower precedence than the operator on the stack.
            Pop the operator from the operator stack and push it onto the output queue.
        Push the current operator onto the operator stack.
        Set the state to ExpectOperand. 
    If the token is a Function.
        Error if the state is not ExpectOperand.  
        Push the token onto the operator stack.
        Set the state to ExpectOperand.
    If the token is a open parentheses.
        Error if the state is not ExpectOperand.
        Push the token onto the operator stack.
        Set the state to ExpectOperand.
    If the token is a close parentheses.
         Error if the state is not ExpectOperator.
         Until the token at the top of the operator stack is a left parenthesis.
             Pop the token off of the operator stack and push it onto the output queue.
         Pop the left parenthesis off of the operator stack and discard.
         If the token at the top of the operator stack is a function then pop it and push it onto the output queue.
         Set the state to ExpectOperator.
At this point you have processed all the input tokens.
While there are tokens on the operator stack.
    Pop the next token from the operator stack and push it onto the output queue.
    If a parenthesis is encountered then error.  There are mismatched parenthesis.

通过查看前一个标记,您可以轻松地区分一元和二元运算符(我具体说的是负前缀和减法运算符)。如果没有前一个标记,前一个标记是一个左括号,或者前一个标记是一个运算符,那么你遇到了一元前缀运算符,否则你遇到了二元运算符。

【讨论】:

  • 我想在我们退出第一个while循环之后,我们必须添加一行Error if the state is not ExpectOperator。这确保了最后一个标记是一个操作数。
  • 另外,必须检查添加到堆栈的最后一个标记是否不是一元的。否则像5++++++6 这样的东西仍然有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 1970-01-01
相关资源
最近更新 更多