<p>There is an international agreed notation for describing protocols and format specifications
(well actually there are several), which is <a href="http://www.itu.int/en/ITU-T/asn1/Pages/introduction.aspx" rel="nofollow">ASN.1</a>. It is a form of Context_free grammar notation.</p>

<p>I used to teach formal notations for networking at one time, but these things have dropped out of the curriculum in favour of security ; which is interesting when a better understanding of the formalism might help enhance security.</p>

<p>It is possible to generate code for handling protocols; and some is done that way. Using state machines for the protocol and notations like ASN.1 for the packet descriptions much can be automated, and has been in the past.</p>

CC BY-SA 3.0
2015-06-12T08:51:41.393Z
The description of networking protocols is typically given by [RFCs](https://www.rfc-editor.org/rfc-index.html), and RFCs are written in a way such that you can automatically extract the relevant ASN.1 grammars and generate parsers for them.
Martin Berger
CC BY-SA 3.0
2015-06-12T08:30:35.257Z
2015-06-12T08:30:35.257Z
Brian Tompsett - 汤莱恩
<p>For network protocols and/or file formats with fixed length fields, the grammar is fairly simple, and can be explained with a regular expression.</p>

<p>However, for protocols with varying data lenghts, there is almost always a fixed-length field preceding the data that gives the length of that data.</p>

<p>Without going into details of protocol/format specifications, we can talk about its equivalents in grammar notation. For example, consider the following production rule:</p>

<pre><code>A -&gt; anb^n
</code></pre>

<p>where 'a' is some fixed-length symbol, 'n' is a non-negative integer, and b^n is b repeated n times.</p>

<p>I'm having trouble understanding how such a rule can capture the fact that the value of 'n' is somehow related to how many b's are present in the sentence.</p>

<p>Where in the Chomsky hierarchy do such grammars lie? Also is it possible to automatically generate parsers for such grammars?</p>

<grammars><parsing>
What is the grammar of network protocols and file formats?
Fi Zixer
# What is the grammar of network protocols and file formats?

(Asked by: username_0 on 2015-06-12)

For network protocols and/or file formats with fixed length fields, the grammar is fairly simple, and can be explained with a regular expression.
However, for protocols with varying data lenghts, there is almost always a fixed-length field preceding the data that gives the length of that data.
Without going into details of protocol/format specifications, we can talk about its equivalents in grammar notation. For example, consider the following production rule:
A -> anb^n

where 'a' is some fixed-length symbol, 'n' is a non-negative integer, and b^n is b repeated n times.
I'm having trouble understanding how such a rule can capture the fact that the value of 'n' is somehow related to how many b's are present in the sentence.
Where in the Chomsky hierarchy do such grammars lie? Also is it possible to automatically generate parsers for such grammars?

**Tags:** grammars, parsing


---

## 1 answers

### Answer by username_1 on 2015-06-12. Score: 4

There is an international agreed notation for describing protocols and format specifications
(well actually there are several), which is [ASN.1](http://www.itu.int/en/ITU-T/asn1/Pages/introduction.aspx). It is a form of Context\_free grammar notation.

I used to teach formal notations for networking at one time, but these things have dropped out of the curriculum in favour of security ; which is interesting when a better understanding of the formalism might help enhance security.

It is possible to generate code for handling protocols; and some is done that way. Using state machines for the protocol and notations like ASN.1 for the packet descriptions much can be automated, and has been in the past.

> **Comments:**
> **username_2**: The description of networking protocols is typically given by [RFCs](https://www.rfc-editor.org/rfc-index.html), and RFCs are written in a way such that you can automatically extract the relevant ASN.1 grammars and generate parsers for them.  

---

