{"id":637,"date":"2017-09-27T13:00:54","date_gmt":"2017-09-27T13:00:54","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=637"},"modified":"2017-09-27T13:02:12","modified_gmt":"2017-09-27T13:02:12","slug":"composition-of-grammars","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/","title":{"rendered":"Composition of Grammars"},"content":{"rendered":"<p>In the <a href=\"http:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/\" target=\"_blank\" rel=\"noopener\">last post<\/a>, we saw how <em><strong>iLangGen<\/strong><\/em> 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.<\/p>\n<p>The following is a simple grammar, nothing fancy to elaborate.<\/p>\n<figure id=\"attachment_638\" aria-describedby=\"caption-attachment-638\" style=\"width: 221px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"638\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1.png\" data-orig-size=\"221,100\" 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\/G1.png\" class=\"size-full wp-image-638\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1.png?resize=221%2C100\" alt=\"Simple Grammar\" width=\"221\" height=\"100\" \/><\/a><figcaption id=\"caption-attachment-638\" class=\"wp-caption-text\"><strong>Simple Grammar<\/strong><\/figcaption><\/figure>\n<p>Here is the output when you traverse the grammar without AST.<\/p>\n<figure id=\"attachment_639\" aria-describedby=\"caption-attachment-639\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"639\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g1-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output.png\" data-orig-size=\"580,71\" 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 from Grammar\" data-image-description=\"&lt;p&gt;Output from Grammar&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output from Grammar&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output.png\" class=\"size-full wp-image-639\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output.png?resize=580%2C71\" alt=\"Output from Grammar\" width=\"580\" height=\"71\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output.png?w=580&amp;ssl=1 580w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output.png?resize=300%2C37&amp;ssl=1 300w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/a><figcaption id=\"caption-attachment-639\" class=\"wp-caption-text\"><strong>Output from Grammar<\/strong><\/figcaption><\/figure>\n<p>You can also obtain the AST as part of traversal.<\/p>\n<figure id=\"attachment_640\" aria-describedby=\"caption-attachment-640\" style=\"width: 578px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output-ast.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"640\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g1-output-ast\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output-ast.png\" data-orig-size=\"578,71\" 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=\"AST Output\" data-image-description=\"&lt;p&gt;AST Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;AST Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output-ast.png\" class=\"size-full wp-image-640\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output-ast.png?resize=578%2C71\" alt=\"AST Output\" width=\"578\" height=\"71\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output-ast.png?w=578&amp;ssl=1 578w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1-output-ast.png?resize=300%2C37&amp;ssl=1 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/a><figcaption id=\"caption-attachment-640\" class=\"wp-caption-text\"><strong>AST Output<\/strong><\/figcaption><\/figure>\n<p>The following grammar uses composition. You can see how the non-terminal <em><strong>y<\/strong><\/em> uses the <em><strong>end<\/strong><\/em> node of <em><strong>G1<\/strong><\/em>.<\/p>\n<figure id=\"attachment_641\" aria-describedby=\"caption-attachment-641\" style=\"width: 198px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G2.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"641\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G2.png\" data-orig-size=\"198,84\" 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 Composition of Grammars\" data-image-description=\"&lt;p&gt;Simple Composition of Grammars&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Simple Composition of Grammars&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G2.png\" class=\"wp-image-641 size-full\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G2.png?resize=198%2C84\" alt=\"Simple Composition of Grammars\" width=\"198\" height=\"84\" \/><\/a><figcaption id=\"caption-attachment-641\" class=\"wp-caption-text\"><strong>Composition of Grammars<\/strong><\/figcaption><\/figure>\n<p>This technique allows us to make use of certain nodes of a grammar without duplicating the logic. Here is the output from this grammar.<\/p>\n<figure id=\"attachment_642\" aria-describedby=\"caption-attachment-642\" style=\"width: 584px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G2-output.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"642\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g2-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G2-output.png\" data-orig-size=\"584,58\" 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\/G2-output.png\" class=\"size-full wp-image-642\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G2-output.png?resize=584%2C58\" alt=\"Grammar Output\" width=\"584\" height=\"58\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G2-output.png?w=584&amp;ssl=1 584w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G2-output.png?resize=300%2C30&amp;ssl=1 300w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/a><figcaption id=\"caption-attachment-642\" class=\"wp-caption-text\"><strong>Grammar Output<\/strong><\/figcaption><\/figure>\n<p>The following grammar, by embedding <em><strong>start@G1<\/strong><\/em>, uses the entire output of <em><strong>G1<\/strong><\/em> in its result.<\/p>\n<figure id=\"attachment_643\" aria-describedby=\"caption-attachment-643\" style=\"width: 239px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G3.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"643\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G3.png\" data-orig-size=\"239,84\" 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=\"Embedding Entire Grammar\" data-image-description=\"&lt;p&gt;Embedding Entire Grammar&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Embedding Entire Grammar&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G3.png\" class=\"size-full wp-image-643\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G3.png?resize=239%2C84\" alt=\"Embedding Entire Grammar\" width=\"239\" height=\"84\" \/><\/a><figcaption id=\"caption-attachment-643\" class=\"wp-caption-text\"><strong>Embedding Entire Grammar<\/strong><\/figcaption><\/figure>\n<p>Here is the output.<\/p>\n<figure id=\"attachment_644\" aria-describedby=\"caption-attachment-644\" style=\"width: 584px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G3-output.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"644\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g3-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G3-output.png\" data-orig-size=\"584,76\" 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\/G3-output.png\" class=\"size-full wp-image-644\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G3-output.png?resize=584%2C76\" alt=\"Grammar Output\" width=\"584\" height=\"76\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G3-output.png?w=584&amp;ssl=1 584w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G3-output.png?resize=300%2C39&amp;ssl=1 300w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/a><figcaption id=\"caption-attachment-644\" class=\"wp-caption-text\"><strong>Grammar Output<\/strong><\/figcaption><\/figure>\n<p>Things can get slightly more interesting; we can combine inheritance and composition. The following grammar <em><strong>G1V<\/strong><\/em> is a variant of <em><strong>G1<\/strong><\/em> with the <em><strong>end<\/strong><\/em> node declared as <em><strong>virtual<\/strong><\/em>.<\/p>\n<figure id=\"attachment_645\" aria-describedby=\"caption-attachment-645\" style=\"width: 216px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1V.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"645\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g1v\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1V.png\" data-orig-size=\"216,119\" 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 Virtual Node\" data-image-description=\"&lt;p&gt;Grammar with Virtual Node&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Grammar with Virtual Node&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1V.png\" class=\"size-full wp-image-645\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G1V.png?resize=216%2C119\" alt=\"Grammar with Virtual Node\" width=\"216\" height=\"119\" \/><\/a><figcaption id=\"caption-attachment-645\" class=\"wp-caption-text\"><strong>Grammar with Virtual Node<\/strong><\/figcaption><\/figure>\n<p>The grammar <em><strong>Derived<\/strong><\/em> derives from <em><strong>G1V<\/strong><\/em>, defines a new node <em><strong>variant<\/strong><\/em> and <em><strong>overrides end<\/strong><\/em> node.<\/p>\n<figure id=\"attachment_646\" aria-describedby=\"caption-attachment-646\" style=\"width: 202px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/Derived.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"646\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/derived\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/Derived.png\" data-orig-size=\"202,78\" 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\/Derived.png\" class=\"size-full wp-image-646\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/Derived.png?resize=202%2C78\" alt=\"Derived Grammar\" width=\"202\" height=\"78\" \/><\/a><figcaption id=\"caption-attachment-646\" class=\"wp-caption-text\"><strong>Derived Grammar<\/strong><\/figcaption><\/figure>\n<p>Here is the output generated by <em><strong>Derived<\/strong><\/em>. Note that <em><strong>variant<\/strong><\/em> node has no effect because it does not appear in the generation path. However, it could be used in other scenarios (see next example).<\/p>\n<figure id=\"attachment_647\" aria-describedby=\"caption-attachment-647\" style=\"width: 623px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/Derived-output.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"647\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/derived-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/Derived-output.png\" data-orig-size=\"623,49\" 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\/Derived-output.png\" class=\"size-full wp-image-647\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/Derived-output.png?resize=623%2C49\" alt=\"Derived Grammar Output\" width=\"623\" height=\"49\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/Derived-output.png?w=623&amp;ssl=1 623w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/Derived-output.png?resize=300%2C24&amp;ssl=1 300w\" sizes=\"(max-width: 623px) 100vw, 623px\" \/><\/a><figcaption id=\"caption-attachment-647\" class=\"wp-caption-text\"><strong>Derived Grammar Output<\/strong><\/figcaption><\/figure>\n<p>Let us now define grammar <em><strong>G4<\/strong><\/em>, which binds to (composition) <em><strong>variant<\/strong><\/em> of <em><strong>G4<\/strong><\/em>.<\/p>\n<figure id=\"attachment_648\" aria-describedby=\"caption-attachment-648\" style=\"width: 226px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G4.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"648\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G4.png\" data-orig-size=\"226,45\" 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=\"Embedding Derived Grammar\" data-image-description=\"&lt;p&gt;Embedding Derived Grammar&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Embedding Derived Grammar&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G4.png\" class=\"size-full wp-image-648\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G4.png?resize=226%2C45\" alt=\"Embedding Derived Grammar\" width=\"226\" height=\"45\" \/><\/a><figcaption id=\"caption-attachment-648\" class=\"wp-caption-text\"><strong>Embedding Derived Grammar<\/strong><\/figcaption><\/figure>\n<p>When you run <em><strong>G4<\/strong><\/em>, you can see that it uses composition as well as inheritance!<\/p>\n<figure id=\"attachment_649\" aria-describedby=\"caption-attachment-649\" style=\"width: 591px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G4-output.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"649\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/27\/composition-of-grammars\/g4-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G4-output.png\" data-orig-size=\"591,54\" 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\/G4-output.png\" class=\"size-full wp-image-649\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G4-output.png?resize=591%2C54\" alt=\"Grammar Output\" width=\"591\" height=\"54\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G4-output.png?w=591&amp;ssl=1 591w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/G4-output.png?resize=300%2C27&amp;ssl=1 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/a><figcaption id=\"caption-attachment-649\" class=\"wp-caption-text\"><strong>Grammar Output<\/strong><\/figcaption><\/figure>\n<p>In this way, i.e., through the use of inheritance and composition techniques, <em><strong>iLangGen<\/strong><\/em> facilitates building large grammar sets to model non-trivial text generation scenarios.<\/p>\n<p>In the coming posts, we will continue to explore other interesting features of <em><strong>iLangGen<\/strong><\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&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],"tags":[109,108],"class_list":["post-637","post","type-post","status-publish","format-standard","hentry","category-lisp","category-natural-language-processing","tag-ilanggen","tag-text-generation"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-ah","jetpack-related-posts":[{"id":617,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/09\/13\/reuse-of-grammars-through-inheritance\/","url_meta":{"origin":637,"position":0},"title":"Reuse of Grammars Through Inheritance","author":"admin","date":"September 13, 2017","format":false,"excerpt":"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,\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\/image1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/09\/image1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1410,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/01\/27\/generating-poetry-using-ilanggen\/","url_meta":{"origin":637,"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":637,"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":2152,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/09\/28\/template-based-text-generation\/","url_meta":{"origin":637,"position":3},"title":"Template-Based Text Generation","author":"admin","date":"September 28, 2020","format":false,"excerpt":"I had written earlier about natural language generation\u00a0using my iLangGen framework. I used a \"template\" text file which was instantiated dynamically based on predefined \"grammars\" and external data. The sample application I show-cased demonstrated its utility and versatility. Today I would like to touch upon a few other \"pattern\" elements\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Template File","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template-300x195.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template-300x195.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/09\/Template-300x195.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":884,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/04\/08\/natural-language-generation\/","url_meta":{"origin":637,"position":4},"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":1659,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/08\/04\/generating-poetry-in-prolog\/","url_meta":{"origin":637,"position":5},"title":"Generating Poetry in Prolog","author":"admin","date":"August 4, 2019","format":false,"excerpt":"In an earlier article, I showed how we can generate poetry (with limitations, of course!) using my iLangGen framework. That implementation (in Lisp) made use of iLexicon, a large dictionary of English words, which I have been building over the years. I subsequently ported iLexicon to Prolog and it now\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Generation Logic","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/08\/Code3.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/08\/Code3.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/08\/Code3.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/637","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=637"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}