Permalink
Browse files

W3C to BGF extractor in XSLT

git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@824 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information...
grammarware committed Sep 9, 2010
1 parent a67cedc commit 919eb2bf57f6f75a5b98e34a072718ac92f34e52
View
@@ -0,0 +1,27 @@
#!/bin/sh
LOCAL=${PWD}
cd `dirname $0`
cd ../..
SLPS=${PWD}
cd ${LOCAL}
if [ ! -r $1 ]; then
echo "Oops: $1 not found or not readable."
exit 1
elif [ $# -eq 2 ]; then
rm -f $2
/usr/local/bin/xsltproc ${SLPS}/topics/extraction/w3c/spec2bgf.xslt $1 > $1.tmp
perl -pi -w -e 's/<unknown>\)\*<\/unknown>/<unknown>\)<\/unknown><postfix-star\/>/g;' $1.tmp
perl -pi -w -e 's/<unknown>\)\?<\/unknown>/<unknown>\)<\/unknown><postfix-optional\/>/g;' $1.tmp
perl -pi -w -e 's/<unknown>\(<\/unknown>/<sequence><bgf:expression>/g;' $1.tmp
perl -pi -w -e 's/<unknown>\)<\/unknown>/<\/bgf:expression><\/sequence>/g;' $1.tmp
/usr/local/bin/xsltproc ${SLPS}/topics/extraction/w3c/postfix2confix.xslt $1.tmp > $2.tmp
/usr/local/bin/xsltproc ${SLPS}/topics/extraction/w3c/cleanup.xslt $2.tmp > $2
rm -f $1.tmp $2.tmp
else
echo "This tool transforms an XML document used by World Wide Web Consortium and produces a BGF grammar"
echo "Usage: spec2bgf <input-xml-document> <output-grammar>"
exit 2
fi
@@ -0,0 +1,8 @@
build:
test:
../../../shared/tools/spec2bgf ../../grammars/xpath/xpath1.xml xpath.bgf
../../../shared/tools/checkxml bgf xpath.bgf
clean:
rm -f *~ *.bgf
@@ -0,0 +1,73 @@
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bgf="http://planet-sl.org/bgf" version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/bgf:grammar">
<bgf:grammar xmlns:bgf="http://planet-sl.org/bgf">
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="local-name(.)='production'">
<xsl:apply-templates select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</bgf:grammar>
</xsl:template>
<xsl:template match="bgf:production">
<bgf:production>
<xsl:copy-of select="label"/>
<xsl:copy-of select="nonterminal"/>
<xsl:apply-templates select="bgf:expression"/>
</bgf:production>
</xsl:template>
<xsl:template match="bgf:expression">
<bgf:expression>
<xsl:choose>
<xsl:when test="choice">
<choice>
<xsl:apply-templates select="choice/*"/>
</choice>
</xsl:when>
<xsl:when test="sequence">
<sequence>
<xsl:apply-templates select="sequence/*"/>
</sequence>
</xsl:when>
<xsl:when test="count(*) &gt; 1 and not(unknown)">
<sequence>
<xsl:for-each select="*">
<bgf:expression>
<xsl:apply-templates select="."/>
</bgf:expression>
</xsl:for-each>
</sequence>
</xsl:when>
<xsl:when test="count(*) &gt; 1 and unknown = '|'">
<choice>
<xsl:for-each select="*">
<bgf:expression>
<xsl:apply-templates select="."/>
</bgf:expression>
</xsl:for-each>
</choice>
</xsl:when>
<xsl:when test="count(*) &gt; 1 and unknown != '|'">
<!-- the extractor gives up -->
<epsilon/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="*"/>
</xsl:otherwise>
</xsl:choose>
</bgf:expression>
</xsl:template>
<xsl:template match="nonterminal|terminal">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="unknown">
<!-- <xsl:value-of select="."/> -->
<epsilon/>
</xsl:template>
</xsl:stylesheet>
@@ -0,0 +1,68 @@
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bgf="http://planet-sl.org/bgf" version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/bgf:grammar">
<bgf:grammar xmlns:bgf="http://planet-sl.org/bgf">
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="local-name(.)='production'">
<xsl:apply-templates select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</bgf:grammar>
</xsl:template>
<xsl:template match="bgf:production">
<bgf:production>
<xsl:copy-of select="label"/>
<xsl:copy-of select="nonterminal"/>
<xsl:apply-templates select="bgf:expression"/>
</bgf:production>
</xsl:template>
<xsl:template match="bgf:expression">
<bgf:expression>
<xsl:choose>
<xsl:when test="choice">
<choice>
<xsl:apply-templates select="choice/*"/>
</choice>
</xsl:when>
<xsl:when test="sequence">
<sequence>
<xsl:apply-templates select="sequence/*"/>
</sequence>
</xsl:when>
<xsl:when test="postfix-optional or postfix-star">
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="local-name(following-sibling::*[1]) = 'postfix-optional'">
<optional>
<xsl:copy-of select="."/>
</optional>
</xsl:when>
<xsl:when test="local-name(following-sibling::*[1]) = 'postfix-star'">
<star>
<xsl:copy-of select="."/>
</star>
</xsl:when>
<xsl:when test="local-name(.) = 'postfix-optional'"/>
<xsl:when test="local-name(.) = 'postfix-star'"/>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="*"/>
</xsl:otherwise>
</xsl:choose>
</bgf:expression>
</xsl:template>
<xsl:template match="nonterminal|terminal|unknown">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
@@ -0,0 +1,113 @@
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bgf="http://planet-sl.org/bgf" version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/spec">
<bgf:grammar xmlns:bgf="http://planet-sl.org/bgf">
<xsl:for-each select="//scrap">
<xsl:comment>
<xsl:value-of select="head"/>
</xsl:comment>
<xsl:for-each select=".//prod">
<bgf:production>
<xsl:if test="@id">
<label>
<xsl:value-of select="@id"/>
</label>
</xsl:if>
<nonterminal>
<xsl:value-of select="lhs"/>
</nonterminal>
<xsl:choose>
<xsl:when test="count(rhs)=1">
<xsl:apply-templates select="rhs"/>
</xsl:when>
<xsl:otherwise>
<bgf:expression>
<choice>
<xsl:apply-templates select="rhs"/>
</choice>
</bgf:expression>
</xsl:otherwise>
</xsl:choose>
</bgf:production>
</xsl:for-each>
</xsl:for-each>
</bgf:grammar>
</xsl:template>
<xsl:template match="rhs">
<bgf:expression xmlns:bgf="http://planet-sl.org/bgf">
<xsl:for-each select="node()">
<xsl:choose>
<!-- regular nonterminal -->
<xsl:when test="local-name(.) = 'nt'">
<nonterminal>
<xsl:value-of select="."/>
</nonterminal>
</xsl:when>
<!-- externally defined nonterminal -->
<xsl:when test="local-name(.) = 'xnt'">
<nonterminal>
<xsl:value-of select="."/>
</nonterminal>
</xsl:when>
<xsl:when test=". = '?'">
<postfix-optional/>
</xsl:when>
<xsl:when test=". = '*'">
<postfix-star/>
</xsl:when>
<xsl:when test="substring(.,1,1) = '|'">
<xsl:call-template name="map-multiple-symbols">
<xsl:with-param name="list" select="substring(.,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="map-multiple-symbols">
<xsl:with-param name="list" select="."/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</bgf:expression>
</xsl:template>
<xsl:template name="map-one-symbol">
<xsl:param name="node"/>
<xsl:choose>
<!-- terminal -->
<xsl:when test="substring($node,1,1) = &quot;'&quot;">
<terminal>
<xsl:value-of select="substring-before(substring($node,2),&quot;'&quot;)"/>
</terminal>
</xsl:when>
<xsl:when test="substring($node,1,1) = ' '">
<leading-space>
<xsl:value-of select="substring($node,2)"/>
</leading-space>
<xsl:call-template name="map-multiple-symbols">
<xsl:with-param name="list" select="substring($node,2)"/>
</xsl:call-template>
</xsl:when>
<!-- empty string -->
<xsl:when test="$node = ''"/>
<xsl:otherwise>
<unknown>
<xsl:copy-of select="$node"/>
</unknown>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="map-multiple-symbols">
<xsl:param name="list"/>
<xsl:variable name="newlist" select="concat(normalize-space($list), ' ')"/>
<xsl:variable name="first" select="substring-before($newlist, ' ')"/>
<xsl:variable name="remaining" select="substring-after($newlist, ' ')"/>
<xsl:call-template name="map-one-symbol">
<xsl:with-param name="node" select="$first"/>
</xsl:call-template>
<xsl:if test="$remaining">
<xsl:call-template name="map-multiple-symbols">
<xsl:with-param name="list" select="$remaining"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
@@ -0,0 +1,12 @@
test:
../../../shared/tools/spec2bgf xpath1.xml xpath.bgf
../../../shared/tools/checkxml bgf xpath.bgf
get:
curl -k http://www.w3.org/TR/1999/REC-xpath-19991116.xml >xpath1.xml
curl -k http://www.w3.org/TR/2007/REC-xpath20-20070123/xpath20.xml >xpath2.xml
curl -k http://www.w3.org/XML/1998/06/xmlspec-v20.dtd > xmlspec-v20.dtd
perl -pi -w -e 's/\/XML\/1998\/06\/xmlspec-v20.dtd/xmlspec-v20.dtd/g;' xpath1.xml
clean:
rm -f *~ *.bgf
Oops, something went wrong.

0 comments on commit 919eb2b

Please sign in to comment.