使用 gperf 时遇到问题:不允许输入空关键字

发布于 2024-10-22 04:53:30 字数 935 浏览 4 评论 0原文

command_options.gperf:

%{
#include "command_options.h"
typedef struct CommandOptionCode CommandOptionCode;
%}
struct CommandOption
  {
  const char *Option;
  int OptionCode;
  };
%%
+helpverbose, CommandOptionCode::HELPVERBOSE
+password, CommandOptionCode::PASSWORD
+nocopyright, CommandOptionCode::NOCOPYRIGHT
+nolog, CommandOptionCode::NOLOG
+_64bit, CommandOptionCode::_64BIT

command_options.h:

#ifndef __COMMANDOPTIONS_H
#define __COMMANDOPTIONS_H
struct CommandOptionCode 
  {
  enum 
    {
    HELPVERBOSE = 1,
    PASSWORD = 2,
    NOCOPYRIGHT = 3,
    NOLOG = 4,
    _64BIT = 5
    };
  };
#endif

当我运行时:

gperf  -L C++ -t --output-file=perfecthash.hpp command_options.gperf

仅获取:

不允许输入空关键字。到 识别空输入关键字,您的 代码应该在之前检查 len == 0 调用 gperf 生成的查找 功能。

版本:GNU gperf 3.0.1 为什么?

command_options.gperf:

%{
#include "command_options.h"
typedef struct CommandOptionCode CommandOptionCode;
%}
struct CommandOption
  {
  const char *Option;
  int OptionCode;
  };
%%
+helpverbose, CommandOptionCode::HELPVERBOSE
+password, CommandOptionCode::PASSWORD
+nocopyright, CommandOptionCode::NOCOPYRIGHT
+nolog, CommandOptionCode::NOLOG
+_64bit, CommandOptionCode::_64BIT

command_options.h:

#ifndef __COMMANDOPTIONS_H
#define __COMMANDOPTIONS_H
struct CommandOptionCode 
  {
  enum 
    {
    HELPVERBOSE = 1,
    PASSWORD = 2,
    NOCOPYRIGHT = 3,
    NOLOG = 4,
    _64BIT = 5
    };
  };
#endif

When I run:

gperf  -L C++ -t --output-file=perfecthash.hpp command_options.gperf

Only to get :

Empty input keyword is not allowed. To
recognize an empty input keyword, your
code should check for len == 0 before
calling the gperf generated lookup
function.

Version: GNU gperf 3.0.1
Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海之角 2024-10-29 04:53:30

我发现 gperf 2.7 不关心第一部分和关键字之间是否有“%%”分隔符。 3.0.1 严格执行这一点。因此,就我而言,我进行了修改:

%{
#include <string.h>
%}

scan

我相信,您的情况有所

%{
#include <string.h>
%}
%%

scan

不同,因为手册规定结构的第一个字段必须称为“名称”:

"This first field must be called `name', although it is possible to modify its name with the `-K' option (or, equivalently, the `%define slot-name' declaration) described below."

-charlie

I discovered that gperf 2.7 did not care if there was a '%%' delimiter between the first section and the keywords. 3.0.1 strictly enforces this. So, in my case I had modify:

%{
#include <string.h>
%}

scan

to be

%{
#include <string.h>
%}
%%

scan

Your case is different, I belive, in that the manual states that the first field of the struct must be called 'name':

"This first field must be called `name', although it is possible to modify its name with the `-K' option (or, equivalently, the `%define slot-name' declaration) described below."

-charlie

绮烟 2024-10-29 04:53:30

我发现 gperf 不喜欢关键字部分中的空行。我猜它将空行视为空字符串,因为它不是注释,并抱怨它是“空”并且 len=0。由于我有总是以空行结束文件的习惯(有一些汇编器和编译器不愿意没有空行),所以它总是会成为一个问题!

I discovered that gperf does not like empty lines in the keyword section. It treats a blank line as a null string I guess, since it is not a comment, and complains about it being 'empty' and having len=0. Since I have a habit of always ending a file with an empty line (there are some assemblers and compilers that baulk at not having it) it was always going to be a problem!

白龙吟 2024-10-29 04:53:30

除了 Richard 提到的空行问题之外,gperf 也不喜欢某些标记前面的空格。 (我剪切并粘贴了一个简单的 gperf 示例,该示例在用户输入中查找“粗鲁”单词。示例的名称是 rude-1.gperf。该示例有一些缩进,引发了同样的错误。)

In addition to the blank line issue mentioned by Richard, gperf also does not like spaces preceding certain tokens. (I had cut and pasted a simple gperf sample that looked for "rude" words in a user's input. The name of the sample was rude-1.gperf. The sample had some indentation that triggered this same error.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文