Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@682 ab42f6e0-554d-0410-b580-99e487e6eeb2
- Loading branch information
1 parent
e30428c
commit b292b66
Showing
3 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters