]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/gperf/doc/gperf.7
Import mandoc 1.14.3
[FreeBSD/FreeBSD.git] / contrib / gperf / doc / gperf.7
1 .Dd 2015-03-02
2 .Dt GPERF 7
3 .Os
4 .Sh NAME
5 .Nm gperf
6 .Nd Perfect Hash Function Generator
7 .Sh  Introduction
8 This manual documents the GNU
9 .Li gperf
10 perfect hash function generator utility, focusing on its features and how
11 to use them, and how to report bugs.
12 .Pp
13 .Sh  GNU GENERAL PUBLIC LICENSE
14 .Bd -filled -offset indent
15 Copyright \(co 1989, 1991 Free Software Foundation, Inc., 59 Temple Place, Suite
16 330, Boston, MA 02111-1307, USA.
17 .Pp
18 Everyone is permitted to copy and distribute verbatim copies of this license
19 document, but changing it is not allowed.
20 .Ed
21 .Pp
22 .Ss  Preamble
23 The licenses for most software are designed to take away your freedom to share
24 and change it. By contrast, the GNU General Public License is intended to
25 guarantee your freedom to share and change free software---to make sure the
26 software is free for all its users. This General Public License applies to
27 most of the Free Software Foundation's software and to any other program whose
28 authors commit to using it. (Some other Free Software Foundation software
29 is covered by the GNU Library General Public License instead.) You can apply
30 it to your programs, too.
31 .Pp
32 When we speak of free software, we are referring to freedom, not price. Our
33 General Public Licenses are designed to make sure that you have the freedom
34 to distribute copies of free software (and charge for this service if you
35 wish), that you receive source code or can get it if you want it, that you
36 can change the software or use pieces of it in new free programs; and that
37 you know you can do these things.
38 .Pp
39 To protect your rights, we need to make restrictions that forbid anyone to
40 deny you these rights or to ask you to surrender the rights. These restrictions
41 translate to certain responsibilities for you if you distribute copies of
42 the software, or if you modify it.
43 .Pp
44 For example, if you distribute copies of such a program, whether gratis or
45 for a fee, you must give the recipients all the rights that you have. You
46 must make sure that they, too, receive or can get the source code. And you
47 must show them these terms so they know their rights.
48 .Pp
49 We protect your rights with two steps: (1) copyright the software, and (2)
50 offer you this license which gives you legal permission to copy, distribute
51 and/or modify the software.
52 .Pp
53 Also, for each author's protection and ours, we want to make certain that
54 everyone understands that there is no warranty for this free software. If
55 the software is modified by someone else and passed on, we want its recipients
56 to know that what they have is not the original, so that any problems introduced
57 by others will not reflect on the original authors' reputations.
58 .Pp
59 Finally, any free program is threatened constantly by software patents. We
60 wish to avoid the danger that redistributors of a free program will individually
61 obtain patent licenses, in effect making the program proprietary. To prevent
62 this, we have made it clear that any patent must be licensed for everyone's
63 free use or not licensed at all.
64 .Pp
65 The precise terms and conditions for copying, distribution and modification
66 follow.
67 .Pp
68 .Bl -enum
69 .It
70 This License applies to any program or other work which contains a notice
71 placed by the copyright holder saying it may be distributed under the terms
72 of this General Public License. The \(lqProgram\(rq, below, refers to any such program
73 or work, and a \(lqwork based on the Program\(rq means either the Program or any derivative
74 work under copyright law: that is to say, a work containing the Program or
75 a portion of it, either verbatim or with modifications and/or translated into
76 another language. (Hereinafter, translation is included without limitation
77 in the term \(lqmodification\(rq.) Each licensee is addressed as \(lqyou\(rq.
78 .Pp
79 Activities other than copying, distribution and modification are not covered
80 by this License; they are outside its scope. The act of running the Program
81 is not restricted, and the output from the Program is covered only if its
82 contents constitute a work based on the Program (independent of having been
83 made by running the Program). Whether that is true depends on what the Program
84 does.
85 .Pp
86 .It
87 You may copy and distribute verbatim copies of the Program's source code as
88 you receive it, in any medium, provided that you conspicuously and appropriately
89 publish on each copy an appropriate copyright notice and disclaimer of warranty;
90 keep intact all the notices that refer to this License and to the absence
91 of any warranty; and give any other recipients of the Program a copy of this
92 License along with the Program.
93 .Pp
94 You may charge a fee for the physical act of transferring a copy, and you
95 may at your option offer warranty protection in exchange for a fee.
96 .Pp
97 .It
98 You may modify your copy or copies of the Program or any portion of it, thus
99 forming a work based on the Program, and copy and distribute such modifications
100 or work under the terms of Section 1 above, provided that you also meet all
101 of these conditions:
102 .Pp
103 .Bl -enum
104 .It
105 You must cause the modified files to carry prominent notices stating that
106 you changed the files and the date of any change.
107 .Pp
108 .It
109 You must cause any work that you distribute or publish, that in whole or in
110 part contains or is derived from the Program or any part thereof, to be licensed
111 as a whole at no charge to all third parties under the terms of this License.
112 .Pp
113 .It
114 If the modified program normally reads commands interactively when run, you
115 must cause it, when started running for such interactive use in the most ordinary
116 way, to print or display an announcement including an appropriate copyright
117 notice and a notice that there is no warranty (or else, saying that you provide
118 a warranty) and that users may redistribute the program under these conditions,
119 and telling the user how to view a copy of this License. (Exception: if the
120 Program itself is interactive but does not normally print such an announcement,
121 your work based on the Program is not required to print an announcement.)
122 .El
123 .Pp
124 These requirements apply to the modified work as a whole. If identifiable
125 sections of that work are not derived from the Program, and can be reasonably
126 considered independent and separate works in themselves, then this License,
127 and its terms, do not apply to those sections when you distribute them as
128 separate works. But when you distribute the same sections as part of a whole
129 which is a work based on the Program, the distribution of the whole must be
130 on the terms of this License, whose permissions for other licensees extend
131 to the entire whole, and thus to each and every part regardless of who wrote
132 it.
133 .Pp
134 Thus, it is not the intent of this section to claim rights or contest your
135 rights to work written entirely by you; rather, the intent is to exercise
136 the right to control the distribution of derivative or collective works based
137 on the Program.
138 .Pp
139 In addition, mere aggregation of another work not based on the Program with
140 the Program (or with a work based on the Program) on a volume of a storage
141 or distribution medium does not bring the other work under the scope of this
142 License.
143 .Pp
144 .It
145 You may copy and distribute the Program (or a work based on it, under Section
146 2) in object code or executable form under the terms of Sections 1 and 2 above
147 provided that you also do one of the following:
148 .Pp
149 .Bl -enum
150 .It
151 Accompany it with the complete corresponding machine-readable source code,
152 which must be distributed under the terms of Sections 1 and 2 above on a medium
153 customarily used for software interchange; or,
154 .Pp
155 .It
156 Accompany it with a written offer, valid for at least three years, to give
157 any third party, for a charge no more than your cost of physically performing
158 source distribution, a complete machine-readable copy of the corresponding
159 source code, to be distributed under the terms of Sections 1 and 2 above on
160 a medium customarily used for software interchange; or,
161 .Pp
162 .It
163 Accompany it with the information you received as to the offer to distribute
164 corresponding source code. (This alternative is allowed only for noncommercial
165 distribution and only if you received the program in object code or executable
166 form with such an offer, in accord with Subsection b above.)
167 .El
168 .Pp
169 The source code for a work means the preferred form of the work for making
170 modifications to it. For an executable work, complete source code means all
171 the source code for all modules it contains, plus any associated interface
172 definition files, plus the scripts used to control compilation and installation
173 of the executable. However, as a special exception, the source code distributed
174 need not include anything that is normally distributed (in either source or
175 binary form) with the major components (compiler, kernel, and so on) of the
176 operating system on which the executable runs, unless that component itself
177 accompanies the executable.
178 .Pp
179 If distribution of executable or object code is made by offering access to
180 copy from a designated place, then offering equivalent access to copy the
181 source code from the same place counts as distribution of the source code,
182 even though third parties are not compelled to copy the source along with
183 the object code.
184 .Pp
185 .It
186 You may not copy, modify, sublicense, or distribute the Program except as
187 expressly provided under this License. Any attempt otherwise to copy, modify,
188 sublicense or distribute the Program is void, and will automatically terminate
189 your rights under this License. However, parties who have received copies,
190 or rights, from you under this License will not have their licenses terminated
191 so long as such parties remain in full compliance.
192 .Pp
193 .It
194 You are not required to accept this License, since you have not signed it.
195 However, nothing else grants you permission to modify or distribute the Program
196 or its derivative works. These actions are prohibited by law if you do not
197 accept this License. Therefore, by modifying or distributing the Program (or
198 any work based on the Program), you indicate your acceptance of this License
199 to do so, and all its terms and conditions for copying, distributing or modifying
200 the Program or works based on it.
201 .Pp
202 .It
203 Each time you redistribute the Program (or any work based on the Program),
204 the recipient automatically receives a license from the original licensor
205 to copy, distribute or modify the Program subject to these terms and conditions.
206 You may not impose any further restrictions on the recipients' exercise of
207 the rights granted herein. You are not responsible for enforcing compliance
208 by third parties to this License.
209 .Pp
210 .It
211 If, as a consequence of a court judgment or allegation of patent infringement
212 or for any other reason (not limited to patent issues), conditions are imposed
213 on you (whether by court order, agreement or otherwise) that contradict the
214 conditions of this License, they do not excuse you from the conditions of
215 this License. If you cannot distribute so as to satisfy simultaneously your
216 obligations under this License and any other pertinent obligations, then as
217 a consequence you may not distribute the Program at all. For example, if a
218 patent license would not permit royalty-free redistribution of the Program
219 by all those who receive copies directly or indirectly through you, then the
220 only way you could satisfy both it and this License would be to refrain entirely
221 from distribution of the Program.
222 .Pp
223 If any portion of this section is held invalid or unenforceable under any
224 particular circumstance, the balance of the section is intended to apply and
225 the section as a whole is intended to apply in other circumstances.
226 .Pp
227 It is not the purpose of this section to induce you to infringe any patents
228 or other property right claims or to contest validity of any such claims;
229 this section has the sole purpose of protecting the integrity of the free
230 software distribution system, which is implemented by public license practices.
231 Many people have made generous contributions to the wide range of software
232 distributed through that system in reliance on consistent application of that
233 system; it is up to the author/donor to decide if he or she is willing to
234 distribute software through any other system and a licensee cannot impose
235 that choice.
236 .Pp
237 This section is intended to make thoroughly clear what is believed to be a
238 consequence of the rest of this License.
239 .Pp
240 .It
241 If the distribution and/or use of the Program is restricted in certain countries
242 either by patents or by copyrighted interfaces, the original copyright holder
243 who places the Program under this License may add an explicit geographical
244 distribution limitation excluding those countries, so that distribution is
245 permitted only in or among countries not thus excluded. In such case, this
246 License incorporates the limitation as if written in the body of this License.
247 .Pp
248 .It
249 The Free Software Foundation may publish revised and/or new versions of the
250 General Public License from time to time. Such new versions will be similar
251 in spirit to the present version, but may differ in detail to address new
252 problems or concerns.
253 .Pp
254 Each version is given a distinguishing version number. If the Program specifies
255 a version number of this License which applies to it and \(lqany later version\(rq,
256 you have the option of following the terms and conditions either of that version
257 or of any later version published by the Free Software Foundation. If the
258 Program does not specify a version number of this License, you may choose
259 any version ever published by the Free Software Foundation.
260 .Pp
261 .It
262 If you wish to incorporate parts of the Program into other free programs whose
263 distribution conditions are different, write to the author to ask for permission.
264 For software which is copyrighted by the Free Software Foundation, write to
265 the Free Software Foundation; we sometimes make exceptions for this. Our decision
266 will be guided by the two goals of preserving the free status of all derivatives
267 of our free software and of promoting the sharing and reuse of software generally.
268 .Pp
269 .It
270 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE
271 PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
272 STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM
273 \(lqAS IS\(rq WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
274 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
275 FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
276 OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
277 THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
278 .Pp
279 .It
280 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
281 ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
282 THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
283 GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE
284 OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA
285 OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES
286 OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
287 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
288 .El
289 .Pp
290 .Ss  How to Apply These Terms to Your New Programs
291 If you develop a new program, and you want it to be of the greatest possible
292 use to the public, the best way to achieve this is to make it free software
293 which everyone can redistribute and change under these terms.
294 .Pp
295 To do so, attach the following notices to the program. It is safest to attach
296 them to the start of each source file to most effectively convey the exclusion
297 of warranty; and each file should have at least the \(lqcopyright\(rq line and a pointer
298 to where the full notice is found.
299 .Pp
300 .Bd -literal -offset indent
301 one line to give the program's name and an idea of what it does.
302 Copyright (C) year  name of author
303
304 This program is free software; you can redistribute it and/or
305 modify it under the terms of the GNU General Public License
306 as published by the Free Software Foundation; either version 2
307 of the License, or (at your option) any later version.
308
309 This program is distributed in the hope that it will be useful,
310 but WITHOUT ANY WARRANTY; without even the implied warranty of
311 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
312 GNU General Public License for more details.
313
314 You should have received a copy of the GNU General Public License
315 along with this program; if not, write to the Free Software
316 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
317 .Ed
318 .Pp
319 Also add information on how to contact you by electronic and paper mail.
320 .Pp
321 If the program is interactive, make it output a short notice like this when
322 it starts in an interactive mode:
323 .Pp
324 .Bd -literal -offset indent
325 Gnomovision version 69, Copyright (C) year  name of author
326 Gnomovision comes with ABSOLUTELY NO WARRANTY; for details
327 type `show w'.  This is free software, and you are welcome
328 to redistribute it under certain conditions; type `show c' 
329 for details.
330 .Ed
331 .Pp
332 The hypothetical commands
333 .Li show w
334 and
335 .Li show c
336 should show the appropriate parts of the General Public License. Of course,
337 the commands you use may be called something other than
338 .Li show w
339 and
340 .Li show c
341 ; they could even be mouse-clicks or menu items---whatever suits your program.
342 .Pp
343 You should also get your employer (if you work as a programmer) or your school,
344 if any, to sign a \(lqcopyright disclaimer\(rq for the program, if necessary. Here
345 is a sample; alter the names:
346 .Pp
347 .Bd -literal -offset indent
348
349 Yoyodyne, Inc., hereby disclaims all copyright
350 interest in the program `Gnomovision'
351 (which makes passes at compilers) written 
352 by James Hacker.
353
354 signature of Ty Coon, 1 April 1989
355 Ty Coon, President of Vice
356
357 .Ed
358 .Pp
359 This General Public License does not permit incorporating your program into
360 proprietary programs. If your program is a subroutine library, you may consider
361 it more useful to permit linking proprietary applications with the library.
362 If this is what you want to do, use the GNU Library General Public License
363 instead of this License.
364 .Pp
365 .Sh  Contributors to GNU Li gperf Utility
366 .Bl -bullet
367 .It
368 The GNU
369 .Li gperf
370 perfect hash function generator utility was written in GNU C++ by Douglas
371 C. Schmidt. The general idea for the perfect hash function generator was inspired
372 by Keith Bostic's algorithm written in C, and distributed to net.sources around
373 1984. The current program is a heavily modified, enhanced, and extended implementation
374 of Keith's basic idea, created at the University of California, Irvine. Bugs,
375 patches, and suggestions should be reported to
376 .Li <bug-gnu-gperf@gnu.org> .
377 .Pp
378 .It
379 Special thanks is extended to Michael Tiemann and Doug Lea, for providing
380 a useful compiler, and for giving me a forum to exhibit my creation.
381 .Pp
382 In addition, Adam de Boor and Nels Olson provided many tips and insights that
383 greatly helped improve the quality and functionality of
384 .Li gperf .
385 .Pp
386 .It
387 Bruno Haible enhanced and optimized the search algorithm. He also rewrote
388 the input routines and the output routines for better reliability, and added
389 a testsuite.
390 .El
391 .Pp
392 .Sh  Introduction
393 .Li gperf
394 is a perfect hash function generator written in C++. It transforms an
395 .Va n
396 element user-specified keyword set
397 .Va W
398 into a perfect hash function
399 .Va F .
400 .Va F
401 uniquely maps keywords in
402 .Va W
403 onto the range 0..
404 .Va k ,
405 where
406 .Va k
407 >=
408 .Va n-1 .
409 If
410 .Va k
411 =
412 .Va n-1
413 then
414 .Va F
415 is a
416 .Em minimal
417 perfect hash function.
418 .Li gperf
419 generates a 0..
420 .Va k
421 element static lookup table and a pair of C functions. These functions determine
422 whether a given character string
423 .Va s
424 occurs in
425 .Va W ,
426 using at most one probe into the lookup table.
427 .Pp
428 .Li gperf
429 currently generates the reserved keyword recognizer for lexical analyzers
430 in several production and research compilers and language processing tools,
431 including GNU C, GNU C++, GNU Java, GNU Pascal, GNU Modula 3, and GNU indent.
432 Complete C++ source code for
433 .Li gperf
434 is available from
435 .Li http://ftp.gnu.org/pub/gnu/gperf/ .
436 A paper describing
437 .Li gperf
438 \&'s design and implementation in greater detail is available in the Second
439 USENIX C++ Conference proceedings or from
440 .Li http://www.cs.wustl.edu/~schmidt/resume.html .
441 .Pp
442 .Sh  Static search structures and GNU Li gperf
443 A
444 .Em static search structure
445 is an Abstract Data Type with certain fundamental operations, e.g.,
446 .Em initialize ,
447 .Em insert ,
448 and
449 .Em retrieve .
450 Conceptually, all insertions occur before any retrievals. In practice,
451 .Li gperf
452 generates a
453 .Em static
454 array containing search set keywords and any associated attributes specified
455 by the user. Thus, there is essentially no execution-time cost for the insertions.
456 It is a useful data structure for representing
457 .Em static search sets .
458 Static search sets occur frequently in software system applications. Typical
459 static search sets include compiler reserved words, assembler instruction
460 opcodes, and built-in shell interpreter commands. Search set members, called
461 .Em keywords ,
462 are inserted into the structure only once, usually during program initialization,
463 and are not generally modified at run-time.
464 .Pp
465 Numerous static search structure implementations exist, e.g., arrays, linked
466 lists, binary search trees, digital search tries, and hash tables. Different
467 approaches offer trade-offs between space utilization and search time efficiency.
468 For example, an
469 .Va n
470 element sorted array is space efficient, though the average-case time complexity
471 for retrieval operations using binary search is proportional to log
472 .Va n .
473 Conversely, hash table implementations often locate a table entry in constant
474 time, but typically impose additional memory overhead and exhibit poor worst
475 case performance.
476 .Pp
477 .Em Minimal perfect hash functions
478 provide an optimal solution for a particular class of static search sets.
479 A minimal perfect hash function is defined by two properties:
480 .Pp
481 .Bl -bullet
482 .It
483 It allows keyword recognition in a static search set using at most
484 .Em one
485 probe into the hash table. This represents the \(lqperfect\(rq property.
486 .It
487 The actual memory allocated to store the keywords is precisely large enough
488 for the keyword set, and
489 .Em no larger .
490 This is the \(lqminimal\(rq property.
491 .El
492 .Pp
493 For most applications it is far easier to generate
494 .Em perfect
495 hash functions than
496 .Em minimal perfect
497 hash functions. Moreover, non-minimal perfect hash functions frequently execute
498 faster than minimal ones in practice. This phenomena occurs since searching
499 a sparse keyword table increases the probability of locating a \(lqnull\(rq entry,
500 thereby reducing string comparisons.
501 .Li gperf
502 \&'s default behavior generates
503 .Em near-minimal
504 perfect hash functions for keyword sets. However,
505 .Li gperf
506 provides many options that permit user control over the degree of minimality
507 and perfection.
508 .Pp
509 Static search sets often exhibit relative stability over time. For example,
510 Ada's 63 reserved words have remained constant for nearly a decade. It is
511 therefore frequently worthwhile to expend concerted effort building an optimal
512 search structure
513 .Em once ,
514 if it subsequently receives heavy use multiple times.
515 .Li gperf
516 removes the drudgery associated with constructing time- and space-efficient
517 search structures by hand. It has proven a useful and practical tool for serious
518 programming projects. Output from
519 .Li gperf
520 is currently used in several production and research compilers, including
521 GNU C, GNU C++, GNU Java, GNU Pascal, and GNU Modula 3. The latter two compilers
522 are not yet part of the official GNU distribution. Each compiler utilizes
523 .Li gperf
524 to automatically generate static search structures that efficiently identify
525 their respective reserved keywords.
526 .Pp
527 .Sh  High-Level Description of GNU Li gperf
528 The perfect hash function generator
529 .Li gperf
530 reads a set of \(lqkeywords\(rq from an input file (or from the standard input by
531 default). It attempts to derive a perfect hashing function that recognizes
532 a member of the
533 .Em static keyword set
534 with at most a single probe into the lookup table. If
535 .Li gperf
536 succeeds in generating such a function it produces a pair of C source code
537 routines that perform hashing and table lookup recognition. All generated
538 C code is directed to the standard output. Command-line options described
539 below allow you to modify the input and output format to
540 .Li gperf .
541 .Pp
542 By default,
543 .Li gperf
544 attempts to produce time-efficient code, with less emphasis on efficient space
545 utilization. However, several options exist that permit trading-off execution
546 time for storage space and vice versa. In particular, expanding the generated
547 table size produces a sparse search structure, generally yielding faster searches.
548 Conversely, you can direct
549 .Li gperf
550 to utilize a C
551 .Li switch
552 statement scheme that minimizes data space storage size. Furthermore, using
553 a C
554 .Li switch
555 may actually speed up the keyword retrieval time somewhat. Actual results
556 depend on your C compiler, of course.
557 .Pp
558 In general,
559 .Li gperf
560 assigns values to the bytes it is using for hashing until some set of values
561 gives each keyword a unique value. A helpful heuristic is that the larger
562 the hash value range, the easier it is for
563 .Li gperf
564 to find and generate a perfect hash function. Experimentation is the key to
565 getting the most from
566 .Li gperf .
567 .Pp
568 .Ss  Input Format to Li gperf
569 You can control the input file format by varying certain command-line arguments,
570 in particular the
571 .Li -t
572 option. The input's appearance is similar to GNU utilities
573 .Li flex
574 and
575 .Li bison
576 (or UNIX utilities
577 .Li lex
578 and
579 .Li yacc ) .
580 Here's an outline of the general format:
581 .Pp
582 .Bd -literal -offset indent
583
584 declarations
585 %%
586 keywords
587 %%
588 functions
589
590 .Ed
591 .Pp
592 .Em Unlike
593 .Li flex
594 or
595 .Li bison ,
596 the declarations section and the functions section are optional. The following
597 sections describe the input format for each section.
598 .Pp
599 It is possible to omit the declaration section entirely, if the
600 .Li -t
601 option is not given. In this case the input file begins directly with the
602 first keyword line, e.g.:
603 .Pp
604 .Bd -literal -offset indent
605
606 january
607 february
608 march
609 april
610 \&...
611
612 .Ed
613 .Pp
614 .Em  Declarations
615 .Pp
616 The keyword input file optionally contains a section for including arbitrary
617 C declarations and definitions,
618 .Li gperf
619 declarations that act like command-line options, as well as for providing
620 a user-supplied
621 .Li struct .
622 .Pp
623 .No  User-supplied Li struct
624 .Pp
625 If the
626 .Li -t
627 option (or, equivalently, the
628 .Li %struct-type
629 declaration)
630 .Em is
631 enabled, you
632 .Em must
633 provide a C
634 .Li struct
635 as the last component in the declaration section from the input file. The
636 first field in this struct must be of type
637 .Li char *
638 or
639 .Li const char *
640 if the
641 .Li -P
642 option is not given, or of type
643 .Li int
644 if the option
645 .Li -P
646 (or, equivalently, the
647 .Li %pic
648 declaration) is enabled. This first field must be called
649 .Li name ,
650 although it is possible to modify its name with the
651 .Li -K
652 option (or, equivalently, the
653 .Li %define slot-name
654 declaration) described below.
655 .Pp
656 Here is a simple example, using months of the year and their attributes as
657 input:
658 .Pp
659 .Bd -literal -offset indent
660
661 struct month { char *name; int number; int days; int leap_days; };
662 %%
663 january,   1, 31, 31
664 february,  2, 28, 29
665 march,     3, 31, 31
666 april,     4, 30, 30
667 may,       5, 31, 31
668 june,      6, 30, 30
669 july,      7, 31, 31
670 august,    8, 31, 31
671 september, 9, 30, 30
672 october,  10, 31, 31
673 november, 11, 30, 30
674 december, 12, 31, 31
675
676 .Ed
677 .Pp
678 Separating the
679 .Li struct
680 declaration from the list of keywords and other fields are a pair of consecutive
681 percent signs,
682 .Li %% ,
683 appearing left justified in the first column, as in the UNIX utility
684 .Li lex .
685 .Pp
686 If the
687 .Li struct
688 has already been declared in an include file, it can be mentioned in an abbreviated
689 form, like this:
690 .Pp
691 .Bd -literal -offset indent
692
693 struct month;
694 %%
695 january,   1, 31, 31
696 \&...
697
698 .Ed
699 .Pp
700 .No  Gperf Declarations
701 .Pp
702 The declaration section can contain
703 .Li gperf
704 declarations. They influence the way
705 .Li gperf
706 works, like command line options do. In fact, every such declaration is equivalent
707 to a command line option. There are three forms of declarations:
708 .Pp
709 .Bl -enum
710 .It
711 Declarations without argument, like
712 .Li %compare-lengths .
713 .Pp
714 .It
715 Declarations with an argument, like
716 .Li %switch= Va count .
717 .Pp
718 .It
719 Declarations of names of entities in the output file, like
720 .Li %define lookup-function-name Va name .
721 .El
722 .Pp
723 When a declaration is given both in the input file and as a command line option,
724 the command-line option's value prevails.
725 .Pp
726 The following
727 .Li gperf
728 declarations are available.
729 .Pp
730 .Bl -tag -width Ds
731 .It  %delimiters= Va delimiter-list
732 Allows you to provide a string containing delimiters used to separate keywords
733 from their attributes. The default is ",". This option is essential if you
734 want to use keywords that have embedded commas or newlines.
735 .Pp
736 .It  %struct-type
737 Allows you to include a
738 .Li struct
739 type declaration for generated code; see above for an example.
740 .Pp
741 .It  %ignore-case
742 Consider upper and lower case ASCII characters as equivalent. The string comparison
743 will use a case insignificant character comparison. Note that locale dependent
744 case mappings are ignored.
745 .Pp
746 .It  %language= Va language-name
747 Instructs
748 .Li gperf
749 to generate code in the language specified by the option's argument. Languages
750 handled are currently:
751 .Pp
752 .Bl -tag -width Ds
753 .It  KR-C
754 Old-style K&R C. This language is understood by old-style C compilers and
755 ANSI C compilers, but ANSI C compilers may flag warnings (or even errors)
756 because of lacking
757 .Li const .
758 .Pp
759 .It  C
760 Common C. This language is understood by ANSI C compilers, and also by old-style
761 C compilers, provided that you
762 .Li #define const
763 to empty for compilers which don't know about this keyword.
764 .Pp
765 .It  ANSI-C
766 ANSI C. This language is understood by ANSI C compilers and C++ compilers.
767 .Pp
768 .It  C++
769 C++. This language is understood by C++ compilers.
770 .El
771 .Pp
772 The default is C.
773 .Pp
774 .It  %define slot-name Va name
775 This declaration is only useful when option
776 .Li -t
777 (or, equivalently, the
778 .Li %struct-type
779 declaration) has been given. By default, the program assumes the structure
780 component identifier for the keyword is
781 .Li name .
782 This option allows an arbitrary choice of identifier for this component, although
783 it still must occur as the first field in your supplied
784 .Li struct .
785 .Pp
786 .It  %define initializer-suffix Va initializers
787 This declaration is only useful when option
788 .Li -t
789 (or, equivalently, the
790 .Li %struct-type
791 declaration) has been given. It permits to specify initializers for the structure
792 members following
793 .Va slot-name
794 in empty hash table entries. The list of initializers should start with a
795 comma. By default, the emitted code will zero-initialize structure members
796 following
797 .Va slot-name .
798 .Pp
799 .It  %define hash-function-name Va name
800 Allows you to specify the name for the generated hash function. Default name
801 is
802 .Li hash .
803 This option permits the use of two hash tables in the same file.
804 .Pp
805 .It  %define lookup-function-name Va name
806 Allows you to specify the name for the generated lookup function. Default
807 name is
808 .Li in_word_set .
809 This option permits multiple generated hash functions to be used in the same
810 application.
811 .Pp
812 .It  %define class-name Va name
813 This option is only useful when option
814 .Li -L C++
815 (or, equivalently, the
816 .Li %language=C++
817 declaration) has been given. It allows you to specify the name of generated
818 C++ class. Default name is
819 .Li Perfect_Hash .
820 .Pp
821 .It  %7bit
822 This option specifies that all strings that will be passed as arguments to
823 the generated hash function and the generated lookup function will solely
824 consist of 7-bit ASCII characters (bytes in the range 0..127). (Note that
825 the ANSI C functions
826 .Li isalnum
827 and
828 .Li isgraph
829 do
830 .Em not
831 guarantee that a byte is in this range. Only an explicit test like
832 .Li c >= 'A' && c <= 'Z'
833 guarantees this.)
834 .Pp
835 .It  %compare-lengths
836 Compare keyword lengths before trying a string comparison. This option is
837 mandatory for binary comparisons (see Section
838 .Dq Binary Strings ) .
839 It also might cut down on the number of string comparisons made during the
840 lookup, since keywords with different lengths are never compared via
841 .Li strcmp .
842 However, using
843 .Li %compare-lengths
844 might greatly increase the size of the generated C code if the lookup table
845 range is large (which implies that the switch option
846 .Li -S
847 or
848 .Li %switch
849 is not enabled), since the length table contains as many elements as there
850 are entries in the lookup table.
851 .Pp
852 .It  %compare-strncmp
853 Generates C code that uses the
854 .Li strncmp
855 function to perform string comparisons. The default action is to use
856 .Li strcmp .
857 .Pp
858 .It  %readonly-tables
859 Makes the contents of all generated lookup tables constant, i.e., \(lqreadonly\(rq.
860 Many compilers can generate more efficient code for this by putting the tables
861 in readonly memory.
862 .Pp
863 .It  %enum
864 Define constant values using an enum local to the lookup function rather than
865 with #defines. This also means that different lookup functions can reside
866 in the same file. Thanks to James Clark
867 .Li <jjc@ai.mit.edu> .
868 .Pp
869 .It  %includes
870 Include the necessary system include file,
871 .Li <string.h> ,
872 at the beginning of the code. By default, this is not done; the user must
873 include this header file himself to allow compilation of the code.
874 .Pp
875 .It  %global-table
876 Generate the static table of keywords as a static global variable, rather
877 than hiding it inside of the lookup function (which is the default behavior).
878 .Pp
879 .It  %pic
880 Optimize the generated table for inclusion in shared libraries. This reduces
881 the startup time of programs using a shared library containing the generated
882 code. If the
883 .Li %struct-type
884 declaration (or, equivalently, the option
885 .Li -t )
886 is also given, the first field of the user-defined struct must be of type
887 .Li int ,
888 not
889 .Li char * ,
890 because it will contain offsets into the string pool instead of actual strings.
891 To convert such an offset to a string, you can use the expression
892 .Li stringpool + Va o ,
893 where
894 .Va o
895 is the offset. The string pool name can be changed through the
896 .Li %define string-pool-name
897 declaration.
898 .Pp
899 .It  %define string-pool-name Va name
900 Allows you to specify the name of the generated string pool created by the
901 declaration
902 .Li %pic
903 (or, equivalently, the option
904 .Li -P ) .
905 The default name is
906 .Li stringpool .
907 This declaration permits the use of two hash tables in the same file, with
908 .Li %pic
909 and even when the
910 .Li %global-table
911 declaration (or, equivalently, the option
912 .Li -G )
913 is given.
914 .Pp
915 .It  %null-strings
916 Use NULL strings instead of empty strings for empty keyword table entries.
917 This reduces the startup time of programs using a shared library containing
918 the generated code (but not as much as the declaration
919 .Li %pic ) ,
920 at the expense of one more test-and-branch instruction at run time.
921 .Pp
922 .It  %define word-array-name Va name
923 Allows you to specify the name for the generated array containing the hash
924 table. Default name is
925 .Li wordlist .
926 This option permits the use of two hash tables in the same file, even when
927 the option
928 .Li -G
929 (or, equivalently, the
930 .Li %global-table
931 declaration) is given.
932 .Pp
933 .It  %define length-table-name Va name
934 Allows you to specify the name for the generated array containing the length
935 table. Default name is
936 .Li lengthtable .
937 This option permits the use of two length tables in the same file, even when
938 the option
939 .Li -G
940 (or, equivalently, the
941 .Li %global-table
942 declaration) is given.
943 .Pp
944 .It  %switch= Va count
945 Causes the generated C code to use a
946 .Li switch
947 statement scheme, rather than an array lookup table. This can lead to a reduction
948 in both time and space requirements for some input files. The argument to
949 this option determines how many
950 .Li switch
951 statements are generated. A value of 1 generates 1
952 .Li switch
953 containing all the elements, a value of 2 generates 2 tables with 1/2 the
954 elements in each
955 .Li switch ,
956 etc. This is useful since many C compilers cannot correctly generate code
957 for large
958 .Li switch
959 statements. This option was inspired in part by Keith Bostic's original C
960 program.
961 .Pp
962 .It  %omit-struct-type
963 Prevents the transfer of the type declaration to the output file. Use this
964 option if the type is already defined elsewhere.
965 .El
966 .Pp
967 .No  C Code Inclusion
968 .Pp
969 Using a syntax similar to GNU utilities
970 .Li flex
971 and
972 .Li bison ,
973 it is possible to directly include C source text and comments verbatim into
974 the generated output file. This is accomplished by enclosing the region inside
975 left-justified surrounding
976 .Li %{ ,
977 .Li %}
978 pairs. Here is an input fragment based on the previous example that illustrates
979 this feature:
980 .Pp
981 .Bd -literal -offset indent
982
983 %{
984 #include <assert.h>
985 /* This section of code is inserted directly into the output. */
986 int return_month_days (struct month *months, int is_leap_year);
987 %}
988 struct month { char *name; int number; int days; int leap_days; };
989 %%
990 january,   1, 31, 31
991 february,  2, 28, 29
992 march,     3, 31, 31
993 \&...
994
995 .Ed
996 .Pp
997 .Em  Format for Keyword Entries
998 .Pp
999 The second input file format section contains lines of keywords and any associated
1000 attributes you might supply. A line beginning with
1001 .Li #
1002 in the first column is considered a comment. Everything following the
1003 .Li #
1004 is ignored, up to and including the following newline. A line beginning with
1005 .Li %
1006 in the first column is an option declaration and must not occur within the
1007 keywords section.
1008 .Pp
1009 The first field of each non-comment line is always the keyword itself. It
1010 can be given in two ways: as a simple name, i.e., without surrounding string
1011 quotation marks, or as a string enclosed in double-quotes, in C syntax, possibly
1012 with backslash escapes like
1013 .Li \e"
1014 or
1015 .Li \e234
1016 or
1017 .Li \exa8 .
1018 In either case, it must start right at the beginning of the line, without
1019 leading whitespace. In this context, a \(lqfield\(rq is considered to extend up to,
1020 but not include, the first blank, comma, or newline. Here is a simple example
1021 taken from a partial list of C reserved words:
1022 .Pp
1023 .Bd -literal -offset indent
1024
1025 # These are a few C reserved words, see the c.gperf file 
1026 # for a complete list of ANSI C reserved words.
1027 unsigned
1028 sizeof
1029 switch
1030 signed
1031 if
1032 default
1033 for
1034 while
1035 return
1036
1037 .Ed
1038 .Pp
1039 Note that unlike
1040 .Li flex
1041 or
1042 .Li bison
1043 the first
1044 .Li %%
1045 marker may be elided if the declaration section is empty.
1046 .Pp
1047 Additional fields may optionally follow the leading keyword. Fields should
1048 be separated by commas, and terminate at the end of line. What these fields
1049 mean is entirely up to you; they are used to initialize the elements of the
1050 user-defined
1051 .Li struct
1052 provided by you in the declaration section. If the
1053 .Li -t
1054 option (or, equivalently, the
1055 .Li %struct-type
1056 declaration) is
1057 .Em not
1058 enabled these fields are simply ignored. All previous examples except the
1059 last one contain keyword attributes.
1060 .Pp
1061 .Em  Including Additional C Functions
1062 .Pp
1063 The optional third section also corresponds closely with conventions found
1064 in
1065 .Li flex
1066 and
1067 .Li bison .
1068 All text in this section, starting at the final
1069 .Li %%
1070 and extending to the end of the input file, is included verbatim into the
1071 generated output file. Naturally, it is your responsibility to ensure that
1072 the code contained in this section is valid C.
1073 .Pp
1074 .Em  Where to place directives for GNU Li indent.
1075 .Pp
1076 If you want to invoke GNU
1077 .Li indent
1078 on a
1079 .Li gperf
1080 input file, you will see that GNU
1081 .Li indent
1082 doesn't understand the
1083 .Li %% ,
1084 .Li %{
1085 and
1086 .Li %}
1087 directives that control
1088 .Li gperf
1089 \&'s interpretation of the input file. Therefore you have to insert some directives
1090 for GNU
1091 .Li indent .
1092 More precisely, assuming the most general input file structure
1093 .Pp
1094 .Bd -literal -offset indent
1095
1096 declarations part 1
1097 %{
1098 verbatim code
1099 %}
1100 declarations part 2
1101 %%
1102 keywords
1103 %%
1104 functions
1105
1106 .Ed
1107 .Pp
1108 you would insert
1109 .Li *INDENT-OFF*
1110 and
1111 .Li *INDENT-ON*
1112 comments as follows:
1113 .Pp
1114 .Bd -literal -offset indent
1115
1116 /* *INDENT-OFF* */
1117 declarations part 1
1118 %{
1119 /* *INDENT-ON* */
1120 verbatim code
1121 /* *INDENT-OFF* */
1122 %}
1123 declarations part 2
1124 %%
1125 keywords
1126 %%
1127 /* *INDENT-ON* */
1128 functions
1129
1130 .Ed
1131 .Pp
1132 .Ss  Output Format for Generated C Code with Li gperf
1133 Several options control how the generated C code appears on the standard output.
1134 Two C functions are generated. They are called
1135 .Li hash
1136 and
1137 .Li in_word_set ,
1138 although you may modify their names with a command-line option. Both functions
1139 require two arguments, a string,
1140 .Li char *
1141 .Va str ,
1142 and a length parameter,
1143 .Li int
1144 .Va len .
1145 Their default function prototypes are as follows:
1146 .Pp
1147 Function:
1148 .Ft  unsigned int
1149 .Fo  hash
1150 .Fa  (const char * Va str, unsigned int Va len)
1151 .Fc
1152 .Pp
1153 By default, the generated
1154 .Li hash
1155 function returns an integer value created by adding
1156 .Va len
1157 to several user-specified
1158 .Va str
1159 byte positions indexed into an
1160 .Em associated values
1161 table stored in a local static array. The associated values table is constructed
1162 internally by
1163 .Li gperf
1164 and later output as a static local C array called
1165 .Li hash_table .
1166 The relevant selected positions (i.e. indices into
1167 .Va str )
1168 are specified via the
1169 .Li -k
1170 option when running
1171 .Li gperf ,
1172 as detailed in the
1173 .Em Options
1174 section below (see Section
1175 .Dq Options ) .
1176 .Pp
1177 Function:
1178 .Ft 
1179 .Fo  in_word_set
1180 .Fa  (const char * Va str, unsigned int Va len)
1181 .Fc
1182 .Pp
1183 If
1184 .Va str
1185 is in the keyword set, returns a pointer to that keyword. More exactly, if
1186 the option
1187 .Li -t
1188 (or, equivalently, the
1189 .Li %struct-type
1190 declaration) was given, it returns a pointer to the matching keyword's structure.
1191 Otherwise it returns
1192 .Li NULL .
1193 .Pp
1194 If the option
1195 .Li -c
1196 (or, equivalently, the
1197 .Li %compare-strncmp
1198 declaration) is not used,
1199 .Va str
1200 must be a NUL terminated string of exactly length
1201 .Va len .
1202 If
1203 .Li -c
1204 (or, equivalently, the
1205 .Li %compare-strncmp
1206 declaration) is used,
1207 .Va str
1208 must simply be an array of
1209 .Va len
1210 bytes and does not need to be NUL terminated.
1211 .Pp
1212 The code generated for these two functions is affected by the following options:
1213 .Pp
1214 .Bl -tag -width Ds
1215 .It  -t
1216 .It  --struct-type
1217 Make use of the user-defined
1218 .Li struct .
1219 .Pp
1220 .It  -S Va total-switch-statements
1221 .It  --switch= Va total-switch-statements
1222 Generate 1 or more C
1223 .Li switch
1224 statement rather than use a large, (and potentially sparse) static array.
1225 Although the exact time and space savings of this approach vary according
1226 to your C compiler's degree of optimization, this method often results in
1227 smaller and faster code.
1228 .El
1229 .Pp
1230 If the
1231 .Li -t
1232 and
1233 .Li -S
1234 options (or, equivalently, the
1235 .Li %struct-type
1236 and
1237 .Li %switch
1238 declarations) are omitted, the default action is to generate a
1239 .Li char *
1240 array containing the keywords, together with additional empty strings used
1241 for padding the array. By experimenting with the various input and output
1242 options, and timing the resulting C code, you can determine the best option
1243 choices for different keyword set characteristics.
1244 .Pp
1245 .Ss  Use of NUL bytes
1246 By default, the code generated by
1247 .Li gperf
1248 operates on zero terminated strings, the usual representation of strings in
1249 C. This means that the keywords in the input file must not contain NUL bytes,
1250 and the
1251 .Va str
1252 argument passed to
1253 .Li hash
1254 or
1255 .Li in_word_set
1256 must be NUL terminated and have exactly length
1257 .Va len .
1258 .Pp
1259 If option
1260 .Li -c
1261 (or, equivalently, the
1262 .Li %compare-strncmp
1263 declaration) is used, then the
1264 .Va str
1265 argument does not need to be NUL terminated. The code generated by
1266 .Li gperf
1267 will only access the first
1268 .Va len ,
1269 not
1270 .Va len+1 ,
1271 bytes starting at
1272 .Va str .
1273 However, the keywords in the input file still must not contain NUL bytes.
1274 .Pp
1275 If option
1276 .Li -l
1277 (or, equivalently, the
1278 .Li %compare-lengths
1279 declaration) is used, then the hash table performs binary comparison. The
1280 keywords in the input file may contain NUL bytes, written in string syntax
1281 as
1282 .Li \e000
1283 or
1284 .Li \ex00 ,
1285 and the code generated by
1286 .Li gperf
1287 will treat NUL like any other byte. Also, in this case the
1288 .Li -c
1289 option (or, equivalently, the
1290 .Li %compare-strncmp
1291 declaration) is ignored.
1292 .Pp
1293 .Sh  Invoking Li gperf
1294 There are
1295 .Em many
1296 options to
1297 .Li gperf .
1298 They were added to make the program more convenient for use with real applications.
1299 \(lqOn-line\(rq help is readily available via the
1300 .Li --help
1301 option. Here is the complete list of options.
1302 .Pp
1303 .Ss  Specifying the Location of the Output File
1304 .Bl -tag -width Ds
1305 .It  --output-file= Va file
1306 Allows you to specify the name of the file to which the output is written
1307 to.
1308 .El
1309 .Pp
1310 The results are written to standard output if no output file is specified
1311 or if it is
1312 .Li - .
1313 .Pp
1314 .Ss  Options that affect Interpretation of the Input File
1315 These options are also available as declarations in the input file (see Section
1316 .Dq Gperf Declarations ) .
1317 .Pp
1318 .Bl -tag -width Ds
1319 .It  -e Va keyword-delimiter-list
1320 .It  --delimiters= Va keyword-delimiter-list
1321 Allows you to provide a string containing delimiters used to separate keywords
1322 from their attributes. The default is ",". This option is essential if you
1323 want to use keywords that have embedded commas or newlines. One useful trick
1324 is to use -e'TAB', where TAB is the literal tab character.
1325 .Pp
1326 .It  -t
1327 .It  --struct-type
1328 Allows you to include a
1329 .Li struct
1330 type declaration for generated code. Any text before a pair of consecutive
1331 .Li %%
1332 is considered part of the type declaration. Keywords and additional fields
1333 may follow this, one group of fields per line. A set of examples for generating
1334 perfect hash tables and functions for Ada, C, C++, Pascal, Modula 2, Modula
1335 3 and JavaScript reserved words are distributed with this release.
1336 .Pp
1337 .It  --ignore-case
1338 Consider upper and lower case ASCII characters as equivalent. The string comparison
1339 will use a case insignificant character comparison. Note that locale dependent
1340 case mappings are ignored. This option is therefore not suitable if a properly
1341 internationalized or locale aware case mapping should be used. (For example,
1342 in a Turkish locale, the upper case equivalent of the lowercase ASCII letter
1343 .Li i
1344 is the non-ASCII character
1345 .Li capital i with dot above . )
1346 For this case, it is better to apply an uppercase or lowercase conversion
1347 on the string before passing it to the
1348 .Li gperf
1349 generated function.
1350 .El
1351 .Pp
1352 .Ss  Options to specify the Language for the Output Code
1353 These options are also available as declarations in the input file (see Section
1354 .Dq Gperf Declarations ) .
1355 .Pp
1356 .Bl -tag -width Ds
1357 .It  -L Va generated-language-name
1358 .It  --language= Va generated-language-name
1359 Instructs
1360 .Li gperf
1361 to generate code in the language specified by the option's argument. Languages
1362 handled are currently:
1363 .Pp
1364 .Bl -tag -width Ds
1365 .It  KR-C
1366 Old-style K&R C. This language is understood by old-style C compilers and
1367 ANSI C compilers, but ANSI C compilers may flag warnings (or even errors)
1368 because of lacking
1369 .Li const .
1370 .Pp
1371 .It  C
1372 Common C. This language is understood by ANSI C compilers, and also by old-style
1373 C compilers, provided that you
1374 .Li #define const
1375 to empty for compilers which don't know about this keyword.
1376 .Pp
1377 .It  ANSI-C
1378 ANSI C. This language is understood by ANSI C compilers and C++ compilers.
1379 .Pp
1380 .It  C++
1381 C++. This language is understood by C++ compilers.
1382 .El
1383 .Pp
1384 The default is C.
1385 .Pp
1386 .It  -a
1387 This option is supported for compatibility with previous releases of
1388 .Li gperf .
1389 It does not do anything.
1390 .Pp
1391 .It  -g
1392 This option is supported for compatibility with previous releases of
1393 .Li gperf .
1394 It does not do anything.
1395 .El
1396 .Pp
1397 .Ss  Options for fine tuning Details in the Output Code
1398 Most of these options are also available as declarations in the input file
1399 (see Section
1400 .Dq Gperf Declarations ) .
1401 .Pp
1402 .Bl -tag -width Ds
1403 .It  -K Va slot-name
1404 .It  --slot-name= Va slot-name
1405 This option is only useful when option
1406 .Li -t
1407 (or, equivalently, the
1408 .Li %struct-type
1409 declaration) has been given. By default, the program assumes the structure
1410 component identifier for the keyword is
1411 .Li name .
1412 This option allows an arbitrary choice of identifier for this component, although
1413 it still must occur as the first field in your supplied
1414 .Li struct .
1415 .Pp
1416 .It  -F Va initializers
1417 .It  --initializer-suffix= Va initializers
1418 This option is only useful when option
1419 .Li -t
1420 (or, equivalently, the
1421 .Li %struct-type
1422 declaration) has been given. It permits to specify initializers for the structure
1423 members following
1424 .Va slot-name
1425 in empty hash table entries. The list of initializers should start with a
1426 comma. By default, the emitted code will zero-initialize structure members
1427 following
1428 .Va slot-name .
1429 .Pp
1430 .It  -H Va hash-function-name
1431 .It  --hash-function-name= Va hash-function-name
1432 Allows you to specify the name for the generated hash function. Default name
1433 is
1434 .Li hash .
1435 This option permits the use of two hash tables in the same file.
1436 .Pp
1437 .It  -N Va lookup-function-name
1438 .It  --lookup-function-name= Va lookup-function-name
1439 Allows you to specify the name for the generated lookup function. Default
1440 name is
1441 .Li in_word_set .
1442 This option permits multiple generated hash functions to be used in the same
1443 application.
1444 .Pp
1445 .It  -Z Va class-name
1446 .It  --class-name= Va class-name
1447 This option is only useful when option
1448 .Li -L C++
1449 (or, equivalently, the
1450 .Li %language=C++
1451 declaration) has been given. It allows you to specify the name of generated
1452 C++ class. Default name is
1453 .Li Perfect_Hash .
1454 .Pp
1455 .It  -7
1456 .It  --seven-bit
1457 This option specifies that all strings that will be passed as arguments to
1458 the generated hash function and the generated lookup function will solely
1459 consist of 7-bit ASCII characters (bytes in the range 0..127). (Note that
1460 the ANSI C functions
1461 .Li isalnum
1462 and
1463 .Li isgraph
1464 do
1465 .Em not
1466 guarantee that a byte is in this range. Only an explicit test like
1467 .Li c >= 'A' && c <= 'Z'
1468 guarantees this.) This was the default in versions of
1469 .Li gperf
1470 earlier than 2.7; now the default is to support 8-bit and multibyte characters.
1471 .Pp
1472 .It  -l
1473 .It  --compare-lengths
1474 Compare keyword lengths before trying a string comparison. This option is
1475 mandatory for binary comparisons (see Section
1476 .Dq Binary Strings ) .
1477 It also might cut down on the number of string comparisons made during the
1478 lookup, since keywords with different lengths are never compared via
1479 .Li strcmp .
1480 However, using
1481 .Li -l
1482 might greatly increase the size of the generated C code if the lookup table
1483 range is large (which implies that the switch option
1484 .Li -S
1485 or
1486 .Li %switch
1487 is not enabled), since the length table contains as many elements as there
1488 are entries in the lookup table.
1489 .Pp
1490 .It  -c
1491 .It  --compare-strncmp
1492 Generates C code that uses the
1493 .Li strncmp
1494 function to perform string comparisons. The default action is to use
1495 .Li strcmp .
1496 .Pp
1497 .It  -C
1498 .It  --readonly-tables
1499 Makes the contents of all generated lookup tables constant, i.e., \(lqreadonly\(rq.
1500 Many compilers can generate more efficient code for this by putting the tables
1501 in readonly memory.
1502 .Pp
1503 .It  -E
1504 .It  --enum
1505 Define constant values using an enum local to the lookup function rather than
1506 with #defines. This also means that different lookup functions can reside
1507 in the same file. Thanks to James Clark
1508 .Li <jjc@ai.mit.edu> .
1509 .Pp
1510 .It  -I
1511 .It  --includes
1512 Include the necessary system include file,
1513 .Li <string.h> ,
1514 at the beginning of the code. By default, this is not done; the user must
1515 include this header file himself to allow compilation of the code.
1516 .Pp
1517 .It  -G
1518 .It  --global-table
1519 Generate the static table of keywords as a static global variable, rather
1520 than hiding it inside of the lookup function (which is the default behavior).
1521 .Pp
1522 .It  -P
1523 .It  --pic
1524 Optimize the generated table for inclusion in shared libraries. This reduces
1525 the startup time of programs using a shared library containing the generated
1526 code. If the option
1527 .Li -t
1528 (or, equivalently, the
1529 .Li %struct-type
1530 declaration) is also given, the first field of the user-defined struct must
1531 be of type
1532 .Li int ,
1533 not
1534 .Li char * ,
1535 because it will contain offsets into the string pool instead of actual strings.
1536 To convert such an offset to a string, you can use the expression
1537 .Li stringpool + Va o ,
1538 where
1539 .Va o
1540 is the offset. The string pool name can be changed through the option
1541 .Li --string-pool-name .
1542 .Pp
1543 .It  -Q Va string-pool-name
1544 .It  --string-pool-name= Va string-pool-name
1545 Allows you to specify the name of the generated string pool created by option
1546 .Li -P .
1547 The default name is
1548 .Li stringpool .
1549 This option permits the use of two hash tables in the same file, with
1550 .Li -P
1551 and even when the option
1552 .Li -G
1553 (or, equivalently, the
1554 .Li %global-table
1555 declaration) is given.
1556 .Pp
1557 .It  --null-strings
1558 Use NULL strings instead of empty strings for empty keyword table entries.
1559 This reduces the startup time of programs using a shared library containing
1560 the generated code (but not as much as option
1561 .Li -P ) ,
1562 at the expense of one more test-and-branch instruction at run time.
1563 .Pp
1564 .It  -W Va hash-table-array-name
1565 .It  --word-array-name= Va hash-table-array-name
1566 Allows you to specify the name for the generated array containing the hash
1567 table. Default name is
1568 .Li wordlist .
1569 This option permits the use of two hash tables in the same file, even when
1570 the option
1571 .Li -G
1572 (or, equivalently, the
1573 .Li %global-table
1574 declaration) is given.
1575 .Pp
1576 .It  --length-table-name= Va length-table-array-name
1577 Allows you to specify the name for the generated array containing the length
1578 table. Default name is
1579 .Li lengthtable .
1580 This option permits the use of two length tables in the same file, even when
1581 the option
1582 .Li -G
1583 (or, equivalently, the
1584 .Li %global-table
1585 declaration) is given.
1586 .Pp
1587 .It  -S Va total-switch-statements
1588 .It  --switch= Va total-switch-statements
1589 Causes the generated C code to use a
1590 .Li switch
1591 statement scheme, rather than an array lookup table. This can lead to a reduction
1592 in both time and space requirements for some input files. The argument to
1593 this option determines how many
1594 .Li switch
1595 statements are generated. A value of 1 generates 1
1596 .Li switch
1597 containing all the elements, a value of 2 generates 2 tables with 1/2 the
1598 elements in each
1599 .Li switch ,
1600 etc. This is useful since many C compilers cannot correctly generate code
1601 for large
1602 .Li switch
1603 statements. This option was inspired in part by Keith Bostic's original C
1604 program.
1605 .Pp
1606 .It  -T
1607 .It  --omit-struct-type
1608 Prevents the transfer of the type declaration to the output file. Use this
1609 option if the type is already defined elsewhere.
1610 .Pp
1611 .It  -p
1612 This option is supported for compatibility with previous releases of
1613 .Li gperf .
1614 It does not do anything.
1615 .El
1616 .Pp
1617 .Ss  Options for changing the Algorithms employed by Li gperf
1618 .Bl -tag -width Ds
1619 .It  -k Va selected-byte-positions
1620 .It  --key-positions= Va selected-byte-positions
1621 Allows selection of the byte positions used in the keywords' hash function.
1622 The allowable choices range between 1-255, inclusive. The positions are separated
1623 by commas, e.g.,
1624 .Li -k 9,4,13,14
1625 ; ranges may be used, e.g.,
1626 .Li -k 2-7
1627 ; and positions may occur in any order. Furthermore, the wildcard '*' causes
1628 the generated hash function to consider
1629 .Sy all
1630 byte positions in each keyword, whereas '$' instructs the hash function to
1631 use the \(lqfinal byte\(rq of a keyword (this is the only way to use a byte position
1632 greater than 255, incidentally).
1633 .Pp
1634 For instance, the option
1635 .Li -k 1,2,4,6-10,'$'
1636 generates a hash function that considers positions 1,2,4,6,7,8,9,10, plus
1637 the last byte in each keyword (which may be at a different position for each
1638 keyword, obviously). Keywords with length less than the indicated byte positions
1639 work properly, since selected byte positions exceeding the keyword length
1640 are simply not referenced in the hash function.
1641 .Pp
1642 This option is not normally needed since version 2.8 of
1643 .Li gperf
1644 ; the default byte positions are computed depending on the keyword set, through
1645 a search that minimizes the number of byte positions.
1646 .Pp
1647 .It  -D
1648 .It  --duplicates
1649 Handle keywords whose selected byte sets hash to duplicate values. Duplicate
1650 hash values can occur if a set of keywords has the same names, but possesses
1651 different attributes, or if the selected byte positions are not well chosen.
1652 With the -D option
1653 .Li gperf
1654 treats all these keywords as part of an equivalence class and generates a
1655 perfect hash function with multiple comparisons for duplicate keywords. It
1656 is up to you to completely disambiguate the keywords by modifying the generated
1657 C code. However,
1658 .Li gperf
1659 helps you out by organizing the output.
1660 .Pp
1661 Using this option usually means that the generated hash function is no longer
1662 perfect. On the other hand, it permits
1663 .Li gperf
1664 to work on keyword sets that it otherwise could not handle.
1665 .Pp
1666 .It  -m Va iterations
1667 .It  --multiple-iterations= Va iterations
1668 Perform multiple choices of the
1669 .Li -i
1670 and
1671 .Li -j
1672 values, and choose the best results. This increases the running time by a
1673 factor of
1674 .Va iterations
1675 but does a good job minimizing the generated table size.
1676 .Pp
1677 .It  -i Va initial-value
1678 .It  --initial-asso= Va initial-value
1679 Provides an initial
1680 .Va value
1681 for the associate values array. Default is 0. Increasing the initial value
1682 helps inflate the final table size, possibly leading to more time efficient
1683 keyword lookups. Note that this option is not particularly useful when
1684 .Li -S
1685 (or, equivalently,
1686 .Li %switch )
1687 is used. Also,
1688 .Li -i
1689 is overridden when the
1690 .Li -r
1691 option is used.
1692 .Pp
1693 .It  -j Va jump-value
1694 .It  --jump= Va jump-value
1695 Affects the \(lqjump value\(rq, i.e., how far to advance the associated byte value
1696 upon collisions.
1697 .Va Jump-value
1698 is rounded up to an odd number, the default is 5. If the
1699 .Va jump-value
1700 is 0
1701 .Li gperf
1702 jumps by random amounts.
1703 .Pp
1704 .It  -n
1705 .It  --no-strlen
1706 Instructs the generator not to include the length of a keyword when computing
1707 its hash value. This may save a few assembly instructions in the generated
1708 lookup table.
1709 .Pp
1710 .It  -r
1711 .It  --random
1712 Utilizes randomness to initialize the associated values table. This frequently
1713 generates solutions faster than using deterministic initialization (which
1714 starts all associated values at 0). Furthermore, using the randomization option
1715 generally increases the size of the table.
1716 .Pp
1717 .It  -s Va size-multiple
1718 .It  --size-multiple= Va size-multiple
1719 Affects the size of the generated hash table. The numeric argument for this
1720 option indicates \(lqhow many times larger or smaller\(rq the maximum associated value
1721 range should be, in relationship to the number of keywords. It can be written
1722 as an integer, a floating-point number or a fraction. For example, a value
1723 of 3 means \(lqallow the maximum associated value to be about 3 times larger than
1724 the number of input keywords\(rq. Conversely, a value of 1/3 means \(lqallow the maximum
1725 associated value to be about 3 times smaller than the number of input keywords\(rq.
1726 Values smaller than 1 are useful for limiting the overall size of the generated
1727 hash table, though the option
1728 .Li -m
1729 is better at this purpose.
1730 .Pp
1731 If `generate switch' option
1732 .Li -S
1733 (or, equivalently,
1734 .Li %switch )
1735 is
1736 .Em not
1737 enabled, the maximum associated value influences the static array table size,
1738 and a larger table should decrease the time required for an unsuccessful search,
1739 at the expense of extra table space.
1740 .Pp
1741 The default value is 1, thus the default maximum associated value about the
1742 same size as the number of keywords (for efficiency, the maximum associated
1743 value is always rounded up to a power of 2). The actual table size may vary
1744 somewhat, since this technique is essentially a heuristic.
1745 .El
1746 .Pp
1747 .Ss  Informative Output
1748 .Bl -tag -width Ds
1749 .It  -h
1750 .It  --help
1751 Prints a short summary on the meaning of each program option. Aborts further
1752 program execution.
1753 .Pp
1754 .It  -v
1755 .It  --version
1756 Prints out the current version number.
1757 .Pp
1758 .It  -d
1759 .It  --debug
1760 Enables the debugging option. This produces verbose diagnostics to \(lqstandard
1761 error\(rq when
1762 .Li gperf
1763 is executing. It is useful both for maintaining the program and for determining
1764 whether a given set of options is actually speeding up the search for a solution.
1765 Some useful information is dumped at the end of the program when the
1766 .Li -d
1767 option is enabled.
1768 .El
1769 .Pp
1770 .Sh  Known Bugs and Limitations with Li gperf
1771 The following are some limitations with the current release of
1772 .Li gperf :
1773 .Pp
1774 .Bl -bullet
1775 .It
1776 The
1777 .Li gperf
1778 utility is tuned to execute quickly, and works quickly for small to medium
1779 size data sets (around 1000 keywords). It is extremely useful for maintaining
1780 perfect hash functions for compiler keyword sets. Several recent enhancements
1781 now enable
1782 .Li gperf
1783 to work efficiently on much larger keyword sets (over 15,000 keywords). When
1784 processing large keyword sets it helps greatly to have over 8 megs of RAM.
1785 .Pp
1786 .It
1787 The size of the generate static keyword array can get
1788 .Em extremely
1789 large if the input keyword file is large or if the keywords are quite similar.
1790 This tends to slow down the compilation of the generated C code, and
1791 .Em greatly
1792 inflates the object code size. If this situation occurs, consider using the
1793 .Li -S
1794 option to reduce data size, potentially increasing keyword recognition time
1795 a negligible amount. Since many C compilers cannot correctly generate code
1796 for large switch statements it is important to qualify the
1797 .Va -S
1798 option with an appropriate numerical argument that controls the number of
1799 switch statements generated.
1800 .Pp
1801 .It
1802 The maximum number of selected byte positions has an arbitrary limit of 255.
1803 This restriction should be removed, and if anyone considers this a problem
1804 write me and let me know so I can remove the constraint.
1805 .El
1806 .Pp
1807 .Sh  Things Still Left to Do
1808 It should be \(lqrelatively\(rq easy to replace the current perfect hash function
1809 algorithm with a more exhaustive approach; the perfect hash module is essential
1810 independent from other program modules. Additional worthwhile improvements
1811 include:
1812 .Pp
1813 .Bl -bullet
1814 .It
1815 Another useful extension involves modifying the program to generate \(lqminimal\(rq
1816 perfect hash functions (under certain circumstances, the current version can
1817 be rather extravagant in the generated table size). This is mostly of theoretical
1818 interest, since a sparse table often produces faster lookups, and use of the
1819 .Li -S
1820 .Li switch
1821 option can minimize the data size, at the expense of slightly longer lookups
1822 (note that the gcc compiler generally produces good code for
1823 .Li switch
1824 statements, reducing the need for more complex schemes).
1825 .Pp
1826 .It
1827 In addition to improving the algorithm, it would also be useful to generate
1828 an Ada package as the code output, in addition to the current C and C++ routines.
1829 .El
1830 .Pp
1831 .Sh  Bibliography
1832 [1] Chang, C.C.:
1833 .Em A Scheme for Constructing Ordered Minimal Perfect Hashing Functions
1834 Information Sciences 39(1986), 187-195.
1835 .Pp
1836 [2] Cichelli, Richard J.
1837 .Em Author's Response to \(lqOn Cichelli's Minimal Perfect Hash Functions Method\(rq
1838 Communications of the ACM, 23, 12(December 1980), 729.
1839 .Pp
1840 [3] Cichelli, Richard J.
1841 .Em Minimal Perfect Hash Functions Made Simple
1842 Communications of the ACM, 23, 1(January 1980), 17-19.
1843 .Pp
1844 [4] Cook, C. R. and Oldehoeft, R.R.
1845 .Em A Letter Oriented Minimal Perfect Hashing Function
1846 SIGPLAN Notices, 17, 9(September 1982), 18-27.
1847 .Pp
1848 [5] Cormack, G. V. and Horspool, R. N. S. and Kaiserwerth, M.
1849 .Em Practical Perfect Hashing
1850 Computer Journal, 28, 1(January 1985), 54-58.
1851 .Pp
1852 [6] Jaeschke, G.
1853 .Em Reciprocal Hashing: A Method for Generating Minimal Perfect Hashing Functions
1854 Communications of the ACM, 24, 12(December 1981), 829-833.
1855 .Pp
1856 [7] Jaeschke, G. and Osterburg, G.
1857 .Em On Cichelli's Minimal Perfect Hash Functions Method
1858 Communications of the ACM, 23, 12(December 1980), 728-729.
1859 .Pp
1860 [8] Sager, Thomas J.
1861 .Em A Polynomial Time Generator for Minimal Perfect Hash Functions
1862 Communications of the ACM, 28, 5(December 1985), 523-532
1863 .Pp
1864 [9] Schmidt, Douglas C.
1865 .Em GPERF: A Perfect Hash Function Generator
1866 Second USENIX C++ Conference Proceedings, April 1990.
1867 .Pp
1868 [10] Schmidt, Douglas C.
1869 .Em GPERF: A Perfect Hash Function Generator
1870 C++ Report, SIGS 10 10 (November/December 1998).
1871 .Pp
1872 [11] Sebesta, R.W. and Taylor, M.A.
1873 .Em Minimal Perfect Hash Functions for Reserved Word Lists
1874 SIGPLAN Notices, 20, 12(September 1985), 47-53.
1875 .Pp
1876 [12] Sprugnoli, R.
1877 .Em Perfect Hashing Functions: A Single Probe Retrieving Method for Static Sets
1878 Communications of the ACM, 20 11(November 1977), 841-850.
1879 .Pp
1880 [13] Stallman, Richard M.
1881 .Em Using and Porting GNU CC
1882 Free Software Foundation, 1988.
1883 .Pp
1884 [14] Stroustrup, Bjarne
1885 .Em The C++ Programming Language.
1886 Addison-Wesley, 1986.
1887 .Pp
1888 [15] Tiemann, Michael D.
1889 .Em User's Guide to GNU C++
1890 Free Software Foundation, 1989.
1891 .Pp
1892 .Sh  Concept Index