无法访问野牛2.6中的令牌表yytname还是消失了? [英] can't access the token table yytname in bison 2.6 or is it gone?

查看:101
本文介绍了无法访问野牛2.6中的令牌表yytname还是消失了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为资产xchange格式构建解析器.而且我在野牛文件中包含了%token-table指令,但是,从flex代码中,我只是无法访问表或与之关联的常量.就是在尝试编译此代码时:

I'm building a parser for an asset xchange format. And I'm including the %token-table directive in the bison file but, from the flex code I just can't access the table or the constants associated with it. That is when trying to compile this code:

Frame|FrameTransformMatrix|Mesh|MeshNormals|MeshMaterialList|Material {
    printf("A keyword: %s\n", yytext);
    yylval.charptr_type = yytext;

    int i;
    for (i = 0; i < YYNTOKENS; i++)
    {
        if (yytname[i] != 0
            && yytname[i][0] == '"'
            && !strncmp(yytname[i] + 1, yytext, strlen(yytext))
            && yytname[i][strlen(yytext) + 1] == '"'
            && yytname[i][strlen(yytext) + 2] == 0)
            return i;
    }
}

gcc说YYNTOKENS和yytname都未声明.那么令牌表最终被弃用并抹掉了吗?

gcc says both YYNTOKENS and yytname are undeclared. So was the token table finally deprecated and wiped or what's the deal?

推荐答案

Bison 2.6.2手册说(在PDF中的p82上):

The Bison 2.6.2 manual says (on p82 in the PDF):

%token-table [指令]

在解析器实现文件中生成令牌名称的数组.的名称 数组是yytname; yytname[i]是其内部Bison令牌的令牌的名称 代码编号为i. yytname的前三个元素对应于预定义的 标记"$end","error"和"$undefined";在这些之后,定义了 语法文件.

Generate an array of token names in the parser implementation file. The name of the array is yytname; yytname[i] is the name of the token whose internal Bison token code number is i. The first three elements of yytname correspond to the predefined tokens "$end", "error", and "$undefined"; after these come the symbols defined in the grammar file.

表中的名称包括表示令牌中所需的所有字符 野牛对于单字符文字和文字字符串,这包括 引用字符和任何转义序列.例如,Bison单字符 文字’+’对应于三个字符的名称,用C表示为"’+’";和 Bison两字符文字字符串"\\/"对应于五个字符的名称, 在C中以"\"\\\\/\""表示.

The name in the table includes all the characters needed to represent the token in Bison. For single-character literals and literal strings, this includes the surrounding quoting characters and any escape sequences. For example, the Bison single-character literal ’+’ corresponds to a three-character name, represented in C as "’+’"; and the Bison two-character literal string "\\/" corresponds to a five-character name, represented in C as "\"\\\\/\"".

当您指定%token-table时,Bison还将为宏生成宏定义 YYNTOKENSYYNNTSYYNRULESYYNSTATES:

When you specify %token-table, Bison also generates macro definitions for macros YYNTOKENS, YYNNTS, and YYNRULES, and YYNSTATES:

YYNTOKENS 最高的令牌号加一个.

YYNTOKENS The highest token number, plus one.

YYNNTS非终结符的数量.

YYNRULES语法规则的数量,

YYNSTATES 解析器状态的数量(请参阅第5.5节[解析器状态],第104页).

YYNSTATES The number of parser states (see Section 5.5 [Parser States], page 104).

看起来应该在那里.

当我尝试一个简单的语法时,表格就出现了:

When I tried a trivial grammar, the table was present:

#if YYDEBUG || YYERROR_VERBOSE || YYTOKEN_TABLE
/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
   First, the terminals, then, starting at YYNTOKENS, nonterminals.  */
static const char *const yytname[] =
{
  "$end", "error", "$undefined", "ABSINTHE", "NESTLING", "$accept",
  "anything", 0
};
#endif

注意:表是静态的;如果您尝试从文件外部访问它,则将不起作用.

Notes: the table is static; if you are trying to access it from outside the file, that will not work.

源中有一个较早的节:

/* Enabling the token table.  */
#ifndef YYTOKEN_TABLE
# define YYTOKEN_TABLE 1
#endif

这可确保已定义令牌表.

This ensures that the token table is defined.

这篇关于无法访问野牛2.6中的令牌表yytname还是消失了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆