After looking for a text generator for grammars I found this
https://github.com/dmbaturin/bnfgen and then to try understand the underlying algorithm I came out with a C simple program for a simple expr grammar see it here
https://github.com/dmbaturin/bnfgen/issues/2#issuecomment-89... and also a thread on sqlite forum here
https://sqlite.org/forum/forumpost/6885cf9e21