From w||||@mwdun|@p @end|ng |rom gm@||@com  Fri Feb  4 21:28:12 2022
From: w||||@mwdun|@p @end|ng |rom gm@||@com (Bill Dunlap)
Date: Fri, 4 Feb 2022 12:28:12 -0800
Subject: [Rd] Parser oddity with <- and =
In-Reply-To: <8ee66596-5531-2fc4-252f-a2a0946a1d1c@gmail.com>
References: <8ee66596-5531-2fc4-252f-a2a0946a1d1c@gmail.com>
Message-ID: <CAHqSRuQvhB-rDeHNhLVRffahkpSyu=hjOdRNs0LUXvUrAopeKA@mail.gmail.com>

In R-4.1.2 and R-devel from two weeks ago I do not get the `<-<-`:

> str.language(parse(text = "a <- b = c"))
expression: structure(expression(a <- b = c), sr ...
  language: a <- b = c
    symbol: =
    language: a <- b
      symbol: <-
      symbol: a
      symbol: b
    symbol: c
> identical(parse(text = "a <- b = c")[[1]], parse(text = "`=`( `<-`(a, b),
c)")[[1]])
[1] TRUE

str.language() is a rudimentary parse tree displayer:

str.language <- function(expr, name = "", indent = 0)
{
    trim... <- function(string, width.cutoff) {
        if (nchar(string) > width.cutoff) {
            string <- sprintf("%.*s ...", width.cutoff-4, string)
        }
        string
    }
    cat(sep="", rep("  ", indent), typeof(expr), ": ",
        if(length(name)==1 && nzchar(name)) { paste0(name, " = ") },
        trim...(deparse1(expr, width.cutoff=40), width.cutoff=40),
        "\n")
    if (is.function(expr)) {
        str.language(formals(expr), name="[formals]", indent = indent + 1)
        str.language(body(expr), name="[body]", indent = indent + 1)
    } else if (is.recursive(expr)) {
        expr <- as.list(expr)
        nms <- names(expr)
        for (i in seq_along(expr)) {
            str.language(expr[[i]], name=nms[[i]], indent = indent + 1)
        }
    }
    invisible(expr)
}


-Bill

On Fri, Feb 4, 2022 at 9:34 AM Duncan Murdoch <murdoch.duncan at gmail.com>
wrote:

> Here's an odd parse:
>
>     a <- b = 1
>
> This appears to be parsed as
>
>     `<-<-`(a, b, 1)
>
> instead of being equivalent to
>
>     a <- b <- 1
>
> I wonder if that's intentional?
>
> (This showed up at https://stackoverflow.com/q/70989067/2554330, where
> it caused a lot of confusion.  I think the original intent was that `a`
> would be a macro holding `b = 1`, but I'm not sure of that.)
>
> Duncan Murdoch
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



[Rd] Parser oddity with <- and =
Bill Dunlap