Parenthesizing infix expressions in Redex renderings?

28 views
Skip to first unread message

David Thrane Christiansen

unread,
Feb 24, 2021, 5:07:59 AM2/24/21
to racket users
Hello all,

I'm working on coding up a little language model in Redex, and I'd like to get it to render things in the form that my colleagues are used to. This means some infix operators as well as dealing with parenthesizing based on operator precedence.

Here's a boiled-down sample of what I'm up to:

#lang racket

(require redex pict)

(define-language L
  (C success (then C C) (or C C)))

(with-compound-rewriters
  (['or (match-lambda [(list _ _ x y _) (list "" x " or " y "")])]
   ['then (match-lambda [(list _ _ x y _) (list "" x " then " y "")])])
  (vl-append
   20
   (render-language L)
   (render-term L (then (or success success) success))))

I've attached the result. The resulting rendering of L looks appropriate, but the nesting of then and or in the rendered term does not indicate the nesting. I'd like to be able to specify precedence and associativity and have parentheses inserted appropriately; failing that, a reasonable backup would be parenthesizing sub-expressions that are not atomic.

Can anyone point me at the right resource to use to figure out how to do this?

Thank you!

David
rendered.png

Ryan Culpepper

unread,
Feb 24, 2021, 5:29:03 AM2/24/21
to David Thrane Christiansen, racket users
The `binary-rw` function from unstable/gui/redex library has some support for optionally parenthesizing its arguments.

Ryan


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAF_itEtHuJP6i%3DR3_ggKTn1%3DRDmswZfCiFMYJqBwcHqpXB7fpw%40mail.gmail.com.

David Thrane Christiansen

unread,
Feb 24, 2021, 5:46:54 AM2/24/21
to ry...@racket-lang.org, racket users
Thanks Ryan!

There's all sorts of nice goodies in that library, like current-atomic-rewriters and friends.

David

Robby Findler

unread,
Feb 24, 2021, 2:38:13 PM2/24/21
to David Thrane Christiansen, Ryan Culpepper, racket users
Yeah, I've been meaning to integrate those parts of that library into Redex proper but just haven't found the time.

Also, I've thought it would be nice if Redex were able to look at the context of a particular, say, "or" or "then" expression and figure out if it needed parens. Sadly, for now you have to specialize the rewriting based on specific examples you're rendering (or just have too many parens, sigh).

Robby


David Thrane Christiansen

unread,
Feb 26, 2021, 7:37:08 AM2/26/21
to Robby Findler, Ryan Culpepper, racket users
Hi Robby,

I suppose a system that optionally annotated each alternative for a nonterminal with precedence and associativity would be the maximally featureful way to go here, with unannotated alternatives being parenthesized like today. Something akin to #:binding-forms, I guess. But I haven't dug enough into the current pict rendering to know if that would fit in nicely to the current customization system.

/David
Reply all
Reply to author
Forward
0 new messages