There's no evidence that we do. Pseudo-scientific "researchers" like Freud and folk stories, such as the Joseph story in the Bible, simply made up the idea whatever you dream is a code or metaphor for something in your life, but this is completely unsupported by scientific research. read more
We don't know what causes specific dream imagery. It might be a code, but it just as well might be random noise. Some researchers believe it has something to do with encoding memories. We need a lot more research before we can confidently used words like "do" as it appears in this question. read more
Analogy and metaphor are integral subroutines of that algorithm, without which classification would not work. Dreaming is a poorly understood, but important part of the learning process. This is why we tend to generalize. read more