我如何可以将三元运营商到precedence爬山算法? [英] How can I incorporate ternary operators into a precedence climbing algorithm?

查看:124
本文介绍了我如何可以将三元运营商到precedence爬山算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着precedence爬坡章节中关于此网页的说明的实现使用各种一元preFIX和二进制中缀运算符precedence登山算法算术评估。我也想包括三元运营商(即三元条件运算符)。

I followed the explanation given in the "Precedence climbing" section on this webpage to implement an arithmetic evaluator using the precedence climbing algorithm with various unary prefix and binary infix operators. I would also like to include ternary operators (namely the ternary conditional operator ?:).

在网页上给出的算法使用以下语法:

The algorithm given on the webpage uses the following grammar:

E --> Exp(0) 
Exp(p) --> P {B Exp(q)} 
P --> U Exp(q) | "(" E ")" | v
B --> "+" | "-"  | "*" |"/" | "^" | "||" | "&&" | "="
U --> "-"

我如何可以将三元运营商进入这个语法?

How can I incorporate ternary operators into this grammar?

推荐答案

要具体,我将使用C / C ++ / Java的作为一个例子

To be specific, I'll use C/C++/Java's ?: as an example.

目前看来,在这些语言的运营商允许 之间的任何有效的前pression和? ,包括形成前pressions:本身和那些与运营商,他们的precedence形成比的的precedence低:,例如: = (例如: A B = C:D A b,C:D A b C:??d:E

It appears that in those languages the ?: operator allows any valid expression between ? and :, including expressions formed with ?: itself and those formed with operators, whose precedence is lower than the precedence of ?:, e.g. = and , (examples: a ? b = c : d, a ? b , c : d, a ? b ? c : d : e).

这意味着你应该把 以完全相同的方式在分析前pressions。当你分析出? EXPR:,它作为一个整体,是一个二元运算。所以,你解析(表达式)? EXPR:以同样的方式,但前者是前pression而后者则是一个二元运算符(内嵌有一个前pression)

This suggests that you should treat ? and : in exactly the same manner as ( and ) while parsing expressions. When you have parsed out ? expr :, it, as a whole, is a binary operator. So, you parse ( expr ) and ? expr : in the same way, but the former is an expression while the latter is a binary operator (with an expression embedded in it).

现在? EXPR:是一个二元运算符( A -expr-:2 B 无异A * B 在binaryness条款),你应该能够支持它pretty很像你已经支持任何其他二元运算符。

Now that ? expr : is a binary operator (a ?-expr-: b being no different than a * b in terms of "binaryness"), you should be able to support it pretty much like any other binary operator that you already support.

个人而言,我不劳烦分裂到自己的独立的二进制运算。最后,它仍然是一个三元运算符,它必须被链接到3操作数和被视为前pression评估过程中一个整体。如果你正在跟踪你在问题中提到的文章中的方法,并正在创建一个AST,然后你去那里,有一个左子树,右子节点(如任何其他二进制运算符),此外,一中间子节点

Personally, I'd not take the trouble to split ?: into separate binary operators of their own. In the end, it's still a ternary operator and it must be linked to 3 operands and considered as a whole during expression evaluation. If you are following the approach in the article that you mentioned in the question and are creating an AST, then there you go, ?: has a left child node, a right child node (as any other binary operator) and, additionally, a middle child node.

的precedence - expr的 - :作为一个整体应该是低的。在C / C ++(和Java中?)这不是最低的,但。它是由你来决定你希望它是什么。

The precedence of ?-expr-: as a whole should be a low one. In C/C++ (and in Java?) it's not the lowest, though. It's up to you to decide what you want it to be.

到目前为止,我们还没有涉及的关联性:。在C / C ++和Java, - EXPR - :是右结合就像赋值操作符 = 。再次,这是由你来让左结合或保持右结合的。

So far we haven't covered the associativity of ?:. In C/C++ and Java, ?-expr-: is right-associative just like the assignment operator =. Again, it's up to you to make it left-associative or keep it right-associative.

和这样的:

E --> P {B P}
P --> v | "(" E ")" | U P
B --> "+" | "-" | "*" | "/" | "^"
U --> "-"

应该成为这样的:

should become something like this:

E --> P {B P}
P --> v | "(" E ")" | U P
B --> "+" | "-" | "*" | "/" | "^" | "?" E ":"
U --> "-"

这篇关于我如何可以将三元运营商到precedence爬山算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆