updated to accept any grammar