import re


def udToAnu(udStr):
    """
     Converts a string of udatta marked syllables to
     an anudatta marked one. Notation:
     U = udatta
     A = anudatta
     S = svarita
     B = unmarked in udatta notation
     D = unmarked in anudatta notation

     Example:
     >>> udToAnu('BBUBBUUB')
     "AADSADDS"
    """

    anuStr = udStr

    anuStr = re.sub(r"BU",  "AU", anuStr)
    anuStr = re.sub(r"UB",  "US", anuStr)
    anuStr = re.sub(r"U",   "D", anuStr)

    while ("BA" in anuStr or "BD" in anuStr):
        anuStr = re.sub(r"^(B*)[BD](A)",    r"\1A\2", anuStr)
        anuStr = re.sub(r"\n(B*)[BD](A)",   r"\1A\2", anuStr)
        anuStr = re.sub(r"([ADSB])B([AD])", r"\1D\2", anuStr)
        anuStr = re.sub(r"B$", "D", anuStr)

    return anuStr