# Does there exist an algorithm to generate the production rules of CFG, given a sample production?

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

e.g.

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?

• – D.W.
Feb 16 '20 at 4:50
• Thanks. This is what I was looking for. Feb 16 '20 at 8:25

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'