Grammar-based codes or grammar-based compression are compression algorithms based on the idea of constructing a context-free grammar (CFG) for the string to be compressed. Examples include universal lossless data compression algorithms.[1] To compress a data sequence x = x 1 ⋯ x n {\displaystyle x=x_{1}\cdots x_{n}} , a grammar-based code transforms x {\displaystyle x} into a context-free grammar G {\displaystyle G} . The problem of finding a smallest grammar for an input sequence (smallest grammar problem) is known to be NP-hard,[2] so many grammar-transform algorithms are proposed from theoretical and practical viewpoints. Generally, the produced grammar G {\displaystyle G} is further compressed by statistical encoders like arithmetic coding.
The class of grammar-based codes is very broad. It includes block codes, the multilevel pattern matching (MPM) algorithm,[3] variations of the incremental parsing Lempel-Ziv code,[4] and many other new universal lossless compression algorithms. Grammar-based codes are universal in the sense that they can achieve asymptotically the entropy rate of any stationary, ergodic source with a finite alphabet.
The compression programs of the following are available from external links.