Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ternary operator ambiguous behaviour #7252

Closed
berceanu opened this issue Jun 13, 2014 · 5 comments
Closed

ternary operator ambiguous behaviour #7252

berceanu opened this issue Jun 13, 2014 · 5 comments
Assignees
Labels
kind:breaking This change will break code

Comments

@berceanu
Copy link

Expressions like
1 > 0 ? a=2 : a=3
fail with
syntax: invalid assignment location
This can be solved by
1 > 0 ? a=2 : (a=3)
but I think the expression is unambiguous enough to work without parantheses.

@quinnj
Copy link
Member

quinnj commented Jun 13, 2014

Why not do a = 1 > 0 ? 2 : 3? <-- That's actually the only allowed use of the ternary in Java.

@berceanu
Copy link
Author

what about 1 > 0 ? a=2 : b=3 then?

@mbauman
Copy link
Sponsor Member

mbauman commented Jun 13, 2014

Sure, but this seems inconsistent with the other parsing behaviors of ?:. My mental model is that it should try to find the matching else : greedily before allowing : to be used within a range.

1 > 0 ? a=2 : 3 # works fine
1 > 0 ? 2 : a=3 # fails (2:a is an invalid assignment location)

That said, this seems unambiguous to me only because #6823 hasn't yet been decided upon — there still must be a colon somewhere in the statement. Were that to change this becomes more tricky.

Edit: oooh, I see. It's not 2:a that's the invalid location; it's the whole ternary expression! That does make a little more sense to me now.

@joehuchette
Copy link
Member

The problem is that = has low precedence, so this gets parsed as

julia> a = :(1 > 0 ? a=2 : a=3)
:(if 1 > 0
            a = 2
        else
            a
        end = 3)

@JeffBezanson JeffBezanson self-assigned this Jun 16, 2014
@JeffBezanson
Copy link
Sponsor Member

? is parsing the then argument as nonterminal eq*, and the else argument as nonterminal cond (the precedence of ?). They should probably both be parsed as eq*, fixing this issue.

jakebolewski added a commit to JuliaLang/JuliaParser.jl that referenced this issue Jun 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:breaking This change will break code
Projects
None yet
Development

No branches or pull requests

5 participants