trying to implement Prolog grammar