1 <?xml version=
"1.0" encoding=
"UTF-8"?>
4 xml2dot.xsl - transform Bison XML Report into DOT.
6 Copyright (C) 2007, 2008 Free Software Foundation, Inc.
8 This file is part of Bison, the GNU Compiler Compiler.
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 Written by Wojciech Polak <polak@gnu.org>.
26 <xsl:stylesheet version=
"1.0"
27 xmlns:
xsl=
"http://www.w3.org/1999/XSL/Transform"
28 xmlns:
bison=
"http://www.gnu.org/software/bison/">
30 <xsl:import href=
"bison.xsl"/>
31 <xsl:output method=
"text" encoding=
"UTF-8" indent=
"no"/>
33 <xsl:template match=
"/">
34 <xsl:apply-templates select=
"bison-xml-report"/>
37 <xsl:template match=
"bison-xml-report">
38 <xsl:apply-templates select=
"automaton"/>
41 <xsl:template match=
"automaton">
42 <xsl:text>digraph Automaton {
</xsl:text>
43 <xsl:apply-templates select=
"state"/>
44 <xsl:text>}
</xsl:text>
47 <xsl:template match=
"automaton/state">
48 <xsl:call-template name=
"output-node">
49 <xsl:with-param name=
"number" select=
"@number"/>
50 <xsl:with-param name=
"label">
51 <xsl:value-of select=
"@number"/>
52 <xsl:apply-templates select=
"itemset/item"/>
55 <xsl:apply-templates select=
"actions/transitions"/>
58 <xsl:template match=
"actions/transitions">
59 <xsl:apply-templates select=
"transition"/>
62 <xsl:template match=
"item">
63 <xsl:apply-templates select=
"key('bison:ruleByNumber', @rule-number)">
64 <xsl:with-param name=
"point" select=
"@point"/>
65 </xsl:apply-templates>
66 <xsl:apply-templates select=
"lookaheads"/>
69 <xsl:template match=
"rule">
70 <xsl:param name=
"point"/>
71 <xsl:text> </xsl:text>
72 <xsl:value-of select=
"lhs"/>
73 <xsl:text> -
></xsl:text>
74 <xsl:if test=
"$point = 0">
75 <xsl:text> .
</xsl:text>
77 <xsl:for-each select=
"rhs/symbol|rhs/empty">
78 <xsl:apply-templates select=
"."/>
79 <xsl:if test=
"$point = position()">
80 <xsl:text> .
</xsl:text>
85 <xsl:template match=
"symbol">
86 <xsl:text> </xsl:text>
87 <xsl:value-of select=
"."/>
90 <xsl:template match=
"empty"/>
92 <xsl:template match=
"lookaheads">
93 <xsl:text>[
</xsl:text>
94 <xsl:apply-templates select=
"symbol"/>
95 <xsl:text>]
</xsl:text>
98 <xsl:template match=
"lookaheads/symbol">
99 <xsl:value-of select=
"."/>
100 <xsl:if test=
"position() != last()">
101 <xsl:text>,
</xsl:text>
105 <xsl:template match=
"transition">
106 <xsl:call-template name=
"output-edge">
107 <xsl:with-param name=
"src" select=
"../../../@number"/>
108 <xsl:with-param name=
"dst" select=
"@state"/>
109 <xsl:with-param name=
"style">
111 <xsl:when test=
"@symbol = 'error'">
112 <xsl:text>dotted
</xsl:text>
114 <xsl:when test=
"@type = 'shift'">
115 <xsl:text>solid
</xsl:text>
118 <xsl:text>dashed
</xsl:text>
122 <xsl:with-param name=
"label">
123 <xsl:if test=
"not(@symbol = 'error')">
124 <xsl:value-of select=
"@symbol"/>
130 <xsl:template name=
"output-node">
131 <xsl:param name=
"number"/>
132 <xsl:param name=
"label"/>
133 <xsl:text> </xsl:text>
134 <xsl:value-of select=
"$number"/>
135 <xsl:text> [
label=
"</xsl:text>
136 <xsl:call-template name="escape
">
137 <xsl:with-param name="subject
" select="$label
"/>
139 <xsl:text>"]
</xsl:text>
142 <xsl:template name=
"output-edge">
143 <xsl:param name=
"src"/>
144 <xsl:param name=
"dst"/>
145 <xsl:param name=
"style"/>
146 <xsl:param name=
"label"/>
147 <xsl:text> </xsl:text>
148 <xsl:value-of select=
"$src"/>
149 <xsl:text> -
> </xsl:text>
150 <xsl:value-of select=
"$dst"/>
151 <xsl:text> [style=
</xsl:text>
152 <xsl:value-of select=
"$style"/>
153 <xsl:if test=
"$label and $label != ''">
154 <xsl:text> label=
"</xsl:text>
155 <xsl:call-template name="escape
">
156 <xsl:with-param name="subject
" select="$label
"/>
158 <xsl:text>"</xsl:text>
160 <xsl:text>]
</xsl:text>
163 <xsl:template name=
"escape">
164 <xsl:param name=
"subject"/> <!-- required -->
165 <xsl:call-template name=
"string-replace">
166 <xsl:with-param name=
"subject">
167 <xsl:call-template name=
"string-replace">
168 <xsl:with-param name=
"subject">
169 <xsl:call-template name=
"string-replace">
170 <xsl:with-param name=
"subject" select=
"$subject"/>
171 <xsl:with-param name=
"search" select=
"'\'"/>
172 <xsl:with-param name=
"replace" select=
"'\\'"/>
175 <xsl:with-param name=
"search" select=
"'"'"/>
176 <xsl:with-param name=
"replace" select=
"'\"'"/>
179 <xsl:with-param name=
"search" select=
"' '"/>
180 <xsl:with-param name=
"replace" select=
"'\n'"/>
184 <xsl:template name=
"string-replace">
185 <xsl:param name=
"subject"/>
186 <xsl:param name=
"search"/>
187 <xsl:param name=
"replace"/>
189 <xsl:when test=
"contains($subject, $search)">
190 <xsl:variable name=
"before" select=
"substring-before($subject, $search)"/>
191 <xsl:variable name=
"after" select=
"substring-after($subject, $search)"/>
192 <xsl:value-of select=
"$before"/>
193 <xsl:value-of select=
"$replace"/>
194 <xsl:call-template name=
"string-replace">
195 <xsl:with-param name=
"subject" select=
"$after"/>
196 <xsl:with-param name=
"search" select=
"$search"/>
197 <xsl:with-param name=
"replace" select=
"$replace"/>
201 <xsl:value-of select=
"$subject"/>