The knifeman, who was known to police, shouted “God is greatest” during the attack, prosecutors say.