Lets say, we provide the algorithm a set of tokens.


x + y - z
x - x - x

It will then try to generate a CFG which fits all the provided examples

S -> S O T | T
T -> x | y | z
O -> + | -

It feels like a data compression problem but I could be wrong.

Does anybody know any existing literature or a starting point to solve this problem?

Does this problem have a name? What should I Google?


Any interesting grammar will generate an infinite language. There is always the trivial grammar with a start symbol that generates each example string on it's own. Checking if two grammars generate the same language, and thus in any sense finding "the smallest" (or whatever other measure you care about) grammar for a language is unsolvable. Inferring the grammar (in any meaningful sense) is probably impossible.


Your problem could not be solved, because production rules themselves could not define a formal language.

Below is the reason:

Given Σ is an alphabet set, Σ* is worlds over alphabet Σ, A formal language L is a subset of Σ*. Basing on your input, "x + y - z" or "x - x - x", Σ is ambiguous. Is it set {'x', '+', 'y' '-', 'z', ' '} or set {'x +', 'x -', 'x', '+ y', '-z ', ' '}?

A formal grammar is a tuple G = (N, Σ, P, S) and the P is build on N and Σ. With ambiguous Σ, N could not be defined (it must be disjointed with Σ) and P could not be built.

An example for this ambiguous is as link Minimal regular expression that matches a given set of words:

Regular expressions: /mãe|mae/ has Σ = {'mãe', 'mae'} whereas /ma[ãa]e/ has Σ = {'ma', 'ã', 'a', 'e'}, but both expressions generate same language.

You could define your alphabet set as Σ = {'x + y - z', 'x + y - z'} and end up with an un-useful rules like below, because of its huge quantities:

S ⟶ 'x + y - z'

S ⟶ 'x + y - z'


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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