Skip to content

Commit

Permalink
bgf and xbgf pretty-printing
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@364 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Oct 22, 2008
1 parent dc8ae91 commit 99f945d
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 6 deletions.
1 change: 1 addition & 0 deletions shared/tools/bgf2bnf
Expand Up @@ -11,6 +11,7 @@ if [ $# -eq 1 ]; then
elif [ $# -ne 2 ]; then
echo "This tool transforms XML BNF-like Grammar Format documents to EBNF dialect used in JLS."
echo "Usage: bgf2bnf <input-bgf-document> [<output-text>]"
echo "When the output file is not specified, the same filename with .bnf extension is used."
exit 1
elif [ ! -r $1 ]; then
echo "Oops: $1 not found or not readable."
Expand Down
25 changes: 25 additions & 0 deletions shared/tools/xbgf2xbnf
@@ -0,0 +1,25 @@
#!/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 EBNF dialect used in JLS."
echo "Usage: xbgf2xbnf <input-xbgf-document> [<output-text>]"
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/xbgf2xbnf.xslt $1 > ${OUTPUT}

22 changes: 17 additions & 5 deletions shared/xsl/bgf2bnf.xslt
Expand Up @@ -28,7 +28,7 @@
</xsl:when>
<xsl:otherwise>
<xsl:text>
</xsl:text>
</xsl:text>
<xsl:apply-templates select="./bgf:expression"/>
<xsl:text>
</xsl:text>
Expand All @@ -41,10 +41,9 @@
</xsl:template>

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

<xsl:template match="star">
Expand Down Expand Up @@ -97,7 +96,8 @@
<xsl:apply-templates select="./*"/>
</xsl:template>

<xsl:template match="choice">
<!-- top level choices - one per line -->
<xsl:template match="/bgf:grammar/bgf:production/bgf:expression/choice">
<xsl:call-template name="car">
<xsl:with-param name="expr" select="./bgf:expression[1]"/>
</xsl:call-template>
Expand All @@ -108,6 +108,18 @@
</xsl:for-each>
</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="car">
<xsl:param name="expr"/>
<xsl:text>
Expand Down
7 changes: 6 additions & 1 deletion shared/xsl/slps-xsl.csproj
Expand Up @@ -39,10 +39,15 @@
</Target>
-->
<ItemGroup>
<Content Include="bgf2bnf.xslt" />
<Content Include="bgf2sdf.xslt" />
<Content Include="btf2btf.xslt" />
<Content Include="ldf2bgf.xslt" />
<Content Include="ldf2ldf_lift.xslt" />
<Content Include="ldf2ldf_ns.xslt" />
<Content Include="ldf2ldf_tail.xslt" />
<Content Include="ldf2samples.xslt" />
<Content Include="ldf2xhtml.xslt" />
<Content Include="xbgf2xbnf.xslt" />
<Content Include="xhtml2fo.xslt" />
</ItemGroup>
</Project>
166 changes: 166 additions & 0 deletions shared/xsl/xbgf2xbnf.xslt
@@ -0,0 +1,166 @@
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:bgf="http://planet-sl.org/bgf"
xmlns:xbgf="http://planet-sl.org/xbgf"
xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xsl:import href="bgf2bnf.xslt" />

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

<xsl:template match="/xbgf:sequence">
<xsl:apply-templates select="./xbgf:*"/>
</xsl:template>

<!-- optional context -->
<xsl:template name="context">
<xsl:param name="in"/>
<xsl:choose>
<xsl:when test="$in/nonterminal">
<xsl:text> in </xsl:text>
<xsl:value-of select="$in/nonterminal" />
</xsl:when>
<xsl:when test="$in/label">
<xsl:text> in "</xsl:text>
<xsl:value-of select="$in/label" />
<xsl:text>"</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>

<!-- add, chain, define, ... -->

<xsl:template match="xbgf:*">
<xsl:value-of select="local-name()" />
<xsl:text>(
</xsl:text>
<xsl:apply-templates select="./bgf:production"/>
<xsl:call-template name="context">
<xsl:with-param name="in" select="./in"/>
</xsl:call-template>
<xsl:text>);
</xsl:text>
</xsl:template>

<xsl:template match="xbgf:designate">
<xsl:value-of select="local-name()" />
<xsl:text>("</xsl:text>
<xsl:value-of select="./bgf:production/label" />
<xsl:text>",
</xsl:text>
<xsl:apply-templates select="./bgf:production"/>
<xsl:text>);
</xsl:text>
</xsl:template>

<xsl:template match="xbgf:deyaccify|xbgf:eliminate|xbgf:horizontal|xbgf:inline|xbgf:undefine">
<xsl:value-of select="local-name()" />
<xsl:text>(</xsl:text>
<xsl:value-of select="text()"/>
<xsl:text>);
</xsl:text>
</xsl:template>

<xsl:template match="xbgf:distribute|xbgf:vertical">
<xsl:value-of select="local-name()" />
<xsl:text>(</xsl:text>
<xsl:call-template name="context">
<xsl:with-param name="in" select="."/>
</xsl:call-template>
<xsl:text> );
</xsl:text>
</xsl:template>

<xsl:template match="xbgf:factor|xbgf:massage|xbgf:narrow|xbgf:replace|xbgf:widen">
<xsl:value-of select="local-name()" />
<xsl:text>(
</xsl:text>
<xsl:apply-templates select="./bgf:expression[1]"/>
<xsl:text>,
</xsl:text>
<xsl:apply-templates select="./bgf:expression[2]"/>
<xsl:choose>
<xsl:when test="in">
<xsl:text>
</xsl:text>
<xsl:call-template name="context">
<xsl:with-param name="in" select="./in"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
<xsl:text>);
</xsl:text>
</xsl:template>

<xsl:template match="xbgf:fold|xbgf:unfold">
<xsl:value-of select="local-name()" />
<xsl:text>(</xsl:text>
<xsl:value-of select="./nonterminal"/>
<xsl:choose>
<xsl:when test="in">
<xsl:text>
</xsl:text>
<xsl:call-template name="context">
<xsl:with-param name="in" select="./in"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
<xsl:text>);
</xsl:text>
</xsl:template>

<xsl:template match="xbgf:rename">
<xsl:value-of select="local-name()" />
<xsl:text>(</xsl:text>
<xsl:value-of select="local-name(*)" />
<xsl:text>
</xsl:text>
<xsl:apply-templates select="./*/*[1]"/>
<xsl:text>,
</xsl:text>
<xsl:apply-templates select="./*/*[2]"/>
<xsl:text>);
</xsl:text>
</xsl:template>

<xsl:template match="xbgf:reroot">
<xsl:value-of select="local-name()" />
<xsl:text>(</xsl:text>
<xsl:value-of select="./root"/>
<xsl:text>);
</xsl:text>
</xsl:template>

<xsl:template match="xbgf:strip">
<xsl:value-of select="local-name()" />
<xsl:text>(</xsl:text>
<xsl:choose>
<xsl:when test="label|selector|terminal">
<xsl:value-of select="local-name(*)" />
<xsl:text>, "</xsl:text>
<xsl:value-of select="*/text()" />
<xsl:text>"</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="local-name(*)" />
</xsl:otherwise>
</xsl:choose>
<xsl:text>);
</xsl:text>
</xsl:template>

<xsl:template match="xbgf:unite">
<xsl:value-of select="local-name()" />
<xsl:text>(</xsl:text>
<xsl:apply-templates select="add/text()"/>
<xsl:text>, </xsl:text>
<xsl:apply-templates select="to/text()"/>
<xsl:text>);
</xsl:text>
</xsl:template>

</xsl:stylesheet>
3 changes: 3 additions & 0 deletions topics/transformation/xbgf/tests/factor1.xbgf
Expand Up @@ -44,5 +44,8 @@
</bgf:expression>
</sequence>
</bgf:expression>
<in>
<nonterminal>a</nonterminal>
</in>
</xbgf:factor>
</xbgf:sequence>

0 comments on commit 99f945d

Please sign in to comment.