As others have already stated, both are grammatically possible. We distinguish the different meanings based on our knowledge of the real world.
I'm answering to add that this is closely related to the AI challenge known as the Winograd schema, which consists of sentences where an ambiguous pronoun changes reference based on the overall meaning of the sentence. The classic example is:
The city council refused the demonstrators a permit because they [feared/advocated] violence.
If the blank is "feared," it means the council was afraid of violence; if the blank is "advocated," it means the demonstrators advocated violence.
Machine learning models, which lack any real-world knowledge/experience, usually find this very difficult; but any speaker familiar with the words will have no trouble understanding the difference.
EDIT: Here's an interesting set of Winograd schemas, including in non-English languages.
This is part of a bigger general pattern: meaning doesn't directly proceed from words. Rather, words serve to narrow down contextually possible meanings, with (at least some minimal) mutual understanding coming first.