<p>The language of valid Bencoded values is <em>not</em> context-free&mdash;and nor is just the language of Bencoded strings. Let $L$ be the set of valid Bencoded values, and let $L&#39;$ be the intersection of $L$ with the regular language <code>[0-9]*:a*</code>, i.e. the Bencoded strings with just the symbol <code>a</code>. If $L$ were context-free, so would $L&#39;$, but $L&#39;$ does not satisfy the pumping lemma.</p>

<p>Suppose $L&#39;$ is context-free, the pumping lemma applies. Let $n$ be the pumping threshold and let $w = \mathrm{dec}(n):a^n$, where $\mathrm{dec}(n)$ denotes $n$ written in decimal. Then $w = \alpha\beta\gamma\delta\epsilon$, and $\alpha\beta^m\gamma\delta^m\epsilon \in L&#39;$ for all $m$. The colon must appear in $\gamma$ (the other cases can easily contradicted). Then the number to the left of the colon grows exponentially in $m$ (since its length in decimal increases linearly), but the string to the right only grows linearly, so the number can't be equal to its length.</p>

<p>You said</p>

<blockquote>
  <p>If you have a trivial function that converts the ASCII “5” into a unary value and all the other ASCII characters into a character then you have an equivalent structure.</p>
</blockquote>

<p>but it turns out that this function is not trivial from the perspective of context-free grammars.</p>

CC BY-SA 3.0
2012-01-08T16:01:34.737Z
2012-01-08T16:01:34.737Z
Max
<p>Bencoding is the encoding scheme used by Bittorrent applications. You’re probably most familiar with bencoding via the <code>.torrent</code> file format used by Bittorrent applications. It’s a simple encoding scheme, so I am wondering if a context-free grammar could be constructed for it. There are a couple of reasons I think  it cannot: </p>

<ol>
<li><p>The location of terminals depends on the ‘interpreted’ value of certain length-prefixed values.</p></li>
<li><p>Terminals can be present inside of non-terminal sequences, but in principal (i.e., in well-formed Bencodes) are not ever encountered by a parser except for verification maybe. In fact, entire Bencodes (in theory) could be present as values of, let’s say, a list element of a Bencode, but in such a way that they’re meant to be interpreted as “data” and not as something to be parsed or transformed by a rule. It’s ever-so-slightly more subtle than normal recursion as far as I can tell…</p></li>
</ol>

<p><strong>Update</strong>: When thinking about this I realized something odd whereby one could convert a context-dependent grammar into a context-free grammar; i.e., consider the problem of length-prefixed values such as appear in Bencode. For convenience I will use Bencode’s syntax; so let’s consider the string <code>5:hello</code>. If you have a trivial function that converts the ASCII “5” into a unary value and all the other ASCII characters into a character then you have an equivalent structure.</p>

<p>Viz. <code>5:hello</code> becomes <code>olleh:hello</code>; I like the idea of reversing the string because then you could easily construct another function to map the string back onto the ASCII numeral (after appropriate checks have been made that the string is in fact the length reported as to not skew semantics; I am not considering non-conforming strings, since we are talking about grammar...).</p>

<p>This is of course a very simple case of dependent grammar, but I wonder 1.) if anybody else has noticed this (I am sure), and 2.) if this could resolve some other context-dependent situations.</p>

<fl.formal-languages><context-free><encoding>
Can Bencodes Be Described With a Context-Free Grammar?
Ryan Hayes
# Can Bencodes Be Described With a Context-Free Grammar?

(Asked by: username_0 on 2012-01-07)

Bencoding is the encoding scheme used by Bittorrent applications. You’re probably most familiar with bencoding via the `.torrent` file format used by Bittorrent applications. It’s a simple encoding scheme, so I am wondering if a context-free grammar could be constructed for it. There are a couple of reasons I think it cannot:

1. The location of terminals depends on the ‘interpreted’ value of certain length-prefixed values.
2. Terminals can be present inside of non-terminal sequences, but in principal (i.e., in well-formed Bencodes) are not ever encountered by a parser except for verification maybe. In fact, entire Bencodes (in theory) could be present as values of, let’s say, a list element of a Bencode, but in such a way that they’re meant to be interpreted as “data” and not as something to be parsed or transformed by a rule. It’s ever-so-slightly more subtle than normal recursion as far as I can tell…

**Update**: When thinking about this I realized something odd whereby one could convert a context-dependent grammar into a context-free grammar; i.e., consider the problem of length-prefixed values such as appear in Bencode. For convenience I will use Bencode’s syntax; so let’s consider the string `5:hello`. If you have a trivial function that converts the ASCII “5” into a unary value and all the other ASCII characters into a character then you have an equivalent structure.

Viz. `5:hello` becomes `olleh:hello`; I like the idea of reversing the string because then you could easily construct another function to map the string back onto the ASCII numeral (after appropriate checks have been made that the string is in fact the length reported as to not skew semantics; I am not considering non-conforming strings, since we are talking about grammar...).

This is of course a very simple case of dependent grammar, but I wonder 1.) if anybody else has noticed this (I am sure), and 2.) if this could resolve some other context-dependent situations.

**Tags:** fl.formal-languages, context-free, encoding

---

## 1 answers

### Answer by username_1 on 2012-01-08. Score: 7

The language of valid Bencoded values is *not* context-free—and nor is just the language of Bencoded strings. Let $L$ be the set of valid Bencoded values, and let $L'$ be the intersection of $L$ with the regular language `[0-9]*:a*`, i.e. the Bencoded strings with just the symbol `a`. If $L$ were context-free, so would $L'$, but $L'$ does not satisfy the pumping lemma.

Suppose $L'$ is context-free, the pumping lemma applies. Let $n$ be the pumping threshold and let $w = \mathrm{dec}(n):a^n$, where $\mathrm{dec}(n)$ denotes $n$ written in decimal. Then $w = \alpha\beta\gamma\delta\epsilon$, and $\alpha\beta^m\gamma\delta^m\epsilon \in L'$ for all $m$. The colon must appear in $\gamma$ (the other cases can easily contradicted). Then the number to the left of the colon grows exponentially in $m$ (since its length in decimal increases linearly), but the string to the right only grows linearly, so the number can't be equal to its length.

You said

> If you have a trivial function that converts the ASCII “5” into a unary value and all the other ASCII characters into a character then you have an equivalent structure.

but it turns out that this function is not trivial from the perspective of context-free grammars.


---

