In the last post, we saw how iLangGen text generation framework supports reuse of grammars through inheritance, akin to object-oriented languages. The good news is that we can achieve reuse through composition as well.
The following is a simple grammar, nothing fancy to elaborate.
Here is the output when you traverse the grammar without AST.
You can also obtain the AST as part of traversal.
The following grammar uses composition. You can see how the non-terminal y uses the end node of G1.
This technique allows us to make use of certain nodes of a grammar without duplicating the logic. Here is the output from this grammar.
The following grammar, by embedding start@G1, uses the entire output of G1 in its result.
Here is the output.
Things can get slightly more interesting; we can combine inheritance and composition. The following grammar G1V is a variant of G1 with the end node declared as virtual.
The grammar Derived derives from G1V, defines a new node variant and overrides end node.
Here is the output generated by Derived. Note that variant node has no effect because it does not appear in the generation path. However, it could be used in other scenarios (see next example).
Let us now define grammar G4, which binds to (composition) variant of G4.
When you run G4, you can see that it uses composition as well as inheritance!
In this way, i.e., through the use of inheritance and composition techniques, iLangGen facilitates building large grammar sets to model non-trivial text generation scenarios.
In the coming posts, we will continue to explore other interesting features of iLangGen.
Recent Comments