Begin by not slurping the full input text in gram.pl, since this is slow and eats up a lot of memory for huge text files.
Maybe just add smarts for segmenting into sentences incrementally, and then just call grammatical_errors on individual sentences.