Friday, March 31, 2006
Monday, March 27, 2006
XML Macros
There are many complex things which can be done with XSLT, and because of this, many people overlook the very simple things which XSLT can also provide. Previously, I discussed XSLT page compilers, but there is a technique buried in that idea which I thought should get some more light, so I'm presenting it here as it's own thing.
Due to the way that XSLT processing descends, it is possible to use the XSLT identity transform in adition to some simple templates to write XSLT transformations which behave very similarly to hygenic macros (possibly identically, but my Scheme is still weak, so I'm avoiding that statement). You can extend existing XML languages with new elements, and use simple XSLT documents to resolve those elements down to the base language which you are extending.
This often proves to be a powerful technique, especially when designing new languages, as a macro resolving transformation can be performed on your new language before the transformation which provides its semantics, dramatically reducing the implementation complexity. The basic approach resolves arround that old workhorse of XSLT hackers, the identity transform:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
! This transform provides a guide towards defining
! XML macros using XSLT. We expand only our XML
! extensions, and preserve everything else in a
! document.
+-->
<xsl:template match="node()">
<!--
! This is an identity template in XSLT. However,
! due to the very low specificity (a technical
! aspect of XSLT) of the match, other templates
! will override this one when they match.
+-->
<xsl:copy>
<xsl:for-each select="attribute::*">
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="bi">
<!--
! This defines the element 'bi',
! which is both bold and italic.
+-->
<b><i>
<xsl:apply-templates/>
</i></b>
</xsl:template>
<xsl:template match="def">
<!--
! This defines the element 'def',
! which provides a link to search the web
! for the definition of a term.
+-->
<a href="http://google.com/search?q=define%3A{.}">
<xsl:value-of select='.'/>
</a>
</xsl:template>
</xsl:transform>
Which permits us to transform this input document, which isn't quite HTML:
<html> <head> <title>Sample</title> </head> <body> Here is <bi>my</bi> page, and I like <def>monkeys</def>. </body> </html>Into this output document, which is pure HTML:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Sample</title> </head> <body> Here is <b><i>my</i></b> page, and I like <a href="http://google.com/search?q=define%3Amonkeys">monkeys</a>. </body> </html>
Tuesday, March 07, 2006
List Resolution Techniques for Grammar Development
This is a paper I wrote a while back, cribbed together from a utility fragment of my thesis. It doesn't present anything new, but I think it does a good job of communicating some techniques which are quite useful in practical grammar developmen, a subject which I have always thought was to little discussed. It is only three pages, and I think it is pretty easy to read. I would post it here, but I'm too lazy to try and translate LaTeX into blogger.
Update: I've messed about with latex2html, and I've uploaded an html version to:
http://littlelanguages.com/web/crutcher/lrtgd/index.html
Abstract: Most popular parser generation tools work in terms of grammar rules which are not directly capable of providing list semantics. While the basic techniques for realizing list semantics are frequently re-invented, the literature has suffered from a lack of a solid collection of these techniques. This paper presents a collection of techniques for realizing various forms of lists as grammatically re-written forms using AST re-writes compatible with most parser generation environments. Also provided is a set of additional EBNF extension operators for specifying AST re-write actions.
Friday, March 03, 2006
A Pattern Language for Language Implementation
Abstract. Programming languages are typically considered to be difficult to implement. However, a programming language tailored to an application domain can be an extremely powerful productivity enhancer. We present a pattern language for implementing languages that gathers together many ideas that are known in the language implementation community that should be more widely known. By applying this pattern language, productivity can be increased by the blossoming of more programming languages tailored to a specific purpose. We begin by discussing some of the dichotomies that shape the language design process. The pattern language itself consists of patterns describing various language flavors, and patterns for doing syntactic recognition, evaluation, and source production.
