Skip to content
Advertisement

Check if int is between two numbers

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)

which seems like a bit of overhead.

Advertisement

Answer

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>

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement