1 <?xml version=
"1.0" encoding=
"UTF-8"?>
4 xml2text.xsl - transform Bison XML Report into plain text.
7 Copyright (C) 2007 Free Software Foundation, Inc.
9 This file is part of Bison, the GNU Compiler Compiler.
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 Written by Wojciech Polak <polak@gnu.org>.
27 <xsl:stylesheet version=
"1.0"
28 xmlns:
xsl=
"http://www.w3.org/1999/XSL/Transform"
29 xmlns:
bison=
"http://www.gnu.org/software/bison/">
31 <xsl:import href=
"bison.xsl"/>
32 <xsl:output method=
"text" encoding=
"UTF-8" indent=
"no"/>
34 <xsl:template match=
"/">
35 <xsl:apply-templates select=
"bison-xml-report"/>
38 <xsl:template match=
"bison-xml-report">
39 <xsl:apply-templates select=
"grammar" mode=
"reductions"/>
40 <xsl:apply-templates select=
"grammar" mode=
"useless-in-parser"/>
41 <xsl:apply-templates select=
"automaton" mode=
"conflicts"/>
42 <xsl:apply-templates select=
"grammar"/>
43 <xsl:apply-templates select=
"automaton"/>
46 <xsl:template match=
"grammar" mode=
"reductions">
47 <xsl:apply-templates select=
"nonterminals" mode=
"useless-in-grammar"/>
48 <xsl:apply-templates select=
"terminals" mode=
"unused-in-grammar"/>
49 <xsl:apply-templates select=
"rules" mode=
"useless-in-grammar"/>
52 <xsl:template match=
"nonterminals" mode=
"useless-in-grammar">
53 <xsl:if test=
"nonterminal[@usefulness='useless-in-grammar']">
54 <xsl:text>Nonterminals useless in grammar
</xsl:text>
55 <xsl:for-each select=
"nonterminal[@usefulness='useless-in-grammar']">
56 <xsl:text> </xsl:text>
57 <xsl:value-of select=
"@name"/>
58 <xsl:text> </xsl:text>
60 <xsl:text> </xsl:text>
64 <xsl:template match=
"terminals" mode=
"unused-in-grammar">
65 <xsl:if test=
"terminal[@usefulness='unused-in-grammar']">
66 <xsl:text>Terminals unused in grammar
</xsl:text>
67 <xsl:for-each select=
"terminal[@usefulness='unused-in-grammar']">
68 <xsl:sort select=
"@symbol-number" data-type=
"number"/>
69 <xsl:text> </xsl:text>
70 <xsl:value-of select=
"@name"/>
71 <xsl:text> </xsl:text>
73 <xsl:text> </xsl:text>
77 <xsl:template match=
"rules" mode=
"useless-in-grammar">
78 <xsl:variable name=
"set" select=
"rule[@usefulness='useless-in-grammar']"/>
80 <xsl:text>Rules useless in grammar
</xsl:text>
81 <xsl:call-template name=
"style-rule-set">
82 <xsl:with-param name=
"rule-set" select=
"$set"/>
84 <xsl:text> </xsl:text>
88 <xsl:template match=
"grammar" mode=
"useless-in-parser">
90 name=
"set" select=
"rules/rule[@usefulness='useless-in-parser']"
93 <xsl:text>Rules useless in parser due to conflicts
</xsl:text>
94 <xsl:call-template name=
"style-rule-set">
95 <xsl:with-param name=
"rule-set" select=
"$set"/>
97 <xsl:text> </xsl:text>
101 <xsl:template match=
"grammar">
102 <xsl:text>Grammar
</xsl:text>
103 <xsl:call-template name=
"style-rule-set">
105 name=
"rule-set" select=
"rules/rule[@usefulness!='useless-in-grammar']"
108 <xsl:text> </xsl:text>
109 <xsl:apply-templates select=
"terminals"/>
110 <xsl:apply-templates select=
"nonterminals"/>
113 <xsl:template name=
"style-rule-set">
114 <xsl:param name=
"rule-set"/>
115 <xsl:for-each select=
"$rule-set">
116 <xsl:apply-templates select=
".">
117 <xsl:with-param name=
"pad" select=
"'3'"/>
118 <xsl:with-param name=
"prev-lhs">
119 <xsl:if test=
"position()>1">
120 <xsl:variable name=
"position" select=
"position()"/>
121 <xsl:value-of select=
"$rule-set[$position - 1]/lhs"/>
124 </xsl:apply-templates>
128 <xsl:template match=
"grammar/terminals">
129 <xsl:text>Terminals, with rules where they appear
</xsl:text>
130 <xsl:apply-templates select=
"terminal"/>
131 <xsl:text> </xsl:text>
134 <xsl:template match=
"grammar/nonterminals">
135 <xsl:text>Nonterminals, with rules where they appear
</xsl:text>
136 <xsl:apply-templates select=
"nonterminal[@usefulness!='useless-in-grammar']"/>
139 <xsl:template match=
"terminal">
140 <xsl:value-of select=
"@name"/>
141 <xsl:call-template name=
"line-wrap">
142 <xsl:with-param name=
"first-line-length">
144 <xsl:when test=
"string-length(@name) > 66">0</xsl:when>
146 <xsl:value-of select=
"66 - string-length(@name)" />
150 <xsl:with-param name=
"line-length" select=
"66" />
151 <xsl:with-param name=
"text">
152 <xsl:value-of select=
"concat(' (', @token-number, ')')"/>
153 <xsl:apply-templates select=
"rule" />
158 <xsl:template match=
"terminal/rule">
159 <xsl:text> </xsl:text>
160 <xsl:value-of select=
"."/>
163 <xsl:template match=
"nonterminal">
164 <xsl:value-of select=
"@name"/>
165 <xsl:value-of select=
"concat(' (', @symbol-number, ')')"/>
166 <xsl:text> </xsl:text>
167 <xsl:variable name=
"output">
168 <xsl:call-template name=
"line-wrap">
169 <xsl:with-param name=
"line-length" select=
"66" />
170 <xsl:with-param name=
"text">
171 <xsl:text> </xsl:text>
172 <xsl:if test=
"left/rule">
173 <xsl:text>on@left:
</xsl:text>
175 <xsl:apply-templates select=
"left/rule"/>
176 <xsl:if test=
"left/rule and right/rule">
177 <xsl:text>,
</xsl:text>
179 <xsl:if test=
"right/rule">
180 <xsl:text>on@right:
</xsl:text>
182 <xsl:apply-templates select=
"right/rule"/>
186 <xsl:value-of select=
"translate($output, '@', ' ')" />
189 <xsl:template match=
"nonterminal/left/rule|nonterminal/right/rule">
190 <xsl:text> </xsl:text>
191 <xsl:value-of select=
"."/>
194 <xsl:template match=
"automaton" mode=
"conflicts">
195 <xsl:variable name=
"conflict-report">
196 <xsl:apply-templates select=
"state" mode=
"conflicts"/>
198 <xsl:if test=
"string-length($conflict-report) != 0">
199 <xsl:value-of select=
"$conflict-report"/>
200 <xsl:text> </xsl:text>
204 <xsl:template match=
"state" mode=
"conflicts">
205 <xsl:variable name=
"conflict-counts">
206 <xsl:apply-templates select=
"." mode=
"bison:count-conflicts" />
209 name=
"sr-count" select=
"substring-before($conflict-counts, ',')"
212 name=
"rr-count" select=
"substring-after($conflict-counts, ',')"
214 <xsl:if test=
"$sr-count > 0 or $rr-count > 0">
215 <xsl:value-of select=
"concat('State ', @number, ' conflicts:')"/>
216 <xsl:if test=
"$sr-count > 0">
217 <xsl:value-of select=
"concat(' ', $sr-count, ' shift/reduce')"/>
218 <xsl:if test=
"$rr-count > 0">
219 <xsl:value-of select=
"(',')"/>
222 <xsl:if test=
"$rr-count > 0">
223 <xsl:value-of select=
"concat(' ', $rr-count, ' reduce/reduce')"/>
225 <xsl:value-of select=
"' '"/>
229 <xsl:template match=
"automaton">
230 <xsl:apply-templates select=
"state">
231 <xsl:with-param name=
"pad" select=
"'3'"/>
232 </xsl:apply-templates>
235 <xsl:template match=
"automaton/state">
236 <xsl:param name=
"pad"/>
237 <xsl:text> </xsl:text>
238 <xsl:text>state
</xsl:text>
239 <xsl:value-of select=
"@number"/>
240 <xsl:text> </xsl:text>
241 <xsl:apply-templates select=
"itemset/item">
242 <xsl:with-param name=
"pad" select=
"$pad"/>
243 </xsl:apply-templates>
244 <xsl:apply-templates select=
"actions/transitions">
245 <xsl:with-param name=
"type" select=
"'shift'"/>
246 </xsl:apply-templates>
247 <xsl:apply-templates select=
"actions/errors"/>
248 <xsl:apply-templates select=
"actions/reductions"/>
249 <xsl:apply-templates select=
"actions/transitions">
250 <xsl:with-param name=
"type" select=
"'goto'"/>
251 </xsl:apply-templates>
252 <xsl:apply-templates select=
"solved-conflicts"/>
255 <xsl:template match=
"actions/transitions">
256 <xsl:param name=
"type"/>
257 <xsl:if test=
"transition[@type = $type]">
258 <xsl:text> </xsl:text>
259 <xsl:apply-templates select=
"transition[@type = $type]">
260 <xsl:with-param name=
"pad">
261 <xsl:call-template name=
"max-width-symbol">
262 <xsl:with-param name=
"node" select=
"transition[@type = $type]"/>
265 </xsl:apply-templates>
269 <xsl:template match=
"actions/errors">
270 <xsl:if test=
"error">
271 <xsl:text> </xsl:text>
272 <xsl:apply-templates select=
"error">
273 <xsl:with-param name=
"pad">
274 <xsl:call-template name=
"max-width-symbol">
275 <xsl:with-param name=
"node" select=
"error"/>
278 </xsl:apply-templates>
282 <xsl:template match=
"actions/reductions">
283 <xsl:if test=
"reduction">
284 <xsl:text> </xsl:text>
285 <xsl:apply-templates select=
"reduction">
286 <xsl:with-param name=
"pad">
287 <xsl:call-template name=
"max-width-symbol">
288 <xsl:with-param name=
"node" select=
"reduction"/>
291 </xsl:apply-templates>
295 <xsl:template match=
"item">
296 <xsl:param name=
"pad"/>
297 <xsl:param name=
"prev-rule-number"
298 select=
"preceding-sibling::item[1]/@rule-number"/>
299 <xsl:apply-templates select=
"key('bison:ruleNumber', current()/@rule-number)">
300 <xsl:with-param name=
"itemset" select=
"'true'"/>
301 <xsl:with-param name=
"pad" select=
"$pad"/>
302 <xsl:with-param name=
"prev-lhs"
303 select=
"key('bison:ruleNumber', $prev-rule-number)/lhs[text()]"/>
304 <xsl:with-param name=
"point" select=
"@point"/>
305 <xsl:with-param name=
"lookaheads">
306 <xsl:apply-templates select=
"lookaheads"/>
308 </xsl:apply-templates>
311 <xsl:template match=
"rule">
312 <xsl:param name=
"itemset"/>
313 <xsl:param name=
"pad"/>
314 <xsl:param name=
"prev-lhs"/>
315 <xsl:param name=
"point"/>
316 <xsl:param name=
"lookaheads"/>
318 <xsl:if test=
"$itemset != 'true' and not($prev-lhs = lhs[text()])">
319 <xsl:text> </xsl:text>
322 <xsl:text> </xsl:text>
323 <xsl:call-template name=
"lpad">
324 <xsl:with-param name=
"str" select=
"string(@number)"/>
325 <xsl:with-param name=
"pad" select=
"number($pad)"/>
327 <xsl:text> </xsl:text>
331 <xsl:when test=
"$itemset != 'true' and $prev-lhs = lhs[text()]">
332 <xsl:call-template name=
"lpad">
333 <xsl:with-param name=
"str" select=
"'|'"/>
334 <xsl:with-param name=
"pad" select=
"number(string-length(lhs[text()])) + 1"/>
337 <xsl:when test=
"$itemset = 'true' and $prev-lhs = lhs[text()]">
338 <xsl:call-template name=
"lpad">
339 <xsl:with-param name=
"str" select=
"'|'"/>
340 <xsl:with-param name=
"pad" select=
"number(string-length(lhs[text()])) + 1"/>
344 <xsl:value-of select=
"lhs"/>
345 <xsl:text>:
</xsl:text>
350 <xsl:for-each select=
"rhs/*">
351 <xsl:if test=
"position() = $point + 1">
352 <xsl:text> .
</xsl:text>
354 <xsl:if test=
"$itemset = 'true' and name(.) != 'empty'">
355 <xsl:apply-templates select=
"."/>
357 <xsl:if test=
"$itemset != 'true'">
358 <xsl:apply-templates select=
"."/>
360 <xsl:if test=
"position() = last() and position() = $point">
361 <xsl:text> .
</xsl:text>
364 <xsl:if test=
"$lookaheads">
365 <xsl:value-of select=
"$lookaheads"/>
368 <xsl:text> </xsl:text>
371 <xsl:template match=
"symbol">
372 <xsl:text> </xsl:text>
373 <xsl:value-of select=
"."/>
376 <xsl:template match=
"empty">
377 <xsl:text> /* empty */
</xsl:text>
380 <xsl:template match=
"lookaheads">
381 <xsl:text> [
</xsl:text>
382 <xsl:apply-templates select=
"symbol"/>
383 <xsl:text>]
</xsl:text>
386 <xsl:template match=
"lookaheads/symbol">
387 <xsl:value-of select=
"."/>
388 <xsl:if test=
"position() != last()">
389 <xsl:text>,
</xsl:text>
393 <xsl:template match=
"transition">
394 <xsl:param name=
"pad"/>
395 <xsl:text> </xsl:text>
396 <xsl:call-template name=
"rpad">
397 <xsl:with-param name=
"str" select=
"string(@symbol)"/>
398 <xsl:with-param name=
"pad" select=
"number($pad) + 2"/>
401 <xsl:when test=
"@type = 'shift'">
402 <xsl:text>shift, and go to state
</xsl:text>
403 <xsl:value-of select=
"@state"/>
405 <xsl:when test=
"@type = 'goto'">
406 <xsl:text>go to state
</xsl:text>
407 <xsl:value-of select=
"@state"/>
410 <xsl:text> </xsl:text>
413 <xsl:template match=
"error">
414 <xsl:param name=
"pad"/>
415 <xsl:text> </xsl:text>
416 <xsl:call-template name=
"rpad">
417 <xsl:with-param name=
"str" select=
"string(@symbol)"/>
418 <xsl:with-param name=
"pad" select=
"number($pad) + 2"/>
420 <xsl:text>error
</xsl:text>
421 <xsl:text> (
</xsl:text>
422 <xsl:value-of select=
"text()"/>
423 <xsl:text>)
</xsl:text>
424 <xsl:text> </xsl:text>
427 <xsl:template match=
"reduction">
428 <xsl:param name=
"pad"/>
429 <xsl:text> </xsl:text>
430 <xsl:call-template name=
"rpad">
431 <xsl:with-param name=
"str" select=
"string(@symbol)"/>
432 <xsl:with-param name=
"pad" select=
"number($pad) + 2"/>
434 <xsl:if test=
"@enabled = 'false'">
435 <xsl:text>[
</xsl:text>
438 <xsl:when test=
"@rule = 'accept'">
439 <xsl:text>accept
</xsl:text>
442 <xsl:text>reduce using rule
</xsl:text>
443 <xsl:value-of select=
"@rule"/>
444 <xsl:text> (
</xsl:text>
446 select=
"key('bison:ruleNumber', current()/@rule)/lhs[text()]"/>
447 <xsl:text>)
</xsl:text>
450 <xsl:if test=
"@enabled = 'false'">
451 <xsl:text>]
</xsl:text>
453 <xsl:text> </xsl:text>
456 <xsl:template match=
"solved-conflicts">
457 <xsl:if test=
"resolution">
458 <xsl:text> </xsl:text>
459 <xsl:apply-templates select=
"resolution"/>
463 <xsl:template match=
"resolution">
464 <xsl:text> Conflict between rule
</xsl:text>
465 <xsl:value-of select=
"@rule"/>
466 <xsl:text> and token
</xsl:text>
467 <xsl:value-of select=
"@symbol"/>
468 <xsl:text> resolved as
</xsl:text>
469 <xsl:if test=
"@type = 'error'">
470 <xsl:text>an
</xsl:text>
472 <xsl:value-of select=
"@type"/>
473 <xsl:text> (
</xsl:text>
474 <xsl:value-of select=
"."/>
475 <xsl:text>).
</xsl:text>
478 <xsl:template name=
"max-width-symbol">
479 <xsl:param name=
"node"/>
480 <xsl:variable name=
"longest">
481 <xsl:for-each select=
"$node">
482 <xsl:sort data-type=
"number" select=
"string-length(@symbol)"
484 <xsl:if test=
"position() = 1">
485 <xsl:value-of select=
"string-length(@symbol)"/>
489 <xsl:value-of select=
"$longest"/>
492 <xsl:template name=
"lpad">
493 <xsl:param name=
"str" select=
"''"/>
494 <xsl:param name=
"pad" select=
"0"/>
495 <xsl:variable name=
"diff" select=
"$pad - string-length($str)" />
497 <xsl:when test=
"$diff < 0">
498 <xsl:value-of select=
"$str"/>
501 <xsl:call-template name=
"space">
502 <xsl:with-param name=
"repeat" select=
"$diff"/>
504 <xsl:value-of select=
"$str"/>
509 <xsl:template name=
"rpad">
510 <xsl:param name=
"str" select=
"''"/>
511 <xsl:param name=
"pad" select=
"0"/>
512 <xsl:variable name=
"diff" select=
"$pad - string-length($str)"/>
514 <xsl:when test=
"$diff < 0">
515 <xsl:value-of select=
"$str"/>
518 <xsl:value-of select=
"$str"/>
519 <xsl:call-template name=
"space">
520 <xsl:with-param name=
"repeat" select=
"$diff"/>
526 <xsl:template name=
"space">
527 <xsl:param name=
"repeat">0</xsl:param>
528 <xsl:param name=
"fill" select=
"' '"/>
529 <xsl:if test=
"number($repeat) >= 1">
530 <xsl:call-template name=
"space">
531 <xsl:with-param name=
"repeat" select=
"$repeat - 1"/>
532 <xsl:with-param name=
"fill" select=
"$fill"/>
534 <xsl:value-of select=
"$fill"/>
538 <xsl:template name=
"line-wrap">
539 <xsl:param name=
"line-length"/> <!-- required -->
540 <xsl:param name=
"first-line-length" select=
"$line-length"/>
541 <xsl:param name=
"text"/> <!-- required -->
543 <xsl:when test=
"normalize-space($text) = ''" />
544 <xsl:when test=
"string-length($text) <= $first-line-length">
545 <xsl:value-of select=
"concat($text, ' ')" />
548 <xsl:variable name=
"break-pos">
549 <xsl:call-template name=
"ws-search">
550 <xsl:with-param name=
"text" select=
"$text" />
551 <xsl:with-param name=
"start" select=
"$first-line-length+1" />
554 <xsl:value-of select=
"substring($text, 1, $break-pos - 1)" />
555 <xsl:text> </xsl:text>
556 <xsl:call-template name=
"line-wrap">
557 <xsl:with-param name=
"line-length" select=
"$line-length" />
559 name=
"text" select=
"concat(' ', substring($text, $break-pos+1))"
566 <xsl:template name=
"ws-search">
567 <xsl:param name=
"text"/> <!-- required -->
568 <xsl:param name=
"start"/> <!-- required -->
569 <xsl:variable name=
"search-text" select=
"substring($text, $start)" />
571 <xsl:when test=
"not(contains($search-text, ' '))">
572 <xsl:value-of select=
"string-length($text)+1" />
576 select=
"$start + string-length(substring-before($search-text, ' '))"