【问题标题】:Flex and Bison, Windows Error using symbol tableFlex 和 Bison,使用符号表的 Windows 错误
【发布时间】:2019-07-13 21:33:12
【问题描述】:

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

我通过

创建可执行文件
flex try1.l
bison -dy try1.y
gcc lex.yy.c y.tab.c -o try1.exe

在 cmd (WINDOWS) 中

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

动词运行
运行
运行

语法错误
名词猫

语法错误
运行
运行

语法错误
cat run
语法错误

我的想法:我不确定为什么我会从代码语法错误中得到这个错误。尽管在调试并尝试打印出存储的值之后,我认为链表肯定存在某种问题。因为似乎只有一个值存储在链表中并导致各种错误。当我试图打印出存储的 word_type 整数值以进行运行时,它会打印出正确的值 259,但会拒绝让我在符号表中定义任何其他单词。我颠倒了 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

【问题讨论】:

    标签: c windows debugging bison flex-lexer


    【解决方案1】:
    1. 如果你觉得你的链表实现有问题,你最好用一个简单的驱动程序测试和调试它,而不是尝试用一些工具(flex 和 bison ) 你还在学习。总的来说,测试越简单,依赖越少,就越容易找到问题。有关调试的一些建议,请参阅this useful essay by Eric Clippert

    2. 我不明白您为什么觉得需要引入令牌 ID 的“短版本”。 Levine 书中的示例代码在任何地方都没有使用这些符号。你不能只是发明符号,你不需要这些缩写来代表任何东西。

      您“必须为某些值创建 2 个版本 [头文件]”但“代码与它们有问题,我不明白为什么”的评论对于答案来说太不具体了.也许问题是您认为您可以使用未在任何地方定义的标识符,这肯定会导致编译器错误。但如果还有其他问题,您可以提出一个准确的问题描述(即您遇到的确切问题)和Minimal, Complete, and Verifiable example(如 StackOverflow 帮助页面中所示)的问题。

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

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

      sentence: subject VERB object
      

      由于您的示例输入(例如“run”)都没有这种形式,因此出现语法错误也就不足为奇了。但是,您在输入“cat”上收到一个非常早的语法错误这一事实确实表明您的符号表查找可能存在问题。 (这可能是上述问题的结果。)

    【讨论】:

    • 谢谢,我将尝试在 Dev C 中调试链表的代码,因为我主要使用 Java 并没有真正使用 C 语言。所以调试它对我来说是相当新的。我只是想知道是否有明显的错误。除了您的第二点之外,我还必须添加它们,因为代码无法编译,因为我收到一个错误,即标头中的新指定值未声明/初始化。对于第三点,我尝试输入唯一可能的工作输出,名词 dog home 和动词 run。然后我进入dog run home,我得到了syntax error syntax error的输出。
    • 我只是更进一步并在 Dev-C 中检查了添加词和查找词功能是否正常工作。由于我的假设不正确,这让我更加困惑。
    • 当有现成的 Flex 和 Bison 时,我才意识到阅读 Lex 和 Yacc 书时的谬误。我试图让它发挥作用,但那非常愚蠢,我没有意识到 O'Reily 也有一本关于这些语言的书。将从那里重新开始,并从那里开始,这些示例很可能会消除我遇到的任何错误。
    • @joey:我又快速查看了您的代码,并意识到您的手写标题不可能工作。抛弃它。找出你在构建过程中做错了什么,并正确地构建你的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多