⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gperf.texinfo

📁 早期freebsd实现
💻 TEXINFO
📖 第 1 页 / 共 4 页
字号:
The keyword input file optionally contains a section for includingarbitrary C declarations and definitions, as well as provisions forproviding a user-supplied @code{struct}.  If the @samp{-t} option@emph{is} enabled, you @emph{must} provide a C @code{struct} as the lastcomponent in the declaration section from the keyfile file.  The firstfield in this struct must be a @code{char *} identifier called ``name,''although it is possible to modify this field's name with the @samp{-K}option described below.Here is simple example, using months of the year and their attributes asinput:@group@examplestruct months @{ char *name; int number; int days; int leap_days; @};%%january,   1, 31, 31february,  2, 28, 29march,     3, 31, 31april,     4, 30, 30may,       5, 31, 31june,      6, 30, 30july,      7, 31, 31august,    8, 31, 31september, 9, 30, 30october,  10, 31, 31november, 11, 30, 30december, 12, 31, 31@end example@end groupSeparating the @code{struct} declaration from the list of key words andother fields are a pair of consecutive percent signs, @code{%%},appearing left justified in the first column, as in the UNIX utility@code{lex}.Using a syntax similar to GNU utilities @code{flex} and @code{bison}, itis possible to directly include C source text and comments verbatim intothe generated output file.  This is accomplished by enclosing the regioninside left-justified surrounding @code{%@{}, @code{%@}} pairs.  Here isan input fragment based on the previous example that illustrates thisfeature:@group@example%@{#include <assert.h>/* This section of code is inserted directly into the output. */int return_month_days (struct months *months, int is_leap_year);%@}struct months @{ char *name; int number; int days; int leap_days; @};%%january,   1, 31, 31february,  2, 28, 29march,     3, 31, 31...@end example@end groupIt is possible to omit the declaration section entirely.  In this casethe keyfile begins directly with the first keyword line, @emph{e.g.}:@group@examplejanuary,   1, 31, 31february,  2, 28, 29march,     3, 31, 31april,     4, 30, 30...@end example@end group@node Keywords, Functions, Declarations, Input Format@subsection Format for Keyword EntriesThe second keyfile format section contains lines of keywords and anyassociated attributes you might supply.  A line beginning with @samp{#}in the first column is considered a comment.  Everything following the@samp{#} is ignored, up to and including the following newline.The first field of each non-comment line is always the key itself.  Itshould be given as a simple name, @emph{i.e.}, without surroundingstring quotation marks, and be left-justified flush against the firstcolumn.  In this context, a ``field'' is considered to extend up to, butnot include, the first blank, comma, or newline.  Here is a simpleexample taken from a partial list of C reserved words:@group@example# These are a few C reserved words, see the c.@code{gperf} file # for a complete list of ANSI C reserved words.unsignedsizeofswitchsignedifdefaultforwhilereturn@end example@end groupNote that unlike @code{flex} or @code{bison} the first @code{%%} markermay be elided if the declaration section is empty.Additional fields may optionally follow the leading keyword.  Fieldsshould be separated by commas, and terminate at the end of line.  Whatthese fields mean is entirely up to you; they are used to initialize theelements of the user-defined @code{struct} provided by you in thedeclaration section.  If the @samp{-t} option is @emph{not} enabledthese fields are simply ignored.  All previous examples except the lastone contain keyword attributes.@node Functions,  , Keywords, Input Format@subsection Including Additional C FunctionsThe optional third section also corresponds closely with conventionsfound in @code{flex} and @code{bison}.  All text in this section,starting at the final @code{%%} and extending to the end of the inputfile, is included verbatim into the generated output file.  Naturally,it is your responsibility to ensure that the code contained in thissection is valid C.@node Output Format,  , Input Format, Description@section Output Format for Generated C Code with @code{gperf}Several options control how the generated C code appears on the standardoutput.  Two C function are generated.  They are called @code{hash} and@code{in_word_set}, although you may modify the name for@code{in_word_set} with a command-line option.  Both functions requiretwo arguments, a string, @code{char *} @var{str}, and a lengthparameter, @code{int} @var{len}.  Their default function prototypes areas follows:@group@examplestatic int hash (char *str, int len);int in_word_set (char *str, int len);@end example@end groupBy default, the generated @code{hash} function returns an integer valuecreated by adding @var{len} to several user-specified @var{str} keypositions indexed into an @dfn{associated values} table stored in alocal static array.  The associated values table is constructedinternally by @code{gperf} and later output as a static local C array called@var{hash_table}; its meaning and properties are described below.@xref{Implementation}. The relevant key positions are specified via the@samp{-k} option when running @code{gperf}, as detailed in the @emph{Options}section below. @xref{Options}.Two options, @samp{-g} (assume you are compiling with GNU C and its@code{inline} feature) and @samp{-a} (assume ANSI C-style functionprototypes), alter the content of both the generated @code{hash} and@code{in_word_set} routines.  However, function @code{in_word_set} maybe modified more extensively, in response to your option settings.  Theoptions that affect the @code{in_word_set} structure are:@itemize @bullet@table @samp@item -pHave function @code{in_word_set} return a pointer rather than a boolean.@item -tMake use of the user-defined @code{struct}.@item -S @var{total switch statements}Generate 1 or more C @code{switch} statement rather than use a large,(and potentially sparse) static array.  Although the exact time andspace savings of this approach vary according to your C compiler'sdegree of optimization, this method often results in smaller and fastercode.@end table@end itemizeIf the @samp{-t}, @samp{-S}, and @samp{-p} options are omitted thedefault action is to generate a @code{char *} array containing the keys,together with additional null strings used for padding the array.  Byexperimenting with the various input and output options, and timing theresulting C code, you can determine the best option choices fordifferent keyword set characteristics.@node Options, Bugs, Description, Top@chapter Options to the @code{gperf} UtilityThere are @emph{many} options to @code{gperf}.  They were added to makethe program more convenient for use with real applications.  ``On-line''help is readily available via the @samp{-h} option.  Other optionsinclude:@itemize @bullet@table @samp@item -aGenerate ANSI Standard C code using function prototypes.  The default isto use ``classic'' K&R C function declaration syntax.@item -cGenerates C code that uses the @code{strncmp} function to performstring comparisons.  The default action is to use @code{strcmp}.@item -CMakes the contents of all generated lookup tables constant, @emph{i.e.},``readonly.''  Many compilers can generate more efficient code for thisby putting the tables in readonly memory.@item -dEnables the debugging option.  This produces verbose diagnostics to``standard error'' when @code{gperf} is executing.  It is useful both formaintaining the program and for determining whether a given set ofoptions is actually speeding up the search for a solution.  Some usefulinformation is dumped at the end of the program when the @samp{-d}option is enabled.@item -DHandle keywords whose key position sets hash to duplicate values.Duplicate hash values occur for two reasons:@itemize @bullet@itemSince @code{gperf} does not backtrack it is possible for it to processall your input keywords without finding a unique mapping for each word.However, frequently only a very small number of duplicates occur, and the majority of keys still require one probe into the table.@itemSometimes a set of keys may have the same names, but possess differentattributes.  With the -D option @code{gperf} treats all these keys as part ofan equivalence class and generates a perfect hash function with multiplecomparisons for duplicate keys.  It is up to you to completelydisambiguate the keywords by modifying the generated C code.  However,@code{gperf} helps you out by organizing the output.@end itemizeOption @samp{-D} is extremely useful for certain large or highlyredundant keyword sets, @emph{i.e.}, assembler instruction opcodes.Using this option usually means that the generated hash function is nolonger perfect.  On the other hand, it permits @code{gperf} to work onkeyword sets that it otherwise could not handle.@item -e @var{keyword delimiter list}Allows the user to provide a string containing delimiters used toseparate keywords from their attributes.  The default is ",\n".  Thisoption is essential if you want to use keywords that have embeddedcommas or newlines.  One useful trick is to use -e'TAB', where TAB isthe literal tab character.@item -EDefine constant values using an enum local to the lookup function ratherthan with #defines.  This also means that different lookup functions canreside in the same file.  Thanks to James Clark (jjc at ai.mit.edu).@item -f @var{iteration amount}Generate the perfect hash function ``fast.''  This decreases @code{gperf}'srunning time at the cost of minimizing generated table-size.  Theiteration amount represents the number of times to iterate whenresolving a collision.  `0' means `iterate by the number of keywords.This option is probably most useful when used in conjunction with options@samp{-D} and/or @samp{-S} for @emph{large} keyword sets.@item -gAssume a GNU compiler, @emph{e.g.}, @code{g++} or @code{gcc}.  Thismakes all generated routines use the ``inline'' keyword to remove thecost of function calls.  Note that @samp{-g} does @emph{not} imply@samp{-a}, since other non-ANSI C compilers may have provisions for afunction @code{inline} feature.@item -GGenerate the static table of keywords as a static global variable,rather than hiding it inside of the lookup function (which is thedefault behavior).@item -hPrints a short summary on the meaning of each program option.  Abortsfurther program execution.@item -H @var{hash function name}Allows you to specify the name for the generated hash function.  Defaultname is `hash.'  This option permits the use of two hash tables in thesame file.@item -i @var{initial value}Provides an initial @var{value} for the associate values array.  Defaultis 0.  Increasing the initial value helps inflate the final table size,possibly leading to more time efficient keyword lookups.  Note that thisoption is not particularly useful when @samp{-S} is used.  Also,@samp{-i} is overriden when the @samp{-r} option is used.@item -j @var{jump value}Affects the ``jump value,'' @emph{i.e.}, how far to advance theassociated character value upon collisions.  @var{Jump value} is roundedup to an odd number, the default is 5.  If the @var{jump value} is 0 @code{gperf}jumps by random amounts.@item -k @var{keys}Allows selection of the character key positions used in the keywords'hash function. The allowable choices range between 1-126, inclusive.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -