Context Free Grammars And Generating Functions
We saw how to get the number of ways to generate a string of length n, matching or avoiding certain patterns, from Regular Expressions (RE).
But it can't help when more expressive power is required. We'll see examples on how to use Context Free Grammars (CFG) to obtain the generating functions (GF), whose coefficients in the formal power series indicate the number of parse trees possible for n-letter string for the given CFG.
So, in a way, it can be used to know whether the grammar is ambiguous or not.
Let's look at relatively simple examples, for the strings constructed over the set of symbols \(\left\{\left(, \right)\right\}\)
1. Find the number of ways of constructing balanced parenthesesThe grammar is
Then, to get its GF, replace each symbol with \(x\), and \(\epsilon\) with 1. Hence, the GF for the RE is
and the series is the well known Catalan numbers.
2. Find the number of ways of constructing balanced parentheses, which can have more opening parenthesese.g. for \(n=3\), \((((, ((), ()(\) are valid
We may obtain the CFG as
Even though the grammar describes the language, it's actually ambiguous, and the GF obtained from this counts all the extra parse trees.
The right CFG is
and we derive the GF
Read Gruber, Lee & Shallit for the theory.