Composition of Grammars

Written by on September 27, 2017 in LISP, Natural Language Processing with 0 Comments

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.

Simple Grammar

Simple Grammar

Here is the output when you traverse the grammar without AST.

Output from Grammar

Output from Grammar

You can also obtain the AST as part of traversal.

AST Output

AST Output

The following grammar uses composition. You can see how the non-terminal y uses the end node of G1.

Simple Composition of Grammars

Composition of Grammars

This technique allows us to make use of certain nodes of a grammar without duplicating the logic. Here is the output from this grammar.

Grammar Output

Grammar Output

The following grammar, by embedding start@G1, uses the entire output of G1 in its result.

Embedding Entire Grammar

Embedding Entire Grammar

Here is the output.

Grammar Output

Grammar 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.

Grammar with Virtual Node

Grammar with Virtual Node

The grammar Derived derives from G1V, defines a new node variant and overrides end node.

Derived Grammar

Derived Grammar

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).

Derived Grammar Output

Derived Grammar Output

Let us now define grammar G4, which binds to (composition) variant of G4.

Embedding Derived Grammar

Embedding Derived Grammar

When you run G4, you can see that it uses composition as well as inheritance!

Grammar Output

Grammar Output

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.

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top