I know it seems like there is no BNF for TeX language. But I am interested in BNF, PEG or any other kind of formal grammar for the LaTeX format. Is it possible to create such a grammar for pure LaTeX (LaTeX without any plain TeX or TeX primitives)? If yes, has anyone done that?

Unfortunately there are only very few researchs and papers about this topic. Googling LaTeX and grammar brings grammar check or such.

  • 1
    Latex is build on top of tex, so you cannot remove tex from the equation. Isn't the whole issue that bnf grammar cannot exist for languages that can transform itself on the fly? (not that I know much about bnf grammars) – daleif Oct 17 '18 at 19:52
  • 2
    @daleif That's basically it. Another way to look at it is that grammars define syntax, but not semantics. However you can't parse a piece of TeX that plays games with catcodes, unless you can define the semantics of the macros that are playing such games at parse time. In effect, the parser can rewrite its own grammar as it parses the input, and how it is rewritten depends on the input it is parsing! – alephzero Oct 17 '18 at 20:11
  • 1
    Maybe one could define a "meta-grammar" that specifies how TeX can construct its own grammar on the fly - but such a thing would certainly not be in BNF. Van Wijngaarden's two-level grammar for Algol 68 comes to mind: " Van Wijngaarden grammars use a context-free grammar to generate an infinite set of productions that will recognize a particular ALGOL 68 program;" to quote Wikipedia... – alephzero Oct 17 '18 at 20:22
  • @alephzero thanks for the explanation, much appreciated – daleif Oct 17 '18 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.