<p>For brevity, let's redefine a grammar to be just the three-tuple <span class="math-container">$(\Sigma_G, P_G, S_G)$</span>.</p>
<p>As usual, <span class="math-container">$\Sigma_G$</span> is the alphabet, <span class="math-container">$P_G$</span> are the production rules and <span class="math-container">$S_G$</span> is the start rule of <span class="math-container">$G$</span>.</p>
<p>For our &quot;special&quot; instance of a grammar, which we call <span class="math-container">$X$</span>:</p>
<p>Let's define our alphabet <span class="math-container">$\Sigma_X$</span> to contain the metasymbols</p>
<blockquote>
<p>or rather &quot;symbols that are indistinguishable to whoever will be parsing the definition&quot;</p>
</blockquote>
<p>themselves used to define grammars in the first place:
<span class="math-container">$$\{\text{ }\pmb{\{},\text{ }\pmb{,}\text{ },\pmb{\}}, etc\}$$</span></p>
<p>Then, we can define <span class="math-container">$P_X$</span> as <span class="math-container">$\{S_X \to \pmb{(}\Sigma_X\pmb{,}P_X\pmb{,}S_X\pmb{)}\}$</span></p>
<p>Of course, <span class="math-container">$\Sigma_X$</span> actually expands to all the required metasymbols to define <span class="math-container">$X$</span> itself <strong>and</strong> any other alphabetic symbols used in the production rules of <span class="math-container">$X$</span>.</p>
<p>As one can see, <span class="math-container">$P_X$</span> is not only a recursive definition, but it never meets an end.</p>
<p>Are there cleverer ways to construct a self-replicating grammar?</p>
<p><em>By self-replicating grammar, I mean a grammar whose only recognisable language is that which contains only the string which is used to define it in the first place.</em></p>

CC BY-SA 4.0
2020-07-01T13:28:46.620Z
I can’t say I understand the question, but it seems you are trying to build a [quine](https://en.wikipedia.org/wiki/Quine_(computing)).
Emil Jeřábek
CC BY-SA 4.0
2020-07-01T13:44:52.677Z
I'll look into those. Essentially, I'm trying to define a grammar that generates it's own definition, if that makes sense.
Novicegrammer
CC BY-SA 4.0
2020-07-01T14:23:21.297Z
@EmilJeřábek these are pretty much what I was looking for, thank you
Novicegrammer
<grammars>
Self Generating Grammars - Does it have to be infinite recursion?
Novicegrammer
# Self Generating Grammars - Does it have to be infinite recursion?

(Asked by: username_0 on 2020-06-30)

For brevity, let's redefine a grammar to be just the three-tuple $(\Sigma\_G, P\_G, S\_G)$.

As usual, $\Sigma\_G$ is the alphabet, $P\_G$ are the production rules and $S\_G$ is the start rule of $G$.

For our "special" instance of a grammar, which we call $X$:

Let's define our alphabet $\Sigma\_X$ to contain the metasymbols

> or rather "symbols that are indistinguishable to whoever will be parsing the definition"

themselves used to define grammars in the first place:
$$\{\text{ }\pmb{\{},\text{ }\pmb{,}\text{ },\pmb{\}}, etc\}$$

Then, we can define $P\_X$ as $\{S\_X \to \pmb{(}\Sigma\_X\pmb{,}P\_X\pmb{,}S\_X\pmb{)}\}$

Of course, $\Sigma\_X$ actually expands to all the required metasymbols to define $X$ itself **and** any other alphabetic symbols used in the production rules of $X$.

As one can see, $P\_X$ is not only a recursive definition, but it never meets an end.

Are there cleverer ways to construct a self-replicating grammar?

*By self-replicating grammar, I mean a grammar whose only recognisable language is that which contains only the string which is used to define it in the first place.*

**Tags:** grammars

> **Comments:**
> **username_1**: I can’t say I understand the question, but it seems you are trying to build a [quine](https://en.wikipedia.org/wiki/Quine_(computing)).  
> **username_0**: I'll look into those. Essentially, I'm trying to define a grammar that generates it's own definition, if that makes sense.  
> **username_0**: @EmilJeřábek these are pretty much what I was looking for, thank you  

---

## 0 answers

