Theoretical Computer Science Stack Exchange is a question and answer site for theoretical computer scientists and researchers in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|cite|improve this question

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.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.