Flex 和 Bison,Windows 使用符号 table 时出错

Flex and Bison, Windows Error using symbol table

程序旨在将值存储在符号 table 中,然后能够将它们打印出来,说明词性。在解析器中进一步解析和声明更多,是否是一个句子等等。

我通过

创建了 executable 文件
flex try1.l
bison -dy try1.y
gcc lex.yy.c y.tab.c -o try1.exe

在命令中 (WINDOWS)

当我尝试在 运行 执行 table 时声明任何值时出现我的问题, 动词 运行 它是这样的 粗体是输入

动词运行
运行
运行

语法错误
名词猫

语法错误
运行
运行

语法错误
猫运行
语法错误

我的想法:我不确定为什么我从代码语法错误中得到这个错误。虽然在调试并尝试打印出正在存储的值之后,我认为链接列表必须存在某种问题。因为它似乎只有一个值被存储在链表中并导致某种错误。当我试图为 运行 打印出存储的 word_type 整数值时,它会打印出正确的值 259,但会拒绝让我为我的符号 table 定义任何其他词。我撤消了 print 语句的更改,现在它如前所述工作。我再次认为 addword 方法存在问题,因为它未正确添加,因此查找方法导致程序崩溃。

Lexer文件,本例取自O'Reily 2nd edition on Lex And Yacc, 示例 1-5,1-6。 我正在尝试自己学习 Lex 和 Yacc 并重现此示例。

%{
/*
* We now build a lexical analyzer to be used by a higher-level parser.
*/
#include <stdlib.h>
#include <string.h>
#include "ytab.h" /* token codes from the parser */
#define LOOKUP 0 /* default - not a defined word type. */
int state;
%}
/* 
*  Example from page 9 Word recognizer with a symbol table. PART 2 of Lexer
*/
%%
\n { state = LOOKUP; } /* end of line, return to default state */
    \.\n { state = LOOKUP;
    return 0; /* end of sentence */
    }
        /* whenever a line starts with a reserved part of speech name */
        /* start defining words of that type */
        ^verb { state = VERB; }
        ^adj { state = ADJ; }
        ^adv { state = ADV; }
        ^noun { state = NOUN; }
        ^prep { state = PREP; }
        ^pron { state = PRON; }
        ^conj { state = CONJ; }
        [a-zA-Z]+ {
            if(state != LOOKUP) {
            add_word(state, yytext);
            } else {
                switch(lookup_word(yytext)) {
                case VERB:
                return(VERB);
                case ADJECTIVE:
                return(ADJECTIVE);
                case ADVERB:
                return(ADVERB);
                case NOUN:
                return(NOUN);
                case PREPOSITION:
                return(PREPOSITION);
                case PRONOUN:
                return(PRONOUN);
                case CONJUNCTION:
                return(CONJUNCTION);
                default:
                printf("%s: don't recognize\n", yytext);
                /* don't return, just ignore it */
                    }
                    }
               }
        . ;
%%
int yywrap()
{
return 1;
}
/* define a linked list of words and types */
struct word {
        char *word_name;
        int word_type;
        struct word *next;
};
struct word *word_list; /* first element in word list */
extern void *malloc() ;
int
add_word(int type, char *word)
    {
    struct word *wp;
        if(lookup_word(word) != LOOKUP) {
        printf("!!! warning: word %s already defined \n", word);
        return 0;
        }
    /* word not there, allocate a new entry and link it on the list */
    wp = (struct word *) malloc(sizeof(struct word));
    wp->next = word_list;
    /* have to copy the word itself as well */
    wp->word_name = (char *) malloc(strlen(word)+1);
    strcpy(wp->word_name, word);
    wp->word_type = type;
    word_list = wp;
    return 1; /* it worked */
    }
    int
    lookup_word(char *word)
    {
    struct word *wp = word_list;
    /* search down the list looking for the word */
    for(; wp; wp = wp->next) {
        if(strcmp(wp->word_name, word) == 0)
        return wp->word_type;
    }
return LOOKUP; /* not found */
}

yacc 文件,

%{
/*
* A lexer for the basic grammar to use for recognizing English sentences.
*/
#include <stdio.h>
%}
%token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION
%%
sentence: subject VERB object{ printf("Sentence is valid.\n"); }
;
subject: NOUN
| PRONOUN
;
object: NOUN
;
%%
extern FILE *yyin;
main()
{
do
{
yyparse();
}
while (!feof(yyin));
}
yyerror(s)
char *s;
{
fprintf(stderr, "%s\n", s);
}

头文件,必须为一些不知道为什么的值创建 2 个版本,但代码对它们有问题,我不明白为什么,所以我只创建了一个带有全名和缩写的令牌每本书只有一本。

# define NOUN 257
# define PRON 258
# define VERB 259
# define ADVERB 260
# define ADJECTIVE 261
# define PREPOSITION 262
# define CONJUNCTION 263
# define ADV 260
# define ADJ 261
# define PREP 262
# define CONJ 263
# define PRONOUN 258
  1. 如果你觉得你的链表实现有问题,你最好用一个简单的 driver 程序测试和调试它,而不是尝试这样做使用您仍在学习的一些工具(flex 和 bison)。总的来说,测试越简单,依赖越少,越容易发现问题。有关调试的一些建议,请参阅 this useful essay by Eric Clippert

  2. 我不明白你为什么觉得有必要引入 "short versions" 个令牌 ID。 Levine 书中的示例代码并未在任何地方使用这些符号。您不能只发明符号,也不需要这些缩写。

    您 "had to create 2 versions [of the header file] for some values" 但 "code was having an issue with them, and I wasn't understanding why" 的评论对于答案来说太不具体了。也许问题在于您认为您可以使用未在任何地方定义的标识符,这肯定会导致编译器错误。但如果还有其他问题,您可以提出一个问题,并提供准确的问题描述(即您遇到的问题)和 Minimal, Complete, and Verifiable example(如 Whosebug 帮助页面中所示)。

    在任何情况下,手动设置令牌 ID 的值几乎肯定 会阻止您识别输入。 Bison/yacc 为内部标记保留值 256 和 257,因此将生成的第一个(并因此在解析器中使用)的值为 258。这意味着您从词法扫描器返回的标记值有一个野牛内部的不同含义。底线:从不 手动设置令牌值。如果您的 header 未正确生成,请查明原因。

  3. 据我所知,您的程序唯一合法的输入格式为:

    sentence: subject VERB object
    

    因为 none 的示例输入(例如 "run")具有这种形式,所以语法错误并不奇怪。但是,您在输入 "cat" 上收到一个非常早的语法错误这一事实确实表明您的符号 table 查找可能存在问题。 (这可能是上述问题的结果。)