Currently LaTeX is generated using ad hoc modules. Could it be done using a grammar? Need to check that.