title: How does attribute assignment in R break down in terms of the language grammar?
body: <h3>Problem</h3>
  
  <p>In R, you can assign an attribute like so:</p>
  
  <pre><code>attr(x, "foo") &lt;- "bar"
  </code></pre>
  
  <p>I get what this does, but I'm trying to understand how this statement breaks down into more fundamental pieces of the R language.</p>
  
  <h3>Thoughts</h3>
  
  <p>While <code>attr</code> is a function, it appears to also be a part of the fundamental language grammar, since you cannot do this:</p>
  
  <pre><code>t = attr
  t(x, "bar") &lt;- "baz" # gives an error
  </code></pre>
  
  <p>The most similar syntax I can think of is in PHP, where you can assign by "calling" the <code>list</code> "function" like this:</p>
  
  <pre><code>list($a, $b) = array(1, 2);
  </code></pre>
  
  <p>I understand the above line to have the following grammar:</p>
  
  <pre><code>list_assignment:
      list(&lt;var&gt;...) = &lt;expression&gt;;
  </code></pre>
  
  <p>It would seem to make sense that in R, the grammar is defined similarly:</p>
  
  <pre><code>attr_assignment:
      attr(&lt;var&gt;, &lt;expression&gt;) &lt;- &lt;expression&gt;;
  </code></pre>
  
  <p>But you can also write this:</p>
  
  <pre><code>x = `attr&lt;-`(x, "bar", "baz")
  </code></pre>
  
  <p>Which seems that it would require one of the following:</p>
  
  <ul>
  <li>Special support for <code>`attr&lt;-`(&lt;var&gt;, &lt;expr&gt;, &lt;expr&gt;)</code> in the grammar</li>
  <li>A real function named <code>attr&lt;-</code> which does the same as <code>attr(...) &lt;-</code> under the hood</li>
  </ul>
  
  <h3>Summary</h3>
  
  <p>How should this <code>attr</code> assignment should be thought about in terms of simpler language elements? What about <code>`attr&lt;-`</code>? Additionally, how are these implemented within the R parser?</p>