{"id":617,"date":"2017-09-13T05:33:05","date_gmt":"2017-09-13T05:33:05","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=617"},"modified":"2017-09-13T13:02:33","modified_gmt":"2017-09-13T13:02:33","slug":"reuse-of-grammars-through-inheritance","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/","title":{"rendered":"Reuse of Grammars Through Inheritance"},"content":{"rendered":"<p>We are familiar with the advantages of class inheritance in object-oriented languages such as C++, C#, Java, and Python. The ability to reuse functionality via inheritance allows us to express our software design optimally, without having to write redundant code.<\/p>\n<p><em><strong>iLangGen<\/strong><\/em> encourages the reuse of grammars by supporting <em><strong>Grammar Inheritance<\/strong><\/em>, where a grammar can explicitly derive from another (base) grammar. As of now, only single inheritance is supported (i.e. it is not possible derive from more than one base grammar). A derived grammar can make use of the rules (or productions) of the base grammar, and in some cases, can <em><strong>override<\/strong><\/em> base grammar productions. Let us look at the various possibilities.<\/p>\n<p>Consider the following grammar:<\/p>\n<figure id=\"attachment_618\" aria-describedby=\"caption-attachment-618\" style=\"width: 591px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image1.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"618\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image1.png\" data-orig-size=\"591,132\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Simple Grammar\" data-image-description=\"&lt;p&gt;Simple Grammar&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Simple Grammar&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image1.png\" class=\"size-full wp-image-618\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image1.png?resize=591%2C132\" alt=\"Simple Grammar\" width=\"591\" height=\"132\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image1.png?w=591&amp;ssl=1 591w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image1.png?resize=300%2C67&amp;ssl=1 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/a><figcaption id=\"caption-attachment-618\" class=\"wp-caption-text\"><strong>A Simple Grammar<\/strong><\/figcaption><\/figure>\n<p>When we run this grammar, we get the following output:<\/p>\n<figure id=\"attachment_619\" aria-describedby=\"caption-attachment-619\" style=\"width: 646px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image2.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"619\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image2.png\" data-orig-size=\"646,101\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Output of SimpleGrammar\" data-image-description=\"&lt;p&gt;Output of SimpleGrammar&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output of SimpleGrammar&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image2.png\" class=\"size-full wp-image-619\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image2.png?resize=646%2C101\" alt=\"Output of SimpleGrammar\" width=\"646\" height=\"101\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image2.png?w=646&amp;ssl=1 646w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image2.png?resize=300%2C47&amp;ssl=1 300w\" sizes=\"(max-width: 646px) 100vw, 646px\" \/><\/a><figcaption id=\"caption-attachment-619\" class=\"wp-caption-text\"><strong>Output Produced by the Grammar<\/strong><\/figcaption><\/figure>\n<p>We now <em><strong>derive<\/strong><\/em> another grammar from this grammar:<\/p>\n<figure id=\"attachment_620\" aria-describedby=\"caption-attachment-620\" style=\"width: 606px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image3.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"620\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image3.png\" data-orig-size=\"606,90\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Derived Grammar\" data-image-description=\"&lt;p&gt;Derived Grammar&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Derived Grammar&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image3.png\" class=\"size-full wp-image-620\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image3.png?resize=606%2C90\" alt=\"Derived Grammar\" width=\"606\" height=\"90\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image3.png?w=606&amp;ssl=1 606w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image3.png?resize=300%2C45&amp;ssl=1 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/a><figcaption id=\"caption-attachment-620\" class=\"wp-caption-text\"><strong>Derived Grammar<\/strong><\/figcaption><\/figure>\n<p>As the syntax suggests, <em><strong>DerivedGrammar<\/strong><\/em> derives from <em><strong>BaseGrammar<\/strong><\/em>. What is interesting in this grammar is that it does not have definitions for the Non-terminals <em><strong>A, B<\/strong><\/em> and <em><strong>C<\/strong><\/em>. When you run this grammar, it <em><strong>inherits<\/strong><\/em> the relevant rules from its base grammar. See the output.<\/p>\n<figure id=\"attachment_621\" aria-describedby=\"caption-attachment-621\" style=\"width: 673px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image4.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"621\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image4.png\" data-orig-size=\"673,98\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Derived Grammar Output\" data-image-description=\"&lt;p&gt;Derived Grammar Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Derived Grammar Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image4.png\" class=\"size-full wp-image-621\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image4.png?resize=673%2C98\" alt=\"Derived Grammar Output\" width=\"673\" height=\"98\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image4.png?w=673&amp;ssl=1 673w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image4.png?resize=300%2C44&amp;ssl=1 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/a><figcaption id=\"caption-attachment-621\" class=\"wp-caption-text\"><strong>Derived Grammar Output<\/strong><\/figcaption><\/figure>\n<p>Let us now look at a slight variant of the <em><strong>BaseGrammar<\/strong><\/em>.<\/p>\n<figure id=\"attachment_622\" aria-describedby=\"caption-attachment-622\" style=\"width: 536px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image5.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"622\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image5.png\" data-orig-size=\"536,104\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Grammar without &amp;#8220;start&amp;#8221; Rule\" data-image-description=\"&lt;p&gt;Grammar without &amp;#8220;start&amp;#8221; Rule&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Grammar without &amp;#8220;start&amp;#8221; Rule&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image5.png\" class=\"size-full wp-image-622\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image5.png?resize=536%2C104\" alt=\"Grammar without &quot;start&quot; Rule\" width=\"536\" height=\"104\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image5.png?w=536&amp;ssl=1 536w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image5.png?resize=300%2C58&amp;ssl=1 300w\" sizes=\"(max-width: 536px) 100vw, 536px\" \/><\/a><figcaption id=\"caption-attachment-622\" class=\"wp-caption-text\"><strong>Grammar without &#8220;start&#8221; Rule<\/strong><\/figcaption><\/figure>\n<p>This grammar does not have a <em><strong>start<\/strong><\/em> production. Because of this, the grammar cannot be used directly. It can only be used in the context of a derived grammar. In this sense, it is like an <em><strong>abstract<\/strong> <strong>class<\/strong><\/em> in OO languages.<\/p>\n<p>The derived grammar defines the <em><strong>start<\/strong><\/em> symbol and uses the rules of the base grammar.<\/p>\n<figure id=\"attachment_623\" aria-describedby=\"caption-attachment-623\" style=\"width: 619px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image6.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"623\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image6\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image6.png\" data-orig-size=\"619,90\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Derived Grammar with &amp;#8220;start&amp;#8221; Rule\" data-image-description=\"&lt;p&gt;Derived Grammar with &amp;#8220;start&amp;#8221; Rule&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Derived Grammar with &amp;#8220;start&amp;#8221; Rule&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image6.png\" class=\"size-full wp-image-623\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image6.png?resize=619%2C90\" alt=\"Derived Grammar with &quot;start&quot; Rule\" width=\"619\" height=\"90\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image6.png?w=619&amp;ssl=1 619w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image6.png?resize=300%2C44&amp;ssl=1 300w\" sizes=\"(max-width: 619px) 100vw, 619px\" \/><\/a><figcaption id=\"caption-attachment-623\" class=\"wp-caption-text\"><strong>Derived Grammar with &#8220;start&#8221; Rule<\/strong><\/figcaption><\/figure>\n<p>Please note that in the earlier example, the base grammar was directly usable, but not in this example. In\u00a0 both the cases, the derived grammar makes use of the rules defined in the base grammar, exhibiting the power of inheritance. Of course, the derived grammar can define additional productions and use them if it chooses to. See this example:<\/p>\n<figure id=\"attachment_624\" aria-describedby=\"caption-attachment-624\" style=\"width: 639px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image7.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"624\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image7.png\" data-orig-size=\"639,123\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Derived Grammar with Additional Rules\" data-image-description=\"&lt;p&gt;Derived Grammar with Additional Rules&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Derived Grammar with Additional Rules&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image7.png\" class=\"size-full wp-image-624\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image7.png?resize=639%2C123\" alt=\"Derived Grammar with Additional Rules\" width=\"639\" height=\"123\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image7.png?w=639&amp;ssl=1 639w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image7.png?resize=300%2C58&amp;ssl=1 300w\" sizes=\"(max-width: 639px) 100vw, 639px\" \/><\/a><figcaption id=\"caption-attachment-624\" class=\"wp-caption-text\"><strong>Derived Grammar with Additional Rules<\/strong><\/figcaption><\/figure>\n<p>Here, <em><strong>D<\/strong><\/em> is an additional rule of the derived grammar. This is the output:<\/p>\n<figure id=\"attachment_625\" aria-describedby=\"caption-attachment-625\" style=\"width: 689px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image8.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"625\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image8\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image8.png\" data-orig-size=\"689,104\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Derived Grammar Output\" data-image-description=\"&lt;p&gt;Derived Grammar Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Derived Grammar Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image8.png\" class=\"size-full wp-image-625\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image8.png?resize=689%2C104\" alt=\"Derived Grammar Output\" width=\"689\" height=\"104\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image8.png?w=689&amp;ssl=1 689w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image8.png?resize=300%2C45&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image8.png?resize=680%2C104&amp;ssl=1 680w\" sizes=\"(max-width: 689px) 100vw, 689px\" \/><\/a><figcaption id=\"caption-attachment-625\" class=\"wp-caption-text\"><strong>Derived Grammar Output<\/strong><\/figcaption><\/figure>\n<p>The last variant of inheritance I want to discuss involves <em><strong>overriding<\/strong><\/em> productions. Just as we can declare a funtion to be <em><strong>virtual<\/strong><\/em> in C++, in <em><strong>iLangGen<\/strong><\/em> we can declare grammar productions as <em><strong>virtual<\/strong><\/em>, thus allowing a derived grammar to provide its <em><strong>overriding<\/strong><\/em> productions.<\/p>\n<p>Take alook at the following grammar:<\/p>\n<figure id=\"attachment_626\" aria-describedby=\"caption-attachment-626\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image9.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"626\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image9.png\" data-orig-size=\"650,153\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Grammar with &amp;#8220;virtual&amp;#8221; Rule\" data-image-description=\"&lt;p&gt;Grammar with &amp;#8220;virtual&amp;#8221; Rule&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Grammar with &amp;#8220;virtual&amp;#8221; Rule&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image9.png\" class=\"size-full wp-image-626\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image9.png?resize=650%2C153\" alt=\"Grammar with &quot;virtual&quot; Rule\" width=\"650\" height=\"153\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image9.png?w=650&amp;ssl=1 650w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image9.png?resize=300%2C71&amp;ssl=1 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-626\" class=\"wp-caption-text\"><strong>Grammar with &#8220;virtual&#8221; Rule<\/strong><\/figcaption><\/figure>\n<p>You will notice that production <em><strong>B<\/strong><\/em> has been declared <em><strong>virtual<\/strong><\/em>. It is an empty production, but in general, it could have non-empty RHS. Since this grammar has a <em><strong>start<\/strong><\/em> production, it can be run directly. This is what you get when you run the grammar:<\/p>\n<figure id=\"attachment_627\" aria-describedby=\"caption-attachment-627\" style=\"width: 662px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image10.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"627\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image10\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image10.png\" data-orig-size=\"662,106\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Grammar Output\" data-image-description=\"&lt;p&gt;Grammar Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Grammar Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image10.png\" class=\"size-full wp-image-627\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image10.png?resize=662%2C106\" alt=\"Grammar Output\" width=\"662\" height=\"106\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image10.png?w=662&amp;ssl=1 662w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image10.png?resize=300%2C48&amp;ssl=1 300w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/a><figcaption id=\"caption-attachment-627\" class=\"wp-caption-text\"><strong>Grammar Output<\/strong><\/figcaption><\/figure>\n<p>We get output as expected. Let us now derive a grammar from this grammar and <em><strong>override<\/strong><\/em> rule <em><strong>B<\/strong><\/em>.<\/p>\n<figure id=\"attachment_628\" aria-describedby=\"caption-attachment-628\" style=\"width: 607px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image11.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"628\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image11\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image11.png\" data-orig-size=\"607,85\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Grammar with Overriding Production\" data-image-description=\"&lt;p&gt;Grammar with Overriding Production&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Grammar with Overriding Production&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image11.png\" class=\"size-full wp-image-628\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image11.png?resize=607%2C85\" alt=\"Grammar with Overriding Production\" width=\"607\" height=\"85\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image11.png?w=607&amp;ssl=1 607w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image11.png?resize=300%2C42&amp;ssl=1 300w\" sizes=\"(max-width: 607px) 100vw, 607px\" \/><\/a><figcaption id=\"caption-attachment-628\" class=\"wp-caption-text\"><strong>Grammar with Overriding Production<\/strong><\/figcaption><\/figure>\n<p>When you run this grammar, you get the following output:<\/p>\n<figure id=\"attachment_629\" aria-describedby=\"caption-attachment-629\" style=\"width: 682px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image12.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"629\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/image12\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image12.png\" data-orig-size=\"682,163\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Output of Grammar\" data-image-description=\"&lt;p&gt;Output of Grammar&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output of Grammar&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image12.png\" class=\"size-full wp-image-629\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image12.png?resize=682%2C163\" alt=\"Output of Grammar\" width=\"682\" height=\"163\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image12.png?w=682&amp;ssl=1 682w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image12.png?resize=300%2C72&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image12.png?resize=680%2C163&amp;ssl=1 680w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/a><figcaption id=\"caption-attachment-629\" class=\"wp-caption-text\"><strong>Output of Grammar<\/strong><\/figcaption><\/figure>\n<p>You can see that the <em><strong>overridden<\/strong><\/em> production participates in the generated output. You can also notice that the derived class does not have a <em><strong>start<\/strong><\/em> rule. This is an interesting generation <em><strong>pattern<\/strong><\/em> where the base class decides the overall structure of the generated text and the derived class supplies appropriate components of the structure.<\/p>\n<p>This brings us to the end of today&#8217;s discussion on grammar inheritance in <em><strong>iLangGen<\/strong><\/em>. As you can see, this is a very powerful feature and will come in handy when we develop large grammars.<\/p>\n<p>In the next post, we shall look at <em><strong>Grammar Composition<\/strong><\/em>, yet another technique for building grammars.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are familiar with the advantages of class inheritance in object-oriented languages such as C++, C#, Java, and Python. The ability to reuse functionality via inheritance allows us to express our software design optimally, without having to write redundant code. iLangGen encourages the reuse of grammars by supporting Grammar Inheritance, where a grammar can explicitly [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18,107,17],"tags":[117,109,66,108],"class_list":["post-617","post","type-post","status-publish","format-standard","hentry","category-lisp","category-natural-language-processing","category-programming","tag-grammar","tag-ilanggen","tag-inheritance","tag-text-generation"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-9X","jetpack-related-posts":[{"id":637,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/","url_meta":{"origin":617,"position":0},"title":"Composition of Grammars","author":"admin","date":"September 27, 2017","format":false,"excerpt":"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\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Simple Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1410,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/01\/27\/generating-poetry-using-ilanggen\/","url_meta":{"origin":617,"position":1},"title":"Generating Poetry Using iLangGen","author":"admin","date":"January 27, 2019","format":false,"excerpt":"In an earlier article, I wrote about using iLangGen to generate natural language text. iLangGen is a powerful text generation library that I have been working on over the years. Today, I would like to show how we can use that library to generate \"poetry\". Be warned, however, that the\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Sample Output 2","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/01\/Output2.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":575,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/08\/06\/text-generation-using-ilanggen-framework\/","url_meta":{"origin":617,"position":2},"title":"Text Generation Using iLangGen Framework","author":"admin","date":"August 6, 2017","format":false,"excerpt":"The two primary areas in Natural Language processing are Natural Language Understanding and Natural Language Generation. The former is concerned with processing and making sense of natural language text, whereas the latter is concerned with synthesizing text, possibly from some deep representation. Both are fascinating and at the same time,\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"iLangGen Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/08\/Blog1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/08\/Blog1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/08\/Blog1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":884,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/04\/08\/natural-language-generation\/","url_meta":{"origin":617,"position":3},"title":"Natural Language Generation","author":"admin","date":"April 8, 2018","format":false,"excerpt":"I had written a series of posts on my iLangGen framework last year. It aims to provide a flexible and expressive approach for building natural language generation systems. In today's post, I would like to describe a concrete example of how iLangGen can be used for generating natural language text\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Overall Approach","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/04\/overall-1.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":653,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/10\/14\/using-function-calls-in-the-grammar\/","url_meta":{"origin":617,"position":4},"title":"Using Function Calls in the Grammar","author":"admin","date":"October 14, 2017","format":false,"excerpt":"One of the advanced features of iLangGen is the ability to embed arbitrary function calls on the RHS of grammar rules. Such functions impart interesting dynamic behaviour to the generated data. For convenience and flexibility, iLangGen supports two types of function calls - Direct and Indirect. In the case of\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Direct Function Call in Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/10\/Image1.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":585,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/08\/18\/repetition-in-grammar-rules\/","url_meta":{"origin":617,"position":5},"title":"Repetition in Grammar Rules","author":"admin","date":"August 18, 2017","format":false,"excerpt":"Many times, we need to repeatedly generate an element, or in general, have a way to control the number times one or more elements get generated. iLangGen supports all the standard cases: - Zero or once - Zero or more times (unbounded) - Zero or more times (bounded) - One\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Zero or Once","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/08\/Optional.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/08\/Optional.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/08\/Optional.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=617"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}