Debian Codegraph @

doc/gperf_5.html

raw
1 <HTML>
2 <HEAD>
3 <!-- This HTML file has been created by texi2html 1.52b
4 from gperf.texi on 16 April 2025 -->
5
6 <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
7 <TITLE>Perfect Hash Function Generator - 4 High-Level Description of GNU gperf</TITLE>
8 </HEAD>
9 <BODY>
10 Go to the <A HREF="gperf_1.html">first</A>, <A HREF="gperf_4.html">previous</A>, <A HREF="gperf_6.html">next</A>, <A HREF="gperf_10.html">last</A> section, <A HREF="gperf_toc.html">table of contents</A>.
11 <P><HR><P>
12
13
14 <H1><A NAME="SEC5" HREF="gperf_toc.html#TOC5">4 High-Level Description of GNU <CODE>gperf</CODE></A></H1>
15
16 <P>
17 The perfect hash function generator <CODE>gperf</CODE> reads a set of
18 “keywords” from an input file (or from the standard input by
19 default). It attempts to derive a perfect hashing function that
20 recognizes a member of the <EM>static keyword set</EM> with at most a
21 single probe into the lookup table. If <CODE>gperf</CODE> succeeds in
22 generating such a function it produces a pair of C source code routines
23 that perform hashing and table lookup recognition. All generated C code
24 is directed to the standard output. Command-line options described
25 below allow you to modify the input and output format to <CODE>gperf</CODE>.
26
27 </P>
28 <P>
29 By default, <CODE>gperf</CODE> attempts to produce time-efficient code, with
30 less emphasis on efficient space utilization. However, several options
31 exist that permit trading-off execution time for storage space and vice
32 versa. In particular, expanding the generated table size produces a
33 sparse search structure, generally yielding faster searches.
34 Conversely, you can direct <CODE>gperf</CODE> to utilize a C <CODE>switch</CODE>
35 statement scheme that minimizes data space storage size. Furthermore,
36 using a C <CODE>switch</CODE> may actually speed up the keyword retrieval time
37 somewhat. Actual results depend on your C compiler, of course.
38
39 </P>
40 <P>
41 In general, <CODE>gperf</CODE> assigns values to the bytes it is using
42 for hashing until some set of values gives each keyword a unique value.
43 A helpful heuristic is that the larger the hash value range, the easier
44 it is for <CODE>gperf</CODE> to find and generate a perfect hash function.
45 Experimentation is the key to getting the most from <CODE>gperf</CODE>.
46
47 </P>
48
49
50 <H2><A NAME="SEC6" HREF="gperf_toc.html#TOC6">4.1 Input Format to <CODE>gperf</CODE></A></H2>
51 <P>
52 <A NAME="IDX4"></A>
53 <A NAME="IDX5"></A>
54 <A NAME="IDX6"></A>
55 <A NAME="IDX7"></A>
56 You can control the input file format by varying certain command-line
57 arguments, in particular the <SAMP>&lsquo;-t&rsquo;</SAMP> option. The input's appearance
58 is similar to GNU utilities <CODE>flex</CODE> and <CODE>bison</CODE> (or UNIX
59 utilities <CODE>lex</CODE> and <CODE>yacc</CODE>). Here's an outline of the general
60 format:
61
62 </P>
63
64 <PRE>
65 declarations
66 %%
67 keywords
68 %%
69 functions
70 </PRE>
71
72 <P>
73 <EM>Unlike</EM> <CODE>flex</CODE> or <CODE>bison</CODE>, the declarations section and
74 the functions section are optional. The following sections describe the
75 input format for each section.
76
77 </P>
78
79 <P>
80 It is possible to omit the declaration section entirely, if the <SAMP>&lsquo;-t&rsquo;</SAMP>
81 option is not given. In this case the input file begins directly with the
82 first keyword line, e.g.:
83
84 </P>
85
86 <PRE>
87 january
88 february
89 march
90 april
91 ...
92 </PRE>
93
94
95
96 <H3><A NAME="SEC7" HREF="gperf_toc.html#TOC7">4.1.1 Declarations</A></H3>
97
98 <P>
99 The keyword input file optionally contains a section for including
100 arbitrary C declarations and definitions, <CODE>gperf</CODE> declarations that
101 act like command-line options, as well as for providing a user-supplied
102 <CODE>struct</CODE>.
103
104 </P>
105
106
107
108 <H4><A NAME="SEC8" HREF="gperf_toc.html#TOC8">4.1.1.1 User-supplied <CODE>struct</CODE></A></H4>
109
110 <P>
111 If the <SAMP>&lsquo;-t&rsquo;</SAMP> option (or, equivalently, the <SAMP>&lsquo;%struct-type&rsquo;</SAMP> declaration)
112 <EM>is</EM> enabled, you <EM>must</EM> provide a C <CODE>struct</CODE> as the last
113 component in the declaration section from the input file. The first
114 field in this struct must be of type <CODE>char *</CODE> or <CODE>const char *</CODE>
115 if the <SAMP>&lsquo;-P&rsquo;</SAMP> option is not given, or of type <CODE>int</CODE> if the option
116 <SAMP>&lsquo;-P&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%pic&rsquo;</SAMP> declaration) is enabled.
117 This first field must be called <SAMP>&lsquo;name&rsquo;</SAMP>, although it is possible to modify
118 its name with the <SAMP>&lsquo;-K&rsquo;</SAMP> option (or, equivalently, the
119 <SAMP>&lsquo;%define slot-name&rsquo;</SAMP> declaration) described below.
120
121 </P>
122 <P>
123 Here is a simple example, using months of the year and their attributes as
124 input:
125
126 </P>
127
128 <PRE>
129 struct month { char *name; int number; int days; int leap_days; };
130 %%
131 january, 1, 31, 31
132 february, 2, 28, 29
133 march, 3, 31, 31
134 april, 4, 30, 30
135 may, 5, 31, 31
136 june, 6, 30, 30
137 july, 7, 31, 31
138 august, 8, 31, 31
139 september, 9, 30, 30
140 october, 10, 31, 31
141 november, 11, 30, 30
142 december, 12, 31, 31
143 </PRE>
144
145 <P>
146 <A NAME="IDX8"></A>
147 Separating the <CODE>struct</CODE> declaration from the list of keywords and
148 other fields are a pair of consecutive percent signs, <SAMP>&lsquo;%%&rsquo;</SAMP>,
149 appearing left justified in the first column, as in the UNIX utility
150 <CODE>lex</CODE>.
151
152 </P>
153 <P>
154 If the <CODE>struct</CODE> has already been declared in an include file, it can
155 be mentioned in an abbreviated form, like this:
156
157 </P>
158
159 <PRE>
160 struct month;
161 %%
162 january, 1, 31, 31
163 ...
164 </PRE>
165
166
167
168 <H4><A NAME="SEC9" HREF="gperf_toc.html#TOC9">4.1.1.2 Gperf Declarations</A></H4>
169
170 <P>
171 The declaration section can contain <CODE>gperf</CODE> declarations. They
172 influence the way <CODE>gperf</CODE> works, like command line options do.
173 In fact, every such declaration is equivalent to a command line option.
174 There are three forms of declarations:
175
176 </P>
177
178 <OL>
179 <LI>
180
181 Declarations without argument, like <SAMP>&lsquo;%compare-lengths&rsquo;</SAMP>.
182
183 <LI>
184
185 Declarations with an argument, like <SAMP>&lsquo;%switch=<VAR>count</VAR>&rsquo;</SAMP>.
186
187 <LI>
188
189 Declarations of names of entities in the output file, like
190 <SAMP>&lsquo;%define lookup-function-name <VAR>name</VAR>&rsquo;</SAMP>.
191 </OL>
192
193 <P>
194 When a declaration is given both in the input file and as a command line
195 option, the command-line option's value prevails.
196
197 </P>
198 <P>
199 The following <CODE>gperf</CODE> declarations are available.
200
201 </P>
202 <DL COMPACT>
203
204 <DT><SAMP>&lsquo;%delimiters=<VAR>delimiter-list</VAR>&rsquo;</SAMP>
205 <DD>
206 <A NAME="IDX9"></A>
207 Allows you to provide a string containing delimiters used to
208 separate keywords from their attributes. The default is ",". This
209 option is essential if you want to use keywords that have embedded
210 commas or newlines.
211
212 <DT><SAMP>&lsquo;%struct-type&rsquo;</SAMP>
213 <DD>
214 <A NAME="IDX10"></A>
215 Allows you to include a <CODE>struct</CODE> type declaration for generated
216 code; see above for an example.
217
218 <DT><SAMP>&lsquo;%ignore-case&rsquo;</SAMP>
219 <DD>
220 <A NAME="IDX11"></A>
221 Consider upper and lower case ASCII characters as equivalent. The string
222 comparison will use a case insignificant character comparison. Note that
223 locale dependent case mappings are ignored.
224
225 <DT><SAMP>&lsquo;%language=<VAR>language-name</VAR>&rsquo;</SAMP>
226 <DD>
227 <A NAME="IDX12"></A>
228 Instructs <CODE>gperf</CODE> to generate code in the language specified by the
229 option's argument. Languages handled are currently:
230
231 <DL COMPACT>
232
233 <DT><SAMP>&lsquo;KR-C&rsquo;</SAMP>
234 <DD>
235 Old-style K&#38;R C. This language is understood by old-style C compilers and
236 ANSI C compilers, but ANSI C compilers may flag warnings (or even errors)
237 because of lacking <SAMP>&lsquo;const&rsquo;</SAMP>.
238
239 <DT><SAMP>&lsquo;C&rsquo;</SAMP>
240 <DD>
241 Common C. This language is understood by ANSI C compilers, and also by
242 old-style C compilers, provided that you <CODE>#define const</CODE> to empty
243 for compilers which don't know about this keyword.
244
245 <DT><SAMP>&lsquo;ANSI-C&rsquo;</SAMP>
246 <DD>
247 ANSI C. This language is understood by ANSI C (C89, ISO C90) compilers,
248 ISO C99 compilers, and C++ compilers.
249
250 <DT><SAMP>&lsquo;C++&rsquo;</SAMP>
251 <DD>
252 C++. This language is understood by C++ compilers.
253 </DL>
254
255 The default is ANSI-C.
256
257 <DT><SAMP>&lsquo;%define slot-name <VAR>name</VAR>&rsquo;</SAMP>
258 <DD>
259 <A NAME="IDX13"></A>
260 This declaration is only useful when option <SAMP>&lsquo;-t&rsquo;</SAMP> (or, equivalently, the
261 <SAMP>&lsquo;%struct-type&rsquo;</SAMP> declaration) has been given.
262 By default, the program assumes the structure component identifier for
263 the keyword is <SAMP>&lsquo;name&rsquo;</SAMP>. This option allows an arbitrary choice of
264 identifier for this component, although it still must occur as the first
265 field in your supplied <CODE>struct</CODE>.
266
267 <DT><SAMP>&lsquo;%define initializer-suffix <VAR>initializers</VAR>&rsquo;</SAMP>
268 <DD>
269 <A NAME="IDX14"></A>
270 This declaration is only useful when option <SAMP>&lsquo;-t&rsquo;</SAMP> (or, equivalently, the
271 <SAMP>&lsquo;%struct-type&rsquo;</SAMP> declaration) has been given.
272 It permits to specify initializers for the structure members following
273 <VAR>slot-name</VAR> in empty hash table entries. The list of initializers
274 should start with a comma. By default, the emitted code will
275 zero-initialize structure members following <VAR>slot-name</VAR>.
276
277 <DT><SAMP>&lsquo;%define hash-function-name <VAR>name</VAR>&rsquo;</SAMP>
278 <DD>
279 <A NAME="IDX15"></A>
280 Allows you to specify the name for the generated hash function. Default
281 name is <SAMP>&lsquo;hash&rsquo;</SAMP>. This option permits the use of two hash tables in
282 the same file.
283
284 <DT><SAMP>&lsquo;%define lookup-function-name <VAR>name</VAR>&rsquo;</SAMP>
285 <DD>
286 <A NAME="IDX16"></A>
287 Allows you to specify the name for the generated lookup function.
288 Default name is <SAMP>&lsquo;in_word_set&rsquo;</SAMP>. This option permits multiple
289 generated hash functions to be used in the same application.
290
291 <DT><SAMP>&lsquo;%define class-name <VAR>name</VAR>&rsquo;</SAMP>
292 <DD>
293 <A NAME="IDX17"></A>
294 This option is only useful when option <SAMP>&lsquo;-L C++&rsquo;</SAMP> (or, equivalently,
295 the <SAMP>&lsquo;%language=C++&rsquo;</SAMP> declaration) has been given. It
296 allows you to specify the name of generated C++ class. Default name is
297 <CODE>Perfect_Hash</CODE>.
298
299 <DT><SAMP>&lsquo;%7bit&rsquo;</SAMP>
300 <DD>
301 <A NAME="IDX18"></A>
302 This option specifies that all strings that will be passed as arguments
303 to the generated hash function and the generated lookup function will
304 solely consist of 7-bit ASCII characters (bytes in the range 0..127).
305 (Note that the ANSI C functions <CODE>isalnum</CODE> and <CODE>isgraph</CODE> do
306 <EM>not</EM> guarantee that a byte is in this range. Only an explicit
307 test like <SAMP>&lsquo;c &#62;= 'A' &#38;&#38; c &#60;= 'Z'&rsquo;</SAMP> guarantees this.)
308
309 <DT><SAMP>&lsquo;%compare-lengths&rsquo;</SAMP>
310 <DD>
311 <A NAME="IDX19"></A>
312 Compare keyword lengths before trying a string comparison. This option
313 is mandatory for binary comparisons (see section <A HREF="gperf_5.html#SEC15">4.3 Use of NUL bytes</A>). It also might
314 cut down on the number of string comparisons made during the lookup, since
315 keywords with different lengths are never compared via <CODE>strcmp</CODE>.
316 However, using <SAMP>&lsquo;%compare-lengths&rsquo;</SAMP> might greatly increase the size of the
317 generated C code if the lookup table range is large (which implies that
318 the switch option <SAMP>&lsquo;-S&rsquo;</SAMP> or <SAMP>&lsquo;%switch&rsquo;</SAMP> is not enabled), since the length
319 table contains as many elements as there are entries in the lookup table.
320
321 <DT><SAMP>&lsquo;%compare-strncmp&rsquo;</SAMP>
322 <DD>
323 <A NAME="IDX20"></A>
324 Generates C code that uses the <CODE>strncmp</CODE> function to perform
325 string comparisons. The default action is to use <CODE>strcmp</CODE>.
326
327 <DT><SAMP>&lsquo;%readonly-tables&rsquo;</SAMP>
328 <DD>
329 <A NAME="IDX21"></A>
330 Makes the contents of all generated lookup tables constant, i.e.,
331 “readonly”. Many compilers can generate more efficient code for this
332 by putting the tables in readonly memory.
333
334 <DT><SAMP>&lsquo;%enum&rsquo;</SAMP>
335 <DD>
336 <A NAME="IDX22"></A>
337 Define constant values using an enum local to the lookup function rather
338 than with #defines. This also means that different lookup functions can
339 reside in the same file. Thanks to James Clark <CODE>&#60;[email protected]&#62;</CODE>.
340
341 <DT><SAMP>&lsquo;%includes&rsquo;</SAMP>
342 <DD>
343 <A NAME="IDX23"></A>
344 Include the necessary system include file, <CODE>&#60;string.h&#62;</CODE>, at the
345 beginning of the code. By default, this is not done; the user must
346 include this header file himself to allow compilation of the code.
347
348 <DT><SAMP>&lsquo;%global-table&rsquo;</SAMP>
349 <DD>
350 <A NAME="IDX24"></A>
351 Generate the static table of keywords as a static global variable,
352 rather than hiding it inside of the lookup function (which is the
353 default behavior).
354
355 <DT><SAMP>&lsquo;%pic&rsquo;</SAMP>
356 <DD>
357 <A NAME="IDX25"></A>
358 Optimize the generated table for inclusion in shared libraries. This
359 reduces the startup time of programs using a shared library containing
360 the generated code. If the <SAMP>&lsquo;%struct-type&rsquo;</SAMP> declaration (or,
361 equivalently, the option <SAMP>&lsquo;-t&rsquo;</SAMP>) is also given, the first field of the
362 user-defined struct must be of type <SAMP>&lsquo;int&rsquo;</SAMP>, not <SAMP>&lsquo;char *&rsquo;</SAMP>, because
363 it will contain offsets into the string pool instead of actual strings.
364 To convert such an offset to a string, you can use the expression
365 <SAMP>&lsquo;stringpool + <VAR>o</VAR>&rsquo;</SAMP>, where <VAR>o</VAR> is the offset. The string pool
366 name can be changed through the <SAMP>&lsquo;%define string-pool-name&rsquo;</SAMP> declaration.
367
368 <DT><SAMP>&lsquo;%define string-pool-name <VAR>name</VAR>&rsquo;</SAMP>
369 <DD>
370 <A NAME="IDX26"></A>
371 Allows you to specify the name of the generated string pool created by
372 the declaration <SAMP>&lsquo;%pic&rsquo;</SAMP> (or, equivalently, the option <SAMP>&lsquo;-P&rsquo;</SAMP>).
373 The default name is <SAMP>&lsquo;stringpool&rsquo;</SAMP>. This declaration permits the use of
374 two hash tables in the same file, with <SAMP>&lsquo;%pic&rsquo;</SAMP> and even when the
375 <SAMP>&lsquo;%global-table&rsquo;</SAMP> declaration (or, equivalently, the option <SAMP>&lsquo;-G&rsquo;</SAMP>)
376 is given.
377
378 <DT><SAMP>&lsquo;%null-strings&rsquo;</SAMP>
379 <DD>
380 <A NAME="IDX27"></A>
381 Use NULL strings instead of empty strings for empty keyword table entries.
382 This reduces the startup time of programs using a shared library containing
383 the generated code (but not as much as the declaration <SAMP>&lsquo;%pic&rsquo;</SAMP>), at the
384 expense of one more test-and-branch instruction at run time.
385
386 <DT><SAMP>&lsquo;%define constants-prefix <VAR>prefix</VAR>&rsquo;</SAMP>
387 <DD>
388 <A NAME="IDX28"></A>
389 Allows you to specify a prefix for the constants <CODE>TOTAL_KEYWORDS</CODE>,
390 <CODE>MIN_WORD_LENGTH</CODE>, <CODE>MAX_WORD_LENGTH</CODE>, and so on. This option
391 permits the use of two hash tables in the same file, even when the option
392 <SAMP>&lsquo;-E&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%enum&rsquo;</SAMP> declaration) is not given or
393 the option <SAMP>&lsquo;-G&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%global-table&rsquo;</SAMP> declaration)
394 is given.
395
396 <DT><SAMP>&lsquo;%define word-array-name <VAR>name</VAR>&rsquo;</SAMP>
397 <DD>
398 <A NAME="IDX29"></A>
399 Allows you to specify the name for the generated array containing the
400 hash table. Default name is <SAMP>&lsquo;wordlist&rsquo;</SAMP>. This option permits the
401 use of two hash tables in the same file, even when the option <SAMP>&lsquo;-G&rsquo;</SAMP>
402 (or, equivalently, the <SAMP>&lsquo;%global-table&rsquo;</SAMP> declaration) is given.
403
404 <DT><SAMP>&lsquo;%define length-table-name <VAR>name</VAR>&rsquo;</SAMP>
405 <DD>
406 <A NAME="IDX30"></A>
407 Allows you to specify the name for the generated array containing the
408 length table. Default name is <SAMP>&lsquo;lengthtable&rsquo;</SAMP>. This option permits the
409 use of two length tables in the same file, even when the option <SAMP>&lsquo;-G&rsquo;</SAMP>
410 (or, equivalently, the <SAMP>&lsquo;%global-table&rsquo;</SAMP> declaration) is given.
411
412 <DT><SAMP>&lsquo;%switch=<VAR>count</VAR>&rsquo;</SAMP>
413 <DD>
414 <A NAME="IDX31"></A>
415 Causes the generated C code to use a <CODE>switch</CODE> statement scheme,
416 rather than an array lookup table. This can lead to a reduction in both
417 time and space requirements for some input files. The argument to this
418 option determines how many <CODE>switch</CODE> statements are generated. A
419 value of 1 generates 1 <CODE>switch</CODE> containing all the elements, a
420 value of 2 generates 2 tables with 1/2 the elements in each
421 <CODE>switch</CODE>, etc. This is useful since many C compilers cannot
422 correctly generate code for large <CODE>switch</CODE> statements. This option
423 was inspired in part by Keith Bostic's original C program.
424
425 <DT><SAMP>&lsquo;%omit-struct-type&rsquo;</SAMP>
426 <DD>
427 <A NAME="IDX32"></A>
428 Prevents the transfer of the type declaration to the output file. Use
429 this option if the type is already defined elsewhere.
430 </DL>
431
432
433
434 <H4><A NAME="SEC10" HREF="gperf_toc.html#TOC10">4.1.1.3 C Code Inclusion</A></H4>
435
436 <P>
437 <A NAME="IDX33"></A>
438 <A NAME="IDX34"></A>
439 Using a syntax similar to GNU utilities <CODE>flex</CODE> and <CODE>bison</CODE>, it
440 is possible to directly include C source text and comments verbatim into
441 the generated output file. This is accomplished by enclosing the region
442 inside left-justified surrounding <SAMP>&lsquo;%{&rsquo;</SAMP>, <SAMP>&lsquo;%}&rsquo;</SAMP> pairs. Here is
443 an input fragment based on the previous example that illustrates this
444 feature:
445
446 </P>
447
448 <PRE>
449 %{
450 #include &#60;assert.h&#62;
451 /* This section of code is inserted directly into the output. */
452 int return_month_days (struct month *months, int is_leap_year);
453 %}
454 struct month { char *name; int number; int days; int leap_days; };
455 %%
456 january, 1, 31, 31
457 february, 2, 28, 29
458 march, 3, 31, 31
459 ...
460 </PRE>
461
462
463
464 <H3><A NAME="SEC11" HREF="gperf_toc.html#TOC11">4.1.2 Format for Keyword Entries</A></H3>
465
466 <P>
467 The second input file format section contains lines of keywords and any
468 associated attributes you might supply. A line beginning with <SAMP>&lsquo;#&rsquo;</SAMP>
469 in the first column is considered a comment. Everything following the
470 <SAMP>&lsquo;#&rsquo;</SAMP> is ignored, up to and including the following newline. A line
471 beginning with <SAMP>&lsquo;%&rsquo;</SAMP> in the first column is an option declaration and
472 must not occur within the keywords section.
473
474 </P>
475 <P>
476 The first field of each non-comment line is always the keyword itself. It
477 can be given in two ways: as a simple name, i.e., without surrounding
478 string quotation marks, or as a string enclosed in double-quotes, in
479 C syntax, possibly with backslash escapes like <CODE>\"</CODE> or <CODE>\234</CODE>
480 or <CODE>\xa8</CODE>. In either case, it must start right at the beginning
481 of the line, without leading whitespace.
482 In this context, a “field” is considered to extend up to, but
483 not include, the first blank, comma, or newline. Here is a simple
484 example taken from a partial list of C reserved words:
485
486 </P>
487
488 <PRE>
489 # These are a few C reserved words, see the c.gperf file
490 # for a complete list of ANSI C reserved words.
491 unsigned
492 sizeof
493 switch
494 signed
495 if
496 default
497 for
498 while
499 return
500 </PRE>
501
502 <P>
503 Note that unlike <CODE>flex</CODE> or <CODE>bison</CODE> the first <SAMP>&lsquo;%%&rsquo;</SAMP> marker
504 may be elided if the declaration section is empty.
505
506 </P>
507 <P>
508 Additional fields may optionally follow the leading keyword. Fields
509 should be separated by commas, and terminate at the end of line. What
510 these fields mean is entirely up to you; they are used to initialize the
511 elements of the user-defined <CODE>struct</CODE> provided by you in the
512 declaration section. If the <SAMP>&lsquo;-t&rsquo;</SAMP> option (or, equivalently, the
513 <SAMP>&lsquo;%struct-type&rsquo;</SAMP> declaration) is <EM>not</EM> enabled
514 these fields are simply ignored. All previous examples except the last
515 one contain keyword attributes.
516
517 </P>
518
519
520 <H3><A NAME="SEC12" HREF="gperf_toc.html#TOC12">4.1.3 Including Additional C Functions</A></H3>
521
522 <P>
523 The optional third section also corresponds closely with conventions
524 found in <CODE>flex</CODE> and <CODE>bison</CODE>. All text in this section,
525 starting at the final <SAMP>&lsquo;%%&rsquo;</SAMP> and extending to the end of the input
526 file, is included verbatim into the generated output file. Naturally,
527 it is your responsibility to ensure that the code contained in this
528 section is valid C.
529
530 </P>
531
532
533 <H3><A NAME="SEC13" HREF="gperf_toc.html#TOC13">4.1.4 Where to place directives for GNU <CODE>indent</CODE>.</A></H3>
534
535 <P>
536 If you want to invoke GNU <CODE>indent</CODE> on a <CODE>gperf</CODE> input file,
537 you will see that GNU <CODE>indent</CODE> doesn't understand the <SAMP>&lsquo;%%&rsquo;</SAMP>,
538 <SAMP>&lsquo;%{&rsquo;</SAMP> and <SAMP>&lsquo;%}&rsquo;</SAMP> directives that control <CODE>gperf</CODE>'s
539 interpretation of the input file. Therefore you have to insert some
540 directives for GNU <CODE>indent</CODE>. More precisely, assuming the most
541 general input file structure
542
543 </P>
544
545 <PRE>
546 declarations part 1
547 %{
548 verbatim code
549 %}
550 declarations part 2
551 %%
552 keywords
553 %%
554 functions
555 </PRE>
556
557 <P>
558 you would insert <SAMP>&lsquo;*INDENT-OFF*&rsquo;</SAMP> and <SAMP>&lsquo;*INDENT-ON*&rsquo;</SAMP> comments
559 as follows:
560
561 </P>
562
563 <PRE>
564 /* *INDENT-OFF* */
565 declarations part 1
566 %{
567 /* *INDENT-ON* */
568 verbatim code
569 /* *INDENT-OFF* */
570 %}
571 declarations part 2
572 %%
573 keywords
574 %%
575 /* *INDENT-ON* */
576 functions
577 </PRE>
578
579
580
581 <H2><A NAME="SEC14" HREF="gperf_toc.html#TOC14">4.2 Output Format for Generated C Code with <CODE>gperf</CODE></A></H2>
582 <P>
583 <A NAME="IDX35"></A>
584
585 </P>
586 <P>
587 Several options control how the generated C code appears on the standard
588 output. Two C functions are generated. They are called <CODE>hash</CODE> and
589 <CODE>in_word_set</CODE>, although you may modify their names with a command-line
590 option. Both functions require two arguments, a string, <CODE>char *</CODE>
591 <VAR>str</VAR>, and a length parameter, <CODE>int</CODE> <VAR>len</VAR>. Their default
592 function prototypes are as follows:
593
594 </P>
595 <P>
596 <DL>
597 <DT><U>Function:</U> unsigned int <B>hash</B> <I>(const char * <VAR>str</VAR>, size_t <VAR>len</VAR>)</I>
598 <DD><A NAME="IDX36"></A>
599 By default, the generated <CODE>hash</CODE> function returns an integer value
600 created by adding <VAR>len</VAR> to several user-specified <VAR>str</VAR> byte
601 positions indexed into an <EM>associated values</EM> table stored in a
602 local static array. The associated values table is constructed
603 internally by <CODE>gperf</CODE> and later output as a static local C array
604 called <SAMP>&lsquo;hash_table&rsquo;</SAMP>. The relevant selected positions (i.e. indices
605 into <VAR>str</VAR>) are specified via the <SAMP>&lsquo;-k&rsquo;</SAMP> option when running
606 <CODE>gperf</CODE>, as detailed in the <EM>Options</EM> section below (see section <A HREF="gperf_6.html#SEC18">5 Invoking <CODE>gperf</CODE></A>).
607 </DL>
608
609 </P>
610 <P>
611 <DL>
612 <DT><U>Function:</U> <B>in_word_set</B> <I>(const char * <VAR>str</VAR>, size_t <VAR>len</VAR>)</I>
613 <DD><A NAME="IDX37"></A>
614 If <VAR>str</VAR> is in the keyword set, returns a pointer to that
615 keyword. More exactly, if the option <SAMP>&lsquo;-t&rsquo;</SAMP> (or, equivalently, the
616 <SAMP>&lsquo;%struct-type&rsquo;</SAMP> declaration) was given, it returns
617 a pointer to the matching keyword's structure. Otherwise it returns
618 <CODE>NULL</CODE>.
619 </DL>
620
621 </P>
622 <P>
623 If the option <SAMP>&lsquo;-c&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%compare-strncmp&rsquo;</SAMP>
624 declaration) is not used, <VAR>str</VAR> must be a NUL terminated
625 string of exactly length <VAR>len</VAR>. If <SAMP>&lsquo;-c&rsquo;</SAMP> (or, equivalently, the
626 <SAMP>&lsquo;%compare-strncmp&rsquo;</SAMP> declaration) is used, <VAR>str</VAR> must
627 simply be an array of <VAR>len</VAR> bytes and does not need to be NUL
628 terminated.
629
630 </P>
631 <P>
632 The code generated for these two functions is affected by the following
633 options:
634
635 </P>
636 <DL COMPACT>
637
638 <DT><SAMP>&lsquo;-t&rsquo;</SAMP>
639 <DD>
640 <DT><SAMP>&lsquo;--struct-type&rsquo;</SAMP>
641 <DD>
642 Make use of the user-defined <CODE>struct</CODE>.
643
644 <DT><SAMP>&lsquo;-S <VAR>total-switch-statements</VAR>&rsquo;</SAMP>
645 <DD>
646 <DT><SAMP>&lsquo;--switch=<VAR>total-switch-statements</VAR>&rsquo;</SAMP>
647 <DD>
648 <A NAME="IDX38"></A>
649 Generate 1 or more C <CODE>switch</CODE> statement rather than use a large,
650 (and potentially sparse) static array. Although the exact time and
651 space savings of this approach vary according to your C compiler's
652 degree of optimization, this method often results in smaller and faster
653 code.
654 </DL>
655
656 <P>
657 If the <SAMP>&lsquo;-t&rsquo;</SAMP> and <SAMP>&lsquo;-S&rsquo;</SAMP> options (or, equivalently, the
658 <SAMP>&lsquo;%struct-type&rsquo;</SAMP> and <SAMP>&lsquo;%switch&rsquo;</SAMP> declarations) are omitted, the default
659 action
660 is to generate a <CODE>char *</CODE> array containing the keywords, together with
661 additional empty strings used for padding the array. By experimenting
662 with the various input and output options, and timing the resulting C
663 code, you can determine the best option choices for different keyword
664 set characteristics.
665
666 </P>
667
668
669 <H2><A NAME="SEC15" HREF="gperf_toc.html#TOC15">4.3 Use of NUL bytes</A></H2>
670 <P>
671 <A NAME="IDX39"></A>
672
673 </P>
674 <P>
675 By default, the code generated by <CODE>gperf</CODE> operates on zero
676 terminated strings, the usual representation of strings in C. This means
677 that the keywords in the input file must not contain NUL bytes,
678 and the <VAR>str</VAR> argument passed to <CODE>hash</CODE> or <CODE>in_word_set</CODE>
679 must be NUL terminated and have exactly length <VAR>len</VAR>.
680
681 </P>
682 <P>
683 If option <SAMP>&lsquo;-c&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%compare-strncmp&rsquo;</SAMP>
684 declaration) is used, then the <VAR>str</VAR> argument does not need
685 to be NUL terminated. The code generated by <CODE>gperf</CODE> will only
686 access the first <VAR>len</VAR>, not <VAR>len+1</VAR>, bytes starting at <VAR>str</VAR>.
687 However, the keywords in the input file still must not contain NUL
688 bytes.
689
690 </P>
691 <P>
692 If option <SAMP>&lsquo;-l&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%compare-lengths&rsquo;</SAMP>
693 declaration) is used, then the hash table performs binary
694 comparison. The keywords in the input file may contain NUL bytes,
695 written in string syntax as <CODE>\000</CODE> or <CODE>\x00</CODE>, and the code
696 generated by <CODE>gperf</CODE> will treat NUL like any other byte.
697 Also, in this case the <SAMP>&lsquo;-c&rsquo;</SAMP> option (or, equivalently, the
698 <SAMP>&lsquo;%compare-strncmp&rsquo;</SAMP> declaration) is ignored.
699
700 </P>
701
702
703 <H2><A NAME="SEC16" HREF="gperf_toc.html#TOC16">4.4 Controlling Identifiers</A></H2>
704
705 <P>
706 The identifiers of the functions, tables, and constants defined by the code
707 generated by <CODE>gperf</CODE> can be controlled through <CODE>gperf</CODE> declarations
708 or the equivalent command-line options. This is useful for three purposes:
709
710 </P>
711
712 <UL>
713 <LI>
714
715 Esthetics of the generated code.
716
717 For this purpose, just use the available declarations or options at will.
718
719 <LI>
720
721 Controlling the exported identifiers of a library.
722
723 Assume you include code generated by <CODE>gperf</CODE> in a library, and to
724 avoid collisions with other libraries, you want to ensure that all exported
725 identifiers of this library start with a certain prefix.
726
727 By default, the only exported identifier is the lookup function. You can
728 therefore use the option <SAMP>&lsquo;-N&rsquo;</SAMP> (or, equivalently, the
729 <SAMP>&lsquo;%define lookup-function-name&rsquo;</SAMP> declaration).
730
731 When you use the option <SAMP>&lsquo;-L C++&rsquo;</SAMP> (or, equivalently, the
732 <SAMP>&lsquo;%language=C++&rsquo;</SAMP> declaration), the only exported entity is a class.
733 You control its name through the option <SAMP>&lsquo;-Z&rsquo;</SAMP> (or, equivalently, the
734 <SAMP>&lsquo;%define class-name&rsquo;</SAMP> declaration).
735
736 <LI>
737
738 Allowing multiple <CODE>gperf</CODE> generated codes in a single compilation unit.
739
740 Assume you invoke <CODE>gperf</CODE> multiple times, with different input files,
741 and want the generated code to be included from the same source file. In this
742 case, you have to customize not only the exported identifiers, but also the
743 names of functions with <SAMP>&lsquo;static&rsquo;</SAMP> scope, types, and constants.
744
745 By default, you will have to deal with the lookup function, the hash
746 function, and the constants. You should therefore use the option <SAMP>&lsquo;-N&rsquo;</SAMP>
747 (or, equivalently, the <SAMP>&lsquo;%define lookup-function-name&rsquo;</SAMP> declaration),
748 the option <SAMP>&lsquo;-H&rsquo;</SAMP> (or, equivalently, the
749 <SAMP>&lsquo;%define hash-function-name&rsquo;</SAMP> declaration), and the option
750 <SAMP>&lsquo;--constants-prefix&rsquo;</SAMP> (or, equivalently, the
751 <SAMP>&lsquo;%define constants-prefix&rsquo;</SAMP> declaration).
752
753 If you use the option <SAMP>&lsquo;-G&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%global-table&rsquo;</SAMP>
754 declaration), you will also have to deal with the word array, the length
755 table if present, and the string pool if present. This means: You should
756 use the option <SAMP>&lsquo;-W&rsquo;</SAMP> (or, equivalently, the
757 <SAMP>&lsquo;%define word-array-name&rsquo;</SAMP> declaration). If you use the option
758 <SAMP>&lsquo;-l&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%compare-lengths&rsquo;</SAMP> declaration), you
759 should use the option <SAMP>&lsquo;--length-table-name&rsquo;</SAMP> (or, equivalently, the
760 <SAMP>&lsquo;%define length-table-name&rsquo;</SAMP> declaration). If you use the option
761 <SAMP>&lsquo;-P&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%pic&rsquo;</SAMP> declaration), you should use
762 the option <SAMP>&lsquo;-Q&rsquo;</SAMP> (or, equivalently, the <SAMP>&lsquo;%define string-pool-name&rsquo;</SAMP>
763 declaration).
764 </UL>
765
766
767
768 <H2><A NAME="SEC17" HREF="gperf_toc.html#TOC17">4.5 The Copyright of the Output</A></H2>
769 <P>
770 <A NAME="IDX40"></A>
771
772 </P>
773 <P>
774 <CODE>gperf</CODE> is under GPL, but that does not cause the output produced
775 by <CODE>gperf</CODE> to be under GPL. The reason is that the output contains
776 only small pieces of text that come directly from <CODE>gperf</CODE>'s source
777 code -- only about 7 lines long, too small for being significant --, and
778 therefore the output is not a “work based on <CODE>gperf</CODE>” (in the
779 sense of the GPL version 3).
780
781 </P>
782 <P>
783 On the other hand, the output produced by <CODE>gperf</CODE> contains
784 essentially all of the input file. Therefore the output is a
785 “derivative work” of the input (in the sense of U.S. copyright law);
786 and its copyright status depends on the copyright of the input. For most
787 software licenses, the result is that the output is under the same
788 license, with the same copyright holder, as the input that was passed to
789 <CODE>gperf</CODE>.
790
791 </P>
792 <P><HR><P>
793 Go to the <A HREF="gperf_1.html">first</A>, <A HREF="gperf_4.html">previous</A>, <A HREF="gperf_6.html">next</A>, <A HREF="gperf_10.html">last</A> section, <A HREF="gperf_toc.html">table of contents</A>.
794 </BODY>
795 </HTML>