why are these patents accepted, since there are others very similar
They can't be exactly similar and granted at the same time. Anybody can file a patent for anything as long as it follows the basic criterias of patentability.
"Code generator" is just a term which might be getting used in completely different contexts in different patents. It is quite possible that these patents may be using a similar term to define different concepts or processes.
It is also possible that the patents you found might be continuations of each other, describing different aspects of a same process. Patents often get granted with just few claims being valid, while other gets rejected due to prior art.
And if somebody files for a patent, how does one know he/she is not infringing?
Answer of @atsby cleared this question pretty well. You are not infringing someone's patent just by filing a similar one. Your patent will get rejected as the other patent will be found by examiner. You have to make, sell, and earn profit from a patented invention in order to infringe.
Another thing, if you're concerned about how will you know that your product is infringing a patent or not. It's done by performing a "freedom to operate" search. You search the market if anybody has a patent on the product you are planning to sell, if you find a patent, you approach them for a license.