Three, in practice. Battlefield 3 groups weapon customisation into attachment categories — optics, muzzle and barrel options, underbarrel pieces, and accessories — and you may equip one item from each category that the weapon supports. Since a given weapon typically offers three usable slots, the effective total is three attachments.
The limit is by design. Allowing a full stack of every available option would flatten the differences between weapons, whereas forcing a choice means an optic costs you a grip, or a suppressor costs you something else. That trade-off is the point of the system.
As a side note, the game ships with around fifty weapons, each with its own attachment list, so the exact three slots available vary slightly from gun to gun.