/
bgf2sdf.xslt
196 lines (173 loc) · 5.74 KB
/
bgf2sdf.xslt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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>