File: node30.html

package info (click to toggle)
kimwitu-doc 10a-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 1,192 kB
  • ctags: 341
  • sloc: makefile: 166; yacc: 125; ansic: 40; lex: 18; sh: 2
file content (197 lines) | stat: -rw-r--r-- 8,189 bytes parent folder | download | duplicates (2)
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
197
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--Converted with LaTeX2HTML 98.1p1 release (March 2nd, 1998)
originally by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>Unparsing</TITLE>
<META NAME="description" CONTENT="Unparsing">
<META NAME="keywords" CONTENT="tpman">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="tpman.css">
<LINK REL="next" HREF="node31.html">
<LINK REL="previous" HREF="node29.html">
<LINK REL="up" HREF="node28.html">
<LINK REL="next" HREF="node31.html">
</HEAD>
<BODY >
<!--Navigation Panel-->
<A NAME="tex2html499"
 HREF="node31.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
 SRC="/usr/share/latex2html/icons/next.png"></A> 
<A NAME="tex2html495"
 HREF="node28.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
 SRC="/usr/share/latex2html/icons/up.png"></A> 
<A NAME="tex2html489"
 HREF="node29.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
 SRC="/usr/share/latex2html/icons/prev.png"></A> 
<A NAME="tex2html497"
 HREF="node4.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
 SRC="/usr/share/latex2html/icons/contents.png"></A> 
<A NAME="tex2html498"
 HREF="node58.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index"
 SRC="/usr/share/latex2html/icons/index.png"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html500"
 HREF="node31.html">Attribute Grammars</A>
<B> Up:</B> <A NAME="tex2html496"
 HREF="node28.html">Cookbook</A>
<B> Previous:</B> <A NAME="tex2html490"
 HREF="node29.html">Structural Induction</A>
<BR>
<BR>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION00092000000000000000">&#160;</A>
<A NAME="cook-unp">&#160;</A>
<BR>
Unparsing
</H2>

<P>
Unparsing is in some ways a special case of structural induction.
An example of an unparsing definition has been given in Section&nbsp;<A HREF="node13.html#unp-def">1.7</A>.
In this subsection we show how a printer function can look that implements indentation.
The idea is to define special control sequences to indicate the increment or decrement of
indentation.
We have chosen these control sequences in accordance with the SSL style.
For example, the string
<PRE><HR><!--lgrindfile: unparse_control_sequences.k 16:19 Sep 28 1992-->
<code>"start%t%nlevel1%nlevel1%b%nend"</code>
<HR></PRE>

should be printed as follows.
<PRE><HR><!--lgrindfile: unparse_output.k 16:19 Sep 28 1992-->
start
        level1
        level1
end
<HR></PRE>

<P>
An example of a printer function to do that is the following:
<A NAME="1088">&#160;</A><A NAME="1089">&#160;</A>
<PRE><HR><!--lgrindfile: printer_function_with_indentation.k 14:04 Feb 19 1997-->
<B>#include</B> &lt;stdio.h&gt;
<B>#include</B> <code>"unpk.h"</code>
<B>static</B> indent=0;
<BR>
<B>void</B> printer(<B>char</B> *s, uview v) {
    <B>char</B> c; <B>int</B> j;
<BR>
    <B>while</B>(c=*s++) {
        <B>if</B> (c!=<code>'%'</code>) putchar(c);
        <B>else</B> <B>switch</B>(c=*s++) {
        <B>case</B> <code>'b'</code>:       indent-; <B>break</B>;
        <B>case</B> <code>'t'</code>:       indent++; <B>break</B>;
        <B>case</B> <code>'n'</code>:       putchar(<code>'&#92;n'</code>);
                        <B>for</B> (j=indent; j&gt;0; j-) putchar(<code>'&#92;t'</code>);
                        <B>break</B>;
        <B>case</B> <code>'&#92;0'</code>:  <B>return</B>;
        <B>default</B>:        putchar(c);
}}}
<HR></PRE>

<P>
The approach above has an advantage that is at the same time a disadvantage.
The <I>printer</I> function also prints <I>casestring</I> and <I>nocasestring</I> terms,
which has the advantage that it is possible to dynamically adapt the indentation, simply by assigning an
unparsing control sequence to a <I>casestring</I> and <I>nocasestring</I> variable before it will be unparsed.
The disadvantage is that one has to be careful that <I>casestring</I> and <I>nocasestring</I> terms do not contain
unexpected unparsing control sequences - it may be necessary to quote the `escape character' (the character % in the example above)
in <I>casestring</I> and <I>nocasestring</I> terms, unless the `escape character' does
not normally appear in the <I>casestring</I> and <I>nocasestring</I> terms.

<P>
The following alternative does not have this problem - but neither has it the advantage of `dynamic' control sequences.
The idea is to use unparse <I>views</I> instead of control sequences - remember that the <I>printer</I> function was passed both
a string and a view argument.
Instead of a single string, a number of strings are unparsed, some of which have <EM>:view</EM> postfix - these strings are
only meant to invoke the printer function with a `control view'.
To reduce the number of printer calls, the `control views' can be combined with the `normal' strings.
This is left as an exercise for the reader.
In the following example the printer function recognizes newline characters ('
<!-- MATH: $\backslash$ -->
<IMG
 WIDTH="11" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
 SRC="img42.gif"
 ALT="$\backslash$">n'):
<PRE><HR><!--lgrindfile: unparse_control_view_sequences.k 13:12 Oct 26 1992-->
<code>"start"</code> <code>""</code>:v_right <code>"&#92;nlevel1&#92;nlevel1"</code> <code>""</code>:v_left <code>"&#92;nend"</code>
<HR></PRE>
The printer function:
<PRE><HR><!--lgrindfile: printer_function_with_control_view_indentation.k 14:04 Feb 19 1997-->
<B>#include</B> &lt;stdio.h&gt;
<B>#include</B> <code>"unpk.h"</code>
<BR>
<B>void</B> printer(<B>char</B> *s, uview v) {
    <B>char</B> c; <B>int</B> j;
    <B>static</B> indent=0;    <I>/* static here, or static at file level */</I>
<BR>
    <B>switch</B>(v) {
    <B>case</B> v_left:        indent-; <B>break</B>;
    <B>case</B> v_right:       indent++; <B>break</B>;
    <B>default</B>:            <B>while</B>(c=*s++) {
                            <B>switch</B>(c){
                            <B>case</B> <code>'&#92;n'</code>:putchar(c);
                                        <B>for</B> (j=indent; j&gt;0; j-) putchar(<code>'&#92;t'</code>);
                                        <B>break</B>;
                            <B>default</B>:    putchar(c); <B>break</B>;
}}}}
<HR></PRE>
If instead of newline characters a newline view would be used (eg. <I>"":v_nl</I>) the printer function
could be simplified even more: the <I>case '</I>
<!-- MATH: $\backslash$ -->
<IMG
 WIDTH="11" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
 SRC="img42.gif"
 ALT="$\backslash$"><I>n':</I> code in the <I>switch(c)</I> can then be
placed as <I>case v_nl:</I> in the <I>switch(v)</I>, and the code for the <I>default:</I> case in that <I>switch(v)</I>
can be replaced by a simple <I>printf("%s",s); </I>.

<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html499"
 HREF="node31.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
 SRC="/usr/share/latex2html/icons/next.png"></A> 
<A NAME="tex2html495"
 HREF="node28.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
 SRC="/usr/share/latex2html/icons/up.png"></A> 
<A NAME="tex2html489"
 HREF="node29.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
 SRC="/usr/share/latex2html/icons/prev.png"></A> 
<A NAME="tex2html497"
 HREF="node4.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
 SRC="/usr/share/latex2html/icons/contents.png"></A> 
<A NAME="tex2html498"
 HREF="node58.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index"
 SRC="/usr/share/latex2html/icons/index.png"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html500"
 HREF="node31.html">Attribute Grammars</A>
<B> Up:</B> <A NAME="tex2html496"
 HREF="node28.html">Cookbook</A>
<B> Previous:</B> <A NAME="tex2html490"
 HREF="node29.html">Structural Induction</A>
<!--End of Navigation Panel-->
<ADDRESS>
<I></I>
<BR><I>2000-04-17</I>
</ADDRESS>
</BODY>
</HTML>