Is it a rule of formal languages that all occurences of a symbol must 'refer' to the same object?
It's a rule, specifically a convention, so yes. Why is it a convention? Because when you have a symbol represent more than one different thing, it creates ambiguity. Generally accepted practice to differentiate is often using tick marks or subscripts if one wants to use very similar labels, say, of items belonging to the same class. Consider:
S1 A contains a, a, a
S2 A contains a1, a2, a3
Does S1 contain three separate as? From a semantic perspective, they all seem to be the same. From a syntactic perspective, if they're written more than once, it seems to suggest no. S1 could be a multiset, for instance, or it could be a problem of naive set theory to test if a student understands that sets can only contain one of an element no matter how many times written. S2 on the other leaves no doubt that the elements in some important way differ.
In written languages you may see sentences like, 'I need him, him and him' where 'him' refers to a different person each time (based on the pragmatics of the context).
In natural language, there's a lot more context so it might be easier to tell without explicit notation, but still, by conventions, one would never put such a sentence in a deposition or a history essay for exactly the same reason. It might be interesting to note that there seems to be conventions for spoken language too. One widely accepted set of conventions are known as the Gricean cooperative principle which is illustrated by some maxims. In the sentence you offer, having three hims is an example of the use of deixis which is the use of reference to an object outside of the language itself:
In linguistics, deixis (/ˈdaɪksɪs/, /ˈdeɪksɪs/)2 is the use of general words and phrases to refer to a specific time, place, or person in context, e.g., the words tomorrow, there, and they. Words are deictic if their semantic meaning is fixed but their denoted meaning varies depending on time and/or place. Words or phrases that require contextual information to be fully understood—for example, English pronouns—are deictic.
Just remember that conventions of language are just that: conventions. While you can follow convention or oppose it (ee cummings comes to mind), generally there are social consequences, so sometimes its important to go along and get along. One could write an entire math paper using the same variable with numerical subscripts, but getting it published might be a tough sell