6 .Nd Perfect Hash Function Generator
8 This manual documents the GNU
10 perfect hash function generator utility, focusing on its features and how
11 to use them, and how to report bugs.
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.
18 Everyone is permitted to copy and distribute verbatim copies of this license
19 document, but changing it is not allowed.
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.
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.
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.
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.
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.
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.
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.
65 The precise terms and conditions for copying, distribution and modification
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.
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
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.
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.
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
105 You must cause the modified files to carry prominent notices stating that
106 you changed the files and the date of any change.
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.
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.)
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
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
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
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:
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,
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,
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.)
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.
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
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.
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.
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.
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.
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.
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
237 This section is intended to make thoroughly clear what is believed to be a
238 consequence of the rest of this License.
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
319 Also add information on how to contact you by electronic and paper mail.
321 If the program is interactive, make it output a short notice like this when
322 it starts in an interactive mode:
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'
332 The hypothetical commands
336 should show the appropriate parts of the General Public License. Of course,
337 the commands you use may be called something other than
341 ; they could even be mouse-clicks or menu items---whatever suits your program.
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:
347 .Bd -literal -offset indent
349 Yoyodyne, Inc., hereby disclaims all copyright
350 interest in the program `Gnomovision'
351 (which makes passes at compilers) written
354 signature of Ty Coon, 1 April 1989
355 Ty Coon, President of Vice
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.
365 .Sh Contributors to GNU Li gperf Utility
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> .
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.
382 In addition, Adam de Boor and Nels Olson provided many tips and insights that
383 greatly helped improve the quality and functionality of
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
394 is a perfect hash function generator written in C++. It transforms an
396 element user-specified keyword set
398 into a perfect hash function
401 uniquely maps keywords in
417 perfect hash function.
421 element static lookup table and a pair of C functions. These functions determine
422 whether a given character string
426 using at most one probe into the lookup table.
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
435 .Li http://ftp.gnu.org/pub/gnu/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 .
442 .Sh Static search structures and GNU Li gperf
444 .Em static search structure
445 is an Abstract Data Type with certain fundamental operations, e.g.,
450 Conceptually, all insertions occur before any retrievals. In practice,
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
462 are inserted into the structure only once, usually during program initialization,
463 and are not generally modified at run-time.
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.
470 element sorted array is space efficient, though the average-case time complexity
471 for retrieval operations using binary search is proportional to log
473 Conversely, hash table implementations often locate a table entry in constant
474 time, but typically impose additional memory overhead and exhibit poor worst
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:
483 It allows keyword recognition in a static search set using at most
485 probe into the hash table. This represents the \(lqperfect\(rq property.
487 The actual memory allocated to store the keywords is precisely large enough
488 for the keyword set, and
490 This is the \(lqminimal\(rq property.
493 For most applications it is far easier to generate
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.
502 \&'s default behavior generates
504 perfect hash functions for keyword sets. However,
506 provides many options that permit user control over the degree of minimality
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
514 if it subsequently receives heavy use multiple times.
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
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
524 to automatically generate static search structures that efficiently identify
525 their respective reserved keywords.
527 .Sh High-Level Description of GNU Li gperf
528 The perfect hash function generator
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
533 .Em static keyword set
534 with at most a single probe into the lookup table. If
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
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
552 statement scheme that minimizes data space storage size. Furthermore, using
555 may actually speed up the keyword retrieval time somewhat. Actual results
556 depend on your C compiler, of course.
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
564 to find and generate a perfect hash function. Experimentation is the key to
565 getting the most from
568 .Ss Input Format to Li gperf
569 You can control the input file format by varying certain command-line arguments,
572 option. The input's appearance is similar to GNU utilities
580 Here's an outline of the general format:
582 .Bd -literal -offset indent
596 the declarations section and the functions section are optional. The following
597 sections describe the input format for each section.
599 It is possible to omit the declaration section entirely, if the
601 option is not given. In this case the input file begins directly with the
602 first keyword line, e.g.:
604 .Bd -literal -offset indent
616 The keyword input file optionally contains a section for including arbitrary
617 C declarations and definitions,
619 declarations that act like command-line options, as well as for providing
623 .No User-supplied Li struct
627 option (or, equivalently, the
635 as the last component in the declaration section from the input file. The
636 first field in this struct must be of type
642 option is not given, or of type
646 (or, equivalently, the
648 declaration) is enabled. This first field must be called
650 although it is possible to modify its name with the
652 option (or, equivalently, the
653 .Li %define slot-name
654 declaration) described below.
656 Here is a simple example, using months of the year and their attributes as
659 .Bd -literal -offset indent
661 struct month { char *name; int number; int days; int leap_days; };
680 declaration from the list of keywords and other fields are a pair of consecutive
683 appearing left justified in the first column, as in the UNIX utility
688 has already been declared in an include file, it can be mentioned in an abbreviated
691 .Bd -literal -offset indent
700 .No Gperf Declarations
702 The declaration section can contain
704 declarations. They influence the way
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:
711 Declarations without argument, like
712 .Li %compare-lengths .
715 Declarations with an argument, like
716 .Li %switch= Va count .
719 Declarations of names of entities in the output file, like
720 .Li %define lookup-function-name Va name .
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.
728 declarations are available.
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.
737 Allows you to include a
739 type declaration for generated code; see above for an example.
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.
746 .It %language= Va language-name
749 to generate code in the language specified by the option's argument. Languages
750 handled are currently:
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)
760 Common C. This language is understood by ANSI C compilers, and also by old-style
761 C compilers, provided that you
763 to empty for compilers which don't know about this keyword.
766 ANSI C. This language is understood by ANSI C compilers and C++ compilers.
769 C++. This language is understood by C++ compilers.
774 .It %define slot-name Va name
775 This declaration is only useful when option
777 (or, equivalently, the
779 declaration) has been given. By default, the program assumes the structure
780 component identifier for the keyword is
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
786 .It %define initializer-suffix Va initializers
787 This declaration is only useful when option
789 (or, equivalently, the
791 declaration) has been given. It permits to specify initializers for the structure
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
799 .It %define hash-function-name Va name
800 Allows you to specify the name for the generated hash function. Default name
803 This option permits the use of two hash tables in the same file.
805 .It %define lookup-function-name Va name
806 Allows you to specify the name for the generated lookup function. Default
809 This option permits multiple generated hash functions to be used in the same
812 .It %define class-name Va name
813 This option is only useful when option
815 (or, equivalently, the
817 declaration) has been given. It allows you to specify the name of generated
818 C++ class. Default name is
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
831 guarantee that a byte is in this range. Only an explicit test like
832 .Li c >= 'A' && c <= 'Z'
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
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
849 is not enabled), since the length table contains as many elements as there
850 are entries in the lookup table.
853 Generates C code that uses the
855 function to perform string comparisons. The default action is to use
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
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> .
870 Include the necessary system include file,
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.
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).
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
884 declaration (or, equivalently, the option
886 is also given, the first field of the user-defined struct must be of type
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 ,
895 is the offset. The string pool name can be changed through the
896 .Li %define string-pool-name
899 .It %define string-pool-name Va name
900 Allows you to specify the name of the generated string pool created by the
903 (or, equivalently, the option
907 This declaration permits the use of two hash tables in the same file, with
911 declaration (or, equivalently, the option
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
920 at the expense of one more test-and-branch instruction at run time.
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
926 This option permits the use of two hash tables in the same file, even when
929 (or, equivalently, the
931 declaration) is given.
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
937 This option permits the use of two length tables in the same file, even when
940 (or, equivalently, the
942 declaration) is given.
944 .It %switch= Va count
945 Causes the generated C code to use a
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
951 statements are generated. A value of 1 generates 1
953 containing all the elements, a value of 2 generates 2 tables with 1/2 the
956 etc. This is useful since many C compilers cannot correctly generate code
959 statements. This option was inspired in part by Keith Bostic's original C
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.
969 Using a syntax similar to GNU utilities
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
978 pairs. Here is an input fragment based on the previous example that illustrates
981 .Bd -literal -offset indent
985 /* This section of code is inserted directly into the output. */
986 int return_month_days (struct month *months, int is_leap_year);
988 struct month { char *name; int number; int days; int leap_days; };
997 .Em Format for Keyword Entries
999 The second input file format section contains lines of keywords and any associated
1000 attributes you might supply. A line beginning with
1002 in the first column is considered a comment. Everything following the
1004 is ignored, up to and including the following newline. A line beginning with
1006 in the first column is an option declaration and must not occur within the
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
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:
1023 .Bd -literal -offset indent
1025 # These are a few C reserved words, see the c.gperf file
1026 # for a complete list of ANSI C reserved words.
1045 marker may be elided if the declaration section is empty.
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
1052 provided by you in the declaration section. If the
1054 option (or, equivalently, the
1058 enabled these fields are simply ignored. All previous examples except the
1059 last one contain keyword attributes.
1061 .Em Including Additional C Functions
1063 The optional third section also corresponds closely with conventions found
1068 All text in this section, starting at the final
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.
1074 .Em Where to place directives for GNU Li indent.
1076 If you want to invoke GNU
1080 input file, you will see that GNU
1082 doesn't understand the
1087 directives that control
1089 \&'s interpretation of the input file. Therefore you have to insert some directives
1092 More precisely, assuming the most general input file structure
1094 .Bd -literal -offset indent
1112 comments as follows:
1114 .Bd -literal -offset indent
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
1138 although you may modify their names with a command-line option. Both functions
1139 require two arguments, a string,
1142 and a length parameter,
1145 Their default function prototypes are as follows:
1150 .Fa (const char * Va str, unsigned int Va len)
1153 By default, the generated
1155 function returns an integer value created by adding
1157 to several user-specified
1159 byte positions indexed into an
1160 .Em associated values
1161 table stored in a local static array. The associated values table is constructed
1164 and later output as a static local C array called
1166 The relevant selected positions (i.e. indices into
1168 are specified via the
1174 section below (see Section
1180 .Fa (const char * Va str, unsigned int Va len)
1185 is in the keyword set, returns a pointer to that keyword. More exactly, if
1188 (or, equivalently, the
1190 declaration) was given, it returns a pointer to the matching keyword's structure.
1191 Otherwise it returns
1196 (or, equivalently, the
1197 .Li %compare-strncmp
1198 declaration) is not used,
1200 must be a NUL terminated string of exactly length
1204 (or, equivalently, the
1205 .Li %compare-strncmp
1206 declaration) is used,
1208 must simply be an array of
1210 bytes and does not need to be NUL terminated.
1212 The code generated for these two functions is affected by the following options:
1217 Make use of the user-defined
1220 .It -S Va total-switch-statements
1221 .It --switch= Va total-switch-statements
1222 Generate 1 or more C
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.
1234 options (or, equivalently, the
1238 declarations) are omitted, the default action is to generate a
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.
1245 .Ss Use of NUL bytes
1246 By default, the code generated by
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,
1256 must be NUL terminated and have exactly length
1261 (or, equivalently, the
1262 .Li %compare-strncmp
1263 declaration) is used, then the
1265 argument does not need to be NUL terminated. The code generated by
1267 will only access the first
1273 However, the keywords in the input file still must not contain NUL bytes.
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
1285 and the code generated by
1287 will treat NUL like any other byte. Also, in this case the
1289 option (or, equivalently, the
1290 .Li %compare-strncmp
1291 declaration) is ignored.
1293 .Sh Invoking 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
1301 option. Here is the complete list of options.
1303 .Ss Specifying the Location of the Output File
1305 .It --output-file= Va file
1306 Allows you to specify the name of the file to which the output is written
1310 The results are written to standard output if no output file is specified
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 ) .
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.
1328 Allows you to include a
1330 type declaration for generated code. Any text before a pair of consecutive
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.
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
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
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 ) .
1357 .It -L Va generated-language-name
1358 .It --language= Va generated-language-name
1361 to generate code in the language specified by the option's argument. Languages
1362 handled are currently:
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)
1372 Common C. This language is understood by ANSI C compilers, and also by old-style
1373 C compilers, provided that you
1375 to empty for compilers which don't know about this keyword.
1378 ANSI C. This language is understood by ANSI C compilers and C++ compilers.
1381 C++. This language is understood by C++ compilers.
1387 This option is supported for compatibility with previous releases of
1389 It does not do anything.
1392 This option is supported for compatibility with previous releases of
1394 It does not do anything.
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
1400 .Dq Gperf Declarations ) .
1404 .It --slot-name= Va slot-name
1405 This option is only useful when option
1407 (or, equivalently, the
1409 declaration) has been given. By default, the program assumes the structure
1410 component identifier for the keyword is
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
1416 .It -F Va initializers
1417 .It --initializer-suffix= Va initializers
1418 This option is only useful when option
1420 (or, equivalently, the
1422 declaration) has been given. It permits to specify initializers for the structure
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
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
1435 This option permits the use of two hash tables in the same file.
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
1442 This option permits multiple generated hash functions to be used in the same
1445 .It -Z Va class-name
1446 .It --class-name= Va class-name
1447 This option is only useful when option
1449 (or, equivalently, the
1451 declaration) has been given. It allows you to specify the name of generated
1452 C++ class. Default name is
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
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
1470 earlier than 2.7; now the default is to support 8-bit and multibyte characters.
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
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
1487 is not enabled), since the length table contains as many elements as there
1488 are entries in the lookup table.
1491 .It --compare-strncmp
1492 Generates C code that uses the
1494 function to perform string comparisons. The default action is to use
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
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> .
1512 Include the necessary system include file,
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.
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).
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
1528 (or, equivalently, the
1530 declaration) is also given, the first field of the user-defined struct must
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 ,
1540 is the offset. The string pool name can be changed through the option
1541 .Li --string-pool-name .
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
1549 This option permits the use of two hash tables in the same file, with
1551 and even when the option
1553 (or, equivalently, the
1555 declaration) is given.
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
1562 at the expense of one more test-and-branch instruction at run time.
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
1569 This option permits the use of two hash tables in the same file, even when
1572 (or, equivalently, the
1574 declaration) is given.
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
1580 This option permits the use of two length tables in the same file, even when
1583 (or, equivalently, the
1585 declaration) is given.
1587 .It -S Va total-switch-statements
1588 .It --switch= Va total-switch-statements
1589 Causes the generated C code to use a
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
1595 statements are generated. A value of 1 generates 1
1597 containing all the elements, a value of 2 generates 2 tables with 1/2 the
1600 etc. This is useful since many C compilers cannot correctly generate code
1603 statements. This option was inspired in part by Keith Bostic's original C
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.
1612 This option is supported for compatibility with previous releases of
1614 It does not do anything.
1617 .Ss Options for changing the Algorithms employed by Li gperf
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
1625 ; ranges may be used, e.g.,
1627 ; and positions may occur in any order. Furthermore, the wildcard '*' causes
1628 the generated hash function to consider
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).
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.
1642 This option is not normally needed since version 2.8 of
1644 ; the default byte positions are computed depending on the keyword set, through
1645 a search that minimizes the number of byte positions.
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.
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
1659 helps you out by organizing the output.
1661 Using this option usually means that the generated hash function is no longer
1662 perfect. On the other hand, it permits
1664 to work on keyword sets that it otherwise could not handle.
1666 .It -m Va iterations
1667 .It --multiple-iterations= Va iterations
1668 Perform multiple choices of the
1672 values, and choose the best results. This increases the running time by a
1675 but does a good job minimizing the generated table size.
1677 .It -i Va initial-value
1678 .It --initial-asso= Va initial-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
1689 is overridden when the
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
1698 is rounded up to an odd number, the default is 5. If the
1702 jumps by random amounts.
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
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.
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
1729 is better at this purpose.
1731 If `generate switch' option
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.
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.
1747 .Ss Informative Output
1751 Prints a short summary on the meaning of each program option. Aborts further
1756 Prints out the current version number.
1760 Enables the debugging option. This produces verbose diagnostics to \(lqstandard
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
1770 .Sh Known Bugs and Limitations with Li gperf
1771 The following are some limitations with the current release of
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
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.
1787 The size of the generate static keyword array can get
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
1792 inflates the object code size. If this situation occurs, consider using the
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
1798 option with an appropriate numerical argument that controls the number of
1799 switch statements generated.
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.
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
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
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
1824 statements, reducing the need for more complex schemes).
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.
1833 .Em A Scheme for Constructing Ordered Minimal Perfect Hashing Functions
1834 Information Sciences 39(1986), 187-195.
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.
1840 [3] Cichelli, Richard J.
1841 .Em Minimal Perfect Hash Functions Made Simple
1842 Communications of the ACM, 23, 1(January 1980), 17-19.
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.
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.
1853 .Em Reciprocal Hashing: A Method for Generating Minimal Perfect Hashing Functions
1854 Communications of the ACM, 24, 12(December 1981), 829-833.
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.
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
1864 [9] Schmidt, Douglas C.
1865 .Em GPERF: A Perfect Hash Function Generator
1866 Second USENIX C++ Conference Proceedings, April 1990.
1868 [10] Schmidt, Douglas C.
1869 .Em GPERF: A Perfect Hash Function Generator
1870 C++ Report, SIGS 10 10 (November/December 1998).
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.
1877 .Em Perfect Hashing Functions: A Single Probe Retrieving Method for Static Sets
1878 Communications of the ACM, 20 11(November 1977), 841-850.
1880 [13] Stallman, Richard M.
1881 .Em Using and Porting GNU CC
1882 Free Software Foundation, 1988.
1884 [14] Stroustrup, Bjarne
1885 .Em The C++ Programming Language.
1886 Addison-Wesley, 1986.
1888 [15] Tiemann, Michael D.
1889 .Em User's Guide to GNU C++
1890 Free Software Foundation, 1989.