{"id":2710,"date":"2020-07-10T19:16:09","date_gmt":"2020-07-10T23:16:09","guid":{"rendered":"http:\/\/codinggorilla.com\/?p=2710"},"modified":"2020-07-14T06:48:43","modified_gmt":"2020-07-14T10:48:43","slug":"tree-transformations-via-xpath-and-s-expressions","status":"publish","type":"post","link":"http:\/\/165.227.223.229\/index.php\/2020\/07\/10\/tree-transformations-via-xpath-and-s-expressions\/","title":{"rendered":"Tree transformations via XPath and S-expressions"},"content":{"rendered":"\n<p>I&#8217;ve finally have the right tools to now implement transforms over an Antlr parse tree.<\/p>\n\n\n\n<p>The first part of a transform is identifying what nodes in the tree that are going to be replaced. It turns out that the best tool to do that is an XPath engine, which I&#8217;ve rewritten in C# from Java over the last month.<\/p>\n\n\n\n<p>The other part of a transform&#8211;and far simpler to implement&#8211;is a way to express a tree that is created and spliced into an existing parse tree. Here, the work I did in Piggy for S-expressions comes in handy. For example, the expression &#8220;( ruleAltList ( labeledAlt ( alternative { child } )))&#8221; identifies the Antlr parse tree to create, and splices in a &#8220;child&#8221; node into a created &#8220;alternative&#8221; node. Note, this is a slightly different usage for &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/S-expression\" class=\"ek-link\">S-expressions<\/a>&#8221; that you may be used to, in which a node is an unnamed pair, but conveys the same purpose.<\/p>\n\n\n\n<p>With XPath and S-expressions, I can now rewrite all the transforms that I hardcoded in C# for Antlrvsix. The code implementing both parts is <a href=\"https:\/\/github.com\/kaby76\/xpath-csharp\" class=\"ek-link\">here<\/a>, but I will be forking this code and placing it under in the <a href=\"https:\/\/github.com\/kaby76\/AntlrVSIX\" class=\"ek-link\">Antlrvsix source<\/a> until I see a need to place this code in a separate Nuget package.<\/p>\n\n\n\n<p>At this moment, I&#8217;m not exactly sure what language and control structures to add on top of XPath and S-expressions. For now, these two tools should suffice along with C# to glue the pieces together in order to modify an Antlr parse tree. The other consideration is having intermediate results of an XPath expression. For example, I may want to get all ruleAltList&#8217;s but continue down the tree for a particular child. I&#8217;ve fixed a bug in the Eclipse XPath library to allow an intermediate result to be used as context for another XPath expression. But I might consider extending XPath to bind the results of an intermediate result into a C# variable.<\/p>\n\n\n\n<p>One other note&#8211;Why am I not looking at <a href=\"https:\/\/en.wikipedia.org\/wiki\/Category:Term-rewriting_programming_languages\" class=\"ek-link\">term rewriting systems<\/a>? I have. The problem is that they are not practical for two reasons: (1) integrating it with Antlr parse trees would not be easy; (2) most do not express manipulations directly on a tree. XSLT is one example. Here, the language isn&#8217;t specifying tree rewrites, but the construction of an entirely new tree. I also looked at <a href=\"http:\/\/www.txl.ca\/\" class=\"ek-link\">TXL<\/a>. Here, the language isn&#8217;t about trees, but term rewrites in the target language. I would need to convert the Antlr grammar into TXL grammar syntax.  In all these systems, I would need to fit in Antlr parse trees into the framework. Again, all I want is to manipulate trees.<\/p>\n\n\n\n<p>What of <a href=\"https:\/\/github.com\/kaby76\/Piggy\" class=\"ek-link\">Piggy<\/a>? Unfortunately, Piggy is not a tree editing library. While it recognizes tree nodes, the problem is that it then executes user code that performs an output on a DFS traversal of the tree. Again, all I want is to manipulate an existing tree, then do something later with that tree.<\/p>\n\n\n\n<p>&#8211;Ken<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve finally have the right tools to now implement transforms over an Antlr parse tree. The first part of a transform is identifying what nodes in the tree that are going to be replaced. It turns out that the best tool to do that is an XPath engine, which I&#8217;ve rewritten in C# from Java &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/165.227.223.229\/index.php\/2020\/07\/10\/tree-transformations-via-xpath-and-s-expressions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Tree transformations via XPath and S-expressions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/2710"}],"collection":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/comments?post=2710"}],"version-history":[{"count":0,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/posts\/2710\/revisions"}],"wp:attachment":[{"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/media?parent=2710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/categories?post=2710"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/165.227.223.229\/index.php\/wp-json\/wp\/v2\/tags?post=2710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}