# context free – Counting spaces in grammars

Sometimes, in contextless language, we would like to require spaces between productions, and sometimes not. For example, take the following part of a grammar describing grammars:

``````production = lhs = ##; RHS.
lhs = not terminal.
rhs = non-terminal | terminal | rhs_rest.
rhs_rest = nonterminal rhs_rest | rhs_rest terminal | no.
non-terminal = tanks.
tanks = tank tanks | no.
terminal = "tank tanks".
``````

Note that I did not intentionally consider spaces. Now consider the input string:

``````abc = abc "def" aa aa bb "ab" "bc"
``````

then abc "def" aa contains three objects, nonterminal + terminal + nonterminal, even if a space does not separate them. However, abc must be interpreted as a single object, not for example ab + c. So we need a space between two non-terminal expressions, but not between two terminals or two non-terminal terminals and a terminal. It would have been easy if we only had pairs, but the rhs can hold many terminals and terminals. How to write a grammar that correctly accounts for required and non-required spaces?