Adding compliant code examples to verbatim blocks in LaTeX
Feature request.
It would be great to be able to include in the LaTeX file a number of compliant code examples, to give a flavour of the language specified by the grammar. Could there be a special notation that could be used in a .cf file that would be ignored by bnfc with the exception of LaTeX generation, which would add each code example in to a verbatim block (if all rules are accepted by bnfc) ?
One could have a pragma like
{-# Example(Exp): 5 + 6 * 7 #-}
The name of the non-terminal (Exp) could be optional; in that case, the example would refer to the non-terminal just before.
These examples could also be used to test the grammar.
Using special comments {-# ... #-} however poses a problem with the BNFC boot-strap process. It defines its own grammar in LBNF and uses its own generated Alex/Happy parser. This parser currently drops all comments. We could add a new LBNF pragma similar to comment,
pragma MyPragma "{-#" "#-}"
that allows lexing of raw content between pragma delimiters. MyPragma could then be content of one of the declaration forms of LBNF.
It would be even nicer if comments would be preserved in a concrete syntax tree but this is a philosophy that is so different from current BNFC that it would warrant a new implementation.
A cheap solution would be a example pragma accepting a string with the example code.
example Exp "5 + 6 * 7";
To scale, we should have multi-line strings, though.