Skip to content

Commit

Permalink
BGF pretty-printer for DMS
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@682 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Sep 30, 2009
1 parent e30428c commit b292b66
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
23 changes: 23 additions & 0 deletions shared/tools/bgf2dms
@@ -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 to DMS-specific BNF."
echo "Usage: bgf2dms <input-bgf-document> [<output-text>]"
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/bgf2dms.xslt $1> ${OUTPUT}
168 changes: 168 additions & 0 deletions shared/xsl/bgf2dms.xslt
@@ -0,0 +1,168 @@
<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">

<!--
Using this pretty-printer on a grammar with BGF-specific features
can lead to generation of incorrect DMS BNF grammars.
-->

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

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

<xsl:template match="bgf:production">
<!--xsl:if test="./label">
<xsl:text>[</xsl:text>
<xsl:value-of select="./label"/>
<xsl:text>] </xsl:text>
</xsl:if-->
<xsl:choose>
<xsl:when test="./bgf:expression/choice">
<xsl:for-each select="./bgf:expression/choice/bgf:expression">
<xsl:value-of select="./nonterminal"/>
<xsl:text> = </xsl:text>
<xsl:call-template name="no-parenthesis">
<xsl:with-param name="expr" select="."/>
</xsl:call-template>
<xsl:text> ;
</xsl:text>
</xsl:for-each>
</xsl:when>
<xsl:when test="./bgf:expression/epsilon">
<xsl:value-of select="./nonterminal"/>
<xsl:text> = ;
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="./nonterminal"/>
<xsl:text> = </xsl:text>
<xsl:call-template name="no-parenthesis">
<xsl:with-param name="expr" select="./bgf:expression"/>
</xsl:call-template>
<xsl:text> ;
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</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></xsl:text>
</xsl:template>

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

<xsl:template match="nonterminal">
<xsl:value-of select="."/>
</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>
1 change: 1 addition & 0 deletions shared/xsl/slps-xsl.csproj
Expand Up @@ -40,6 +40,7 @@
-->
<ItemGroup>
<Content Include="bgf2bnf.xslt" />
<Content Include="bgf2dms.xslt" />
<Content Include="bgf2sdf.xslt" />
<Content Include="btf2btf.xslt" />
<Content Include="ecore2bgf.xslt" />
Expand Down

0 comments on commit b292b66

Please sign in to comment.