I think I found the answer. It's Focus.
Here's what McCawley 1998 says, page 68, Chapter 3
(Tests for Deep and Surface Constituent Structures):
iv. Placement of Elements with Focus
There are a number of words in English (only, even, too, also) that are associated with a focus: an element that is implicitly constrasted with other items, as in John drinks only beer, where only serves to contrast beer with such other items as wine or vodka; that is, John drinks only beer says that John doesn't drink wine, that he doesn't drink vodka, and so on.
Only usually precedes its focus, but need not immediately precede it; for example, John only drinks beer can be interpreted with beer as focus even though only is separated from it by drinks. ...
While only can be separated from its focus, it cannot be put in front of just any matter that precedes the focus.
The rule for only is that
- only can precede
- either the focus itself,
- or any constituent containing the focus.
(or any constituent containing that constituent, ad infinitum)
This means that, in the sentences
- He needed to talk only about tomatoes.
- He needed to talk about only tomatoes.
there is no difference in meaning or grammaticality.
Only can come immediately before the focus tomatoes, or it can come before the preposition phrase about tomatoes, which contains the focus. Indeed, only can come before the verb phrase needed to talk about tomatoes, which also contains the focus, with no change in meaning or grammaticality.
- He only needed to talk about tomatoes.
Of course, the further away only is from its focus, the more ambiguity is possible, because there are more possible focusses in a large constituent; that means that the focus is normally stressed heavily to identify it. This is not possible in writing, since there's no written intonation or stress except in the mind's ear. So keeping such an item close to its focus is usually good advice for writers.
Though it's not a grammar rule.
The grammar rule is as stated.