# Show operator precedence?

**URL:** <https://discourse.julialang.org/t/show-operator-precedence/115143>\
**Category:** New to Julia\
**Tags:** operator\
**Created:** [June 3, 2024, 9:13pm UTC](https://discourse.julialang.org/t/show-operator-precedence/115143 "2024-06-03T21:13:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [June 3, 2024, 9:13pm UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/1 "2024-06-03T21:13:13Z")

</div>

In the old days, I used to use a handy utility every once in a while to show how the parser interprets operator precedence:

```julia
% perl -MO=Deparse,-p -e '$x < 3 && $y < 7' 
(($x < 3) and ($y < 7));

```

I’ve always found this a lot easier than locating the precedence table in the docs, and trying to remember which way “higher” and “lower” means. Is there something similar in Julia, maybe a macro?

---

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [June 3, 2024, 9:19pm UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/2 "2024-06-03T21:19:32Z")

</div>

[https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity)

Does that cover what you are looking for? Specifically the link to the parser file and/or the operator precedence lookup:

```julia-repl
julia> Base.operator_precedence(:+), Base.operator_precedence(:*), Base.operator_precedence(:.)
(11, 12, 17)

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 3, 2024, 9:33pm UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/3 "2024-06-03T21:33:08Z")

</div>

A great utility — especially if you’re already familiar with Lispy (non-)syntaxes — is `Meta.show_sexpr`:

```Julia-repl
julia> Meta.show_sexpr(:(x < 3 && y < 7))
(:&&, (:call, :<, :x, 3), (:call, :<, :y, 7))

julia> Meta.show_sexpr(:(x < 3 & y < 7))
(:comparison, :x, :<, (:call, :&, 3, :y), :<, 7)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 3, 2024, 10:45pm UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/4 "2024-06-03T22:45:25Z")

</div>

`Meta.@lower` could also be useful.

```julia
julia> Meta.@lower x < 3 && y < 7
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = x < 3
└── goto #3 if not %1
2 ─ %3 = y < 7
└── return %3
3 ─ return false
))))

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 4, 2024, 1:47am UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/5 "2024-06-04T01:47:38Z")

</div>

there’s also `Meta.@dump` which will show the parsing with fewer changes.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 4, 2024, 2:39am UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/6 "2024-06-04T02:39:22Z")

</div>

is there an `from_sexpr`?

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [June 4, 2024, 4:07pm UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/7 "2024-06-04T16:07:38Z")

</div>

Thanks, those are all good to know - I think `Meta.show_sexpr()` might be the most directly useful for this purpose. Still takes a bit of brain-scratching to see what’s happening, but not too much.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [June 4, 2024, 5:28pm UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/8 "2024-06-04T17:28:54Z")

</div>

Another one for the heap, using JuliaSyntax.jl:

```julia-repl
julia> JuliaSyntax.parsestmt(SyntaxNode, "x < 3 && y < 7")
line:col│ tree │ file_name
   1:1 │[&&]
   1:1 │ [call-i]
   1:1 │ x
   1:3 │ <
   1:5 │ 3
   1:9 │ [call-i]
   1:10 │ y
   1:12 │ <
   1:14 │ 7

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 4, 2024, 7:15pm UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/9 "2024-06-04T19:15:05Z")

</div>

> [@jling](#):
>
> is there an `from_sexpr`?

To go `from_sexpr`, you plop an `Expr` in front of each and every parentheses. So `(:comparison, :x, :<, (:call, :&, 3, :y), :<, 7)` is:

```julia-repl
julia> Expr(:comparison, :x, :<, Expr(:call, :&, 3, :y), :<, 7)
:(x < 3 & y < 7)

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 4, 2024, 8:22pm UTC](https://discourse.julialang.org/t/show-operator-precedence/115143/10 "2024-06-04T20:22:28Z")

</div>

seems like it could be a useful addition to Base.
