Skip to content

Commit

Permalink
TXL to BGF magic
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@660 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Jul 7, 2009
1 parent e3ebd34 commit 6ce3d27
Show file tree
Hide file tree
Showing 12 changed files with 1,455 additions and 1 deletion.
2 changes: 1 addition & 1 deletion shared/tools/asfix2btf
Expand Up @@ -19,7 +19,7 @@ else
make build
cd ${LOCAL1}
cat $2 | python ${SLPS}/topics/fl/asfsdf/pre.py | sglr -p ${SLPS}/topics/fl/asfsdf/FL.tbl -t > $3.trm
(echo "main("; cat $3.trm; echo ")" ) | sglr -p ${SLPS}/topics/extraction/asfix2btf//Main.tbl | asfe -e ${SLPS}/topics/extraction/asfix2btf//Main.eqs | unparsePT > $3.tmp
(echo "main("; cat $3.trm; echo ")" ) | sglr -p ${SLPS}/topics/extraction/asfix2btf/Main.tbl | asfe -e ${SLPS}/topics/extraction/asfix2btf/Main.eqs | unparsePT > $3.tmp
xsltproc ${SLPS}/shared/xsl/btf2btf.xslt $3.tmp >$3
rm -f $3.tmp $3.trm
fi
Expand Down
38 changes: 38 additions & 0 deletions shared/tools/txl2bgf
@@ -0,0 +1,38 @@
#!/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
txl $1 ${SLPS}/topics/extraction/txl2bgf/TXLGrammar/Txl/txl.txl -xml > grammar.xml
perl -pi -w -e 's/repeat statement/repeat_statement/g;' grammar.xml
perl -pi -w -e 's/repeat compound/repeat_compound/g;' grammar.xml
perl -pi -w -e 's/repeat literalOrType/repeat_literalOrType/g;' grammar.xml
perl -pi -w -e 's/repeat literalOrVariable/repeat_literalOrVariable/g;' grammar.xml
perl -pi -w -e 's/repeat barLiteralsAndTypes/repeat_barLiteralsAndTypes/g;' grammar.xml
perl -pi -w -e 's/opt typeRepeater/opt_typeRepeater/g;' grammar.xml
xsltproc ${SLPS}/shared/xsl/txl2bgf.xslt grammar.xml > $2
rm -f grammar.xml
elif [ $# -eq 1 ]; then
txl $1 ${SLPS}/topics/extraction/txl2bgf/TXLGrammar/Txl/txl.txl -xml > grammar.xml
perl -pi -w -e 's/repeat statement/repeat_statement/g;' grammar.xml
perl -pi -w -e 's/repeat compound/repeat_compound/g;' grammar.xml
perl -pi -w -e 's/repeat literalOrType/repeat_literalOrType/g;' grammar.xml
perl -pi -w -e 's/repeat literalOrVariable/repeat_literalOrVariable/g;' grammar.xml
perl -pi -w -e 's/repeat barLiteralsAndTypes/repeat_barLiteralsAndTypes/g;' grammar.xml
perl -pi -w -e 's/opt typeRepeater/opt_typeRepeater/g;' grammar.xml
xsltproc ${SLPS}/shared/xsl/txl2bgf.xslt grammar.xml > ${LOCAL}/`basename $1 .txl`.bgf
rm -f grammar.xml
else
echo "This tool extracts a BGF grammar from a TXL grammar"
echo "Usage: txl2bgf <txl-grammar> [<output-grammar>]"
exit 2
fi

1 change: 1 addition & 0 deletions shared/xsl/slps-xsl.csproj
Expand Up @@ -49,6 +49,7 @@
<Content Include="ldf2ldf_tail.xslt" />
<Content Include="ldf2tex.xslt" />
<Content Include="ldf2xhtml.xslt" />
<Content Include="txl2bgf.xslt" />
<Content Include="xbgf2xbnf.xslt" />
<Content Include="xhtml2fo.xslt" />
</ItemGroup>
Expand Down
98 changes: 98 additions & 0 deletions shared/xsl/txl2bgf.xslt
@@ -0,0 +1,98 @@
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:bgf="http://planet-sl.org/bgf"
>
<xsl:output method="xml" encoding="UTF-8"/>

<xsl:template match="/program">
<bgf:grammar>
<xsl:apply-templates select="repeat_statement/statement/defineStatement"/>
</bgf:grammar>
</xsl:template>

<xsl:template match="defineStatement">
<bgf:production>
<nonterminal>
<xsl:value-of select="typeid/id" />
</nonterminal>
<!--xsl:apply-templates select="repeat_literalOrType/literalOrType/type/typeSpec"/-->
<xsl:choose>
<xsl:when test="repeat_barLiteralsAndTypes">
<bgf:expression>
<choice>
<xsl:call-template name="sequenceOrNot">
<xsl:with-param name="list" select="repeat_literalOrType"/>
</xsl:call-template>
<xsl:for-each select="repeat_barLiteralsAndTypes/barLiteralsAndTypes/repeat_literalOrType">
<xsl:call-template name="sequenceOrNot">
<xsl:with-param name="list" select="."/>
</xsl:call-template>
</xsl:for-each>
</choice>
</bgf:expression>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="sequenceOrNot">
<xsl:with-param name="list" select="repeat_literalOrType"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</bgf:production>
</xsl:template>

<xsl:template name="sequenceOrNot">
<xsl:param name="list"/>
<bgf:expression>
<xsl:choose>
<xsl:when test="count($list/literalOrType)=1">
<xsl:apply-templates select="$list/literalOrType"/>
</xsl:when>
<xsl:otherwise>
<sequence>
<xsl:apply-templates select="$list/literalOrType"/>
</sequence>
</xsl:otherwise>
</xsl:choose>
</bgf:expression>
</xsl:template>

<xsl:template match="literalOrType">
<!--xsl:for-each select="repeat_literalOrType/literalOrType"-->
<xsl:choose>
<xsl:when test="type/typeSpec/opt_typeRepeater/typeRepeater='+'">
<bgf:expression>
<plus>
<nonterminal>
<xsl:value-of select="type/typeSpec/typeid/id" />
</nonterminal>
</plus>
</bgf:expression>
</xsl:when>
<xsl:when test="type/typeSpec/opt_typeRepeater/typeRepeater='*'">
<bgf:expression>
<star>
<nonterminal>
<xsl:value-of select="type/typeSpec/typeid/id" />
</nonterminal>
</star>
</bgf:expression>
</xsl:when>
<xsl:when test="literal">
<bgf:expression>
<terminal>
<xsl:value-of select="literal/unquotedLiteral/special" />
</terminal>
</bgf:expression>
</xsl:when>
<!-- fallback -->
<xsl:otherwise>
<bgf:expression>
<nonterminal>
<xsl:value-of select="type/typeSpec/typeid/id" />
</nonterminal>
</bgf:expression>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
3 changes: 3 additions & 0 deletions topics/extraction/txl2bgf/Makefile
@@ -0,0 +1,3 @@
test:
../../../shared/tools/txl2bgf ../../fl/txl/FL.Txl fl.bgf
../../../shared/tools/bgf2bnf fl.bgf
84 changes: 84 additions & 0 deletions topics/extraction/txl2bgf/TXLGrammar/Examples/calculator.txl
@@ -0,0 +1,84 @@
% Calculator.Txl - simple numerical expression evaluator

% Part I. Syntax specification
define program
[expression]
end define

define expression
[term]
| [expression] [addop] [term]
end define

define term
[primary]
| [term] [mulop] [primary]
end define

define primary
[number]
| ( [expression] )
end define

define addop
'+
| '-
end define

define mulop
'*
| '/
end define


% Part 2. Transformation rules
rule main
replace [expression]
E [expression]
construct NewE [expression]
E [resolveAddition]
[resolveSubtraction]
[resolveMultiplication]
[resolveDivision]
[resolveBracketedExpressions]
where not
NewE [= E]
by
NewE
end rule

rule resolveAddition
replace [expression]
N1 [number] + N2 [number]
by
N1 [+ N2]
end rule

rule resolveSubtraction
replace [expression]
N1 [number] - N2 [number]
by
N1 [- N2]
end rule

rule resolveMultiplication
replace [term]
N1 [number] * N2 [number]
by
N1 [* N2]
end rule

rule resolveDivision
replace [term]
N1 [number] / N2 [number]
by
N1 [/ N2]
end rule

rule resolveBracketedExpressions
replace [primary]
( N [number] )
by
N
end rule

0 comments on commit 6ce3d27

Please sign in to comment.