The active ingredients in cough syrup are drugs derived from opium. Coughing is caused by the brain, and these drugs act on the brain to suppress the urge to cough.
Opiates are a type of bitter tasting chemicals called alkaloids. These are molecules that contain basic (alkaline) nitrogen atoms. Some alkaloids you may know include caffeine, nicotine, codeine, and quinine. Others are cocaine, morphine, heroin, ephedrine, atropine, dextromethorphan, pseudoephedrine, and strychnine.
Because the ingredients are bitter tasting, cough syrups have strong flavors to mask the bitterness. But that is not the only reason cough syrups are unpleasant to use.
Because the ingredients in cough syrup include things like dextromethorphan and codeine, which are used by some people as recreational drugs, the pill forms have been taken off the market. Instead, the syrup forms are available, since they are much harder to use for purposes other than preventing coughs.
In other words, cough syrups taste bad on purpose, so people don’t misuse them.