[Rd] Operator precedence of =, <- and ?

Tomas Kalibera tom@@@k@||ber@ @end|ng |rom gm@||@com
Mon Jan 27 15:31:05 CET 2020

Thanks, for reference, this has been already reported as PR#16710. Now 
fixed, the precedence of "?" is again lower than the precedence of "=", 
as documented in ?Syntax.

Please note the role of the precedence table in the parser definition: 
the precedence given there is not necessarily the precedence of 
operators you would see in the language. The precedence table in the 
parser definition tells the parser how to behave in situations when it 
does not know otherwise (please refer to bison documentation if you were 
curious about the details), but it "knows otherwise" in R, the grammar 
was written so that "=" had the lowest precedence, before the fix. Also, 
in principle, the precedence can be different in different contexts.

It is the precedence table in the programming language 
definition/specification/documentation (here ?Syntax) that should give 
the precedence a user would observe in the language. Following the 
table, programmers know when it is safe to omit parentheses. However, 
this may be error prone for operators that have different 
meaning/precedence in different languages, and hence such tables are 
often intentionally incomplete. This is another reason why not to look 
into the parser implementation to learn about operator precedence.  R's 
precedence table in ?Syntax is fairly complete and in many cases using 
parentheses where the table gives precedence would improve readability 
of the code.


On 1/10/20 11:59 AM, Konrad Rudolph wrote:
> The documentation (help("Syntax")) gives the operator precedence of the
> assignment operators and help, from highest to lowest, as:
>         ‘<- <<-’           assignment (right to left)
>         ‘=’                assignment (right to left)
>         ‘?’                help (unary and binary)
> If I understand correctly this implies that `a = b ? c` and `a <- b ? c`
> should parse identically. Or, if using the unary version, `?a = b` and `?a
> <- b` should parse identically.
> However, as noted by Antoine Fabri on Stack Overflow [1], they have
> different parses (on R 3.5.3 and 3.6.1, at least), which puts the
> precedence of `?` *between* that of `<-` and `=`. In fact, src/main/gram.y
> [2] appears to show the same precedence table as the documentation;
> presumably the parser at some point rewrites the parse tree manually.
> At any rate, should this be fixed in the documentation? Or is the
> documentation “correct”, and there’s a bug in the parser (in some versions
> of R)?
> [1] <
> https://stackoverflow.com/questions/1741820/51564252#comment105506343_51564252
> [2] <
> https://github.com/wch/r-source/blob/386c3a93cbcaf95017fa6ae52453530fb95149f4/src/main/gram.y#L384-L390
> --
> Konrad Rudolph
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

More information about the R-devel mailing list