Skip to content

Commit

Permalink
some more transformation tools for BGF
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@683 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Oct 1, 2009
1 parent b292b66 commit d36a90e
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 6 deletions.
23 changes: 23 additions & 0 deletions shared/tools/bgf2sdf
@@ -0,0 +1,23 @@
#!/bin/sh

LOCAL=${PWD}
cd `dirname $0`
cd ../..
SLPS=${PWD}
cd ${LOCAL}

if [ $# -eq 1 ]; then
OUTPUT=/dev/stdout
elif [ $# -ne 2 ]; then
echo "This tool pretty-prints BGF, generating an SDF."
echo "Usage: bgf2sdf <input-bgf-document> [<output-sdf-definition>]"
echo "When output file is not specified, stdout is used."
exit 1
elif [ ! -r $1 ]; then
echo "Oops: $1 not found or not readable."
exit 1
else
OUTPUT=$2
fi

xsltproc ${SLPS}/shared/xsl/bgf2sdf.xslt $1> ${OUTPUT}
24 changes: 24 additions & 0 deletions shared/tools/xbgf2html
@@ -0,0 +1,24 @@
#!/bin/sh

LOCAL=${PWD}
cd `dirname $0`
cd ../..
SLPS=${PWD}
cd ${LOCAL}

if [ $# -eq 1 ]; then
OUTPUT=/dev/stdout
elif [ $# -ne 2 ]; then
echo "This tool transforms Transformational BGF documents to hypertext EBNF."
echo "Usage: xbgf2html <input-xbgf-document> [<output-hypertex>]"
echo "When output file is not specified, stdout is used."
exit 1
else
OUTPUT=$2
fi
if [ ! -r $1 ]; then
echo "Oops: $1 not found or not readable."
exit 1
fi

xsltproc ${SLPS}/shared/xsl/xbgf2xhtml.xslt $1 | grep -v 'DOCTYPE' > ${OUTPUT}
36 changes: 36 additions & 0 deletions shared/xsl/bgf2dot.xslt
@@ -0,0 +1,36 @@
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:bgf="http://planet-sl.org/bgf"
xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xsl:output
method="text"
encoding="UTF-8"
omit-xml-declaration="yes"
/>

<xsl:template match="/bgf:grammar">
<xsl:text>digraph generated{</xsl:text>
<xsl:for-each select="./bgf:production">
<xsl:call-template name="findDependencies">
<xsl:with-param name="start" select="./nonterminal"/>
<xsl:with-param name="traverse" select="./bgf:expression"/>
</xsl:call-template>
</xsl:for-each>
<xsl:text>}</xsl:text>
</xsl:template>

<xsl:template name="findDependencies">
<xsl:param name="start"/>
<xsl:param name="traverse"/>
<xsl:for-each select="$traverse//nonterminal">
<xsl:if test=".!=$start">
<xsl:value-of select="$start"/>
<xsl:text>-></xsl:text>
<xsl:value-of select="."/>
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
196 changes: 196 additions & 0 deletions shared/xsl/bgf2sdf.xslt
@@ -0,0 +1,196 @@
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:bgf="http://planet-sl.org/bgf"
xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xsl:output
method="text"
encoding="UTF-8"
omit-xml-declaration="yes"
/>

<!--
module Main
exports context-free start-symbols
<xsl:apply-templates select="./root" />
context-free sorts
<xsl:apply-templates select="./bgf:production" />
context-free syntax
<xsl:apply-templates select="./bgf:production" />
-->

<xsl:template match="/bgf:grammar">
<xsl:text>module Main

exports
</xsl:text>
<xsl:if test="./root">
<xsl:text> context-free start-symbols </xsl:text>
<xsl:value-of select="./root"/>
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:text> sorts
</xsl:text>
<xsl:for-each select="./bgf:production/nonterminal[not(text()=../preceding-sibling::*/nonterminal/text())]">
<xsl:call-template name="display-nonterminal">
<xsl:with-param name="nt" select="text()"/>
</xsl:call-template>
<xsl:text> </xsl:text>
</xsl:for-each>
<xsl:text>
context-free syntax</xsl:text>
<xsl:apply-templates select="./bgf:*"/>
</xsl:template>

<xsl:template name="display-nonterminal">
<xsl:param name="nt"/>
<xsl:value-of select="concat(translate(substring($nt,1,1), 'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring(translate($nt,'_','-'),2))"/>
</xsl:template>

<xsl:template match="bgf:production">
<xsl:choose>
<xsl:when test="./bgf:expression/choice">
<xsl:for-each select="./bgf:expression/choice/bgf:expression">
<xsl:text>
</xsl:text>
<xsl:call-template name="no-parenthesis">
<xsl:with-param name="expr" select="."/>
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:text>
</xsl:text>
<xsl:call-template name="no-parenthesis">
<xsl:with-param name="expr" select="./bgf:expression"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<xsl:text><![CDATA[ -> ]]></xsl:text>
<xsl:call-template name="display-nonterminal">
<xsl:with-param name="nt" select="./nonterminal"/>
</xsl:call-template>
<xsl:if test="./label">
<xsl:text> {cons(</xsl:text>
<xsl:value-of select="./label"/>
<xsl:text>)}</xsl:text>
</xsl:if>
</xsl:template>

<xsl:template match="bgf:expression">
<xsl:apply-templates select="./*"/>
</xsl:template>

<xsl:template match="marked">
<xsl:text><![CDATA[<]]></xsl:text>
<xsl:apply-templates select="./*"/>
<xsl:text><![CDATA[>]]></xsl:text>
</xsl:template>

<xsl:template match="plus">
<xsl:apply-templates select="./*"/>
<xsl:text>+</xsl:text>
</xsl:template>

<xsl:template match="star">
<xsl:apply-templates select="./*"/>
<xsl:text>*</xsl:text>
</xsl:template>

<xsl:template match="optional">
<xsl:apply-templates select="./*"/>
<xsl:text>?</xsl:text>
</xsl:template>

<xsl:template match="terminal">
<xsl:text>"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"</xsl:text>
</xsl:template>

<xsl:template match="value">
<xsl:choose>
<xsl:when test=". = 'string'">
<xsl:text>STR</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>INT</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="epsilon">
<xsl:text></xsl:text>
</xsl:template>

<xsl:template match="empty">
<xsl:text>EMPTY</xsl:text>
</xsl:template>

<xsl:template match="any">
<xsl:text>ANY</xsl:text>
</xsl:template>

<xsl:template match="nonterminal">
<xsl:call-template name="display-nonterminal">
<xsl:with-param name="nt" select="."/>
</xsl:call-template>
</xsl:template>

<xsl:template match="selectable">
<xsl:value-of select="selector"/>
<xsl:text>::</xsl:text>
<xsl:choose>
<xsl:when test="local-name(bgf:expression/*) = 'star'
or local-name(bgf:expression/*) = 'optional'
or local-name(bgf:expression/*) = 'plus'">
<xsl:text>(</xsl:text>
<xsl:apply-templates select="bgf:expression"/>
<xsl:text>)</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="bgf:expression"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="sequence">
<xsl:text>(</xsl:text>
<xsl:apply-templates select="./bgf:expression[1]/*"/>
<xsl:for-each select="./bgf:expression[position()>1]">
<xsl:text> </xsl:text>
<xsl:apply-templates select="./*"/>
</xsl:for-each>
<xsl:text>)</xsl:text>
</xsl:template>

<!-- inner choices - BNF bar -->
<xsl:template match="choice">
<xsl:text>(</xsl:text>
<xsl:apply-templates select="./bgf:expression[1]/*"/>
<xsl:for-each select="./bgf:expression[position()>1]">
<xsl:text> | </xsl:text>
<xsl:apply-templates select="./*"/>
</xsl:for-each>
<xsl:text>)</xsl:text>
</xsl:template>

<xsl:template name="no-parenthesis">
<xsl:param name="expr"/>
<xsl:choose>
<xsl:when test="$expr/sequence">
<xsl:apply-templates select="$expr/sequence/bgf:expression[1]/*"/>
<xsl:for-each select="$expr/sequence/bgf:expression[position()>1]">
<xsl:text> </xsl:text>
<xsl:apply-templates select="./*"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$expr"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
20 changes: 14 additions & 6 deletions shared/xsl/xbgf2xhtml.xslt
Expand Up @@ -16,17 +16,25 @@
<xsl:template match="/xbgf:sequence">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<body>
<xsl:apply-templates select="./xbgf:*"/>
<ul>
<xsl:for-each select="./xbgf:*">
<li>
<xsl:apply-templates select="."/>
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>

<xsl:template match="xbgf:atomic">
<xsl:text>[[
</xsl:text>
<xsl:apply-templates select="./xbgf:*"/>
<xsl:text>]];
</xsl:text>
<ul xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<xsl:for-each select="./xbgf:*">
<li>
<xsl:apply-templates select="."/>
</li>
</xsl:for-each>
</ul>
</xsl:template>

<!-- optional context -->
Expand Down

0 comments on commit d36a90e

Please sign in to comment.