pattern compilation algorithm