检查int是否在两个数字之间 [英] Check if int is between two numbers

查看:129
本文介绍了检查int是否在两个数字之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你试图找出int是否介于数字之间,为什么不能这样做:

Why can't do you this if you try to find out whether an int is between to numbers:

if(10 < x < 20)

而不是它,你将不得不做

Instead of it, you'll have to do

if(10<x && x<20)

这似乎有点开销。

推荐答案

一个问题是三元关系结构会引入严重的解析器问题:

One problem is that a ternary relational construct would introduce serious parser problems:

<expr> ::= <expr> <rel-op> <expr> |
           ... |
           <expr> <rel-op> <expr> <rel-op> <expr>

当你尝试使用典型的PGS表达这些作品的语法时,你会发现那里是第一个< rel-op> 点的转换减少冲突。解析器需要预先查看任意数量的符号,以查看是否有第二个< rel-op> ,然后才能决定是否使用了二进制或三元形式。在这种情况下,你不能简单地忽略冲突,因为这会导致错误的解析。

When you try to express a grammar with those productions using a typical PGS, you'll find that there is a shift-reduce conflict at the point of the first <rel-op>. The parse needs to lookahead an arbitrary number of symbols to see if there is a second <rel-op> before it can decide whether the binary or ternary form has been used. In this case, you could not simply ignore the conflict because that would result in incorrect parses.

我不是说这个语法是致命的歧义。但我认为你需要一个回溯解析器才能正确处理它。对于编程语言而言,这是一个严重的问题,其中快速编译是一个主要卖点。

I'm not saying that this grammar is fatally ambiguous. But I think you'd need a backtracking parser to deal with it correctly. And that is a serious problem for a programming language where fast compilation is a major selling point.

这篇关于检查int是否在两个数字之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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