The EBNF grammar for PNG/JNG/MNG datastreams (draft, version 0.4)

The EBNF grammar for PNG-1.2 datastreams is here: png.ebnf
The EBNF grammar for JNG-1.0 datastreams is here: jng.ebnf
The EBNF grammar for MNG-1.0 datastreams is here: mng.ebnf


Corrections and other changes:


Notes:


Testing:

You are welcome to test the grammar against valid MNGs, provided that, if there are embedded loops, their physical (not logical) nesting level is no bigger than 12.
gcc -o test_mnglex test_mnglex.c  # you should be able to use your favorite compiler
mnglist -c -s file.mng | test_mnglex
You can download test_mnglex.c (745K !!) or test_mnglex.c.gz (165K), or you can generate it by yourself (see below). The program mnglist.c is needed to list the chunk names of PNG/JNG/MNG files.
Do not be discouraged by the BIG size of test lexer - that's because it is just a dummy tester, and there is no optimization in it at all. Its size is due mainly to the expansion of mng_loop, which yields a very big number of DFA states. A much more efficient approach would be to translate the EBNF productions to a LR(1) or a LALR(1) grammar (e.g. YACC), rather than to a regular grammar.

How to generate the test lexer

The script to produce FLEX files is here: mkmnglex.pl. Please note that this script expands the recursive definition of mng_loop only 12 times, because FLEX would run out of NFA states otherwise.
# png.l and jng.l can be compiled as standalone parsers for PNG and JNG files
mkmnglex.pl png.ebnf > png.l
mkmnglex.pl jng.ebnf > jng.l
mkmnglex.pl png.ebnf jng.ebnf mng.ebnf > mng.l
flex -otest_mnglex.c mng.l


Last updated: Jan 29, 2001
Cosmin Truta