对 yyparse 的未定义引用(flex 和 bison)

2024-03-14

我正在尝试学习一些 Flex/Bison,并且正在阅读 John Levine (O'Reilly) 的 Flex & Bison。有一个我需要运行的示例,但是我无法运行它,因为出现以下错误:

/tmp/ccKZcRYB.o: In function `yylex':
fb3-1.lex.c:(.text+0x2bd): undefined reference to `yylval'
/tmp/cclBqnOk.o: In function `main':
fb3-1funcs.c:(.text+0x420): undefined reference to `yyparse'
collect2: ld returned 1 exit status

我有四个源文件:

fb3-1.h:

/*
 * Declarations for calculator  fb3-1
 */

/* Interface to the lexer */
extern int yylineno; /* from lexer */
void yyerror(char *s, ...);

/* nodes in the abstract syntax tree */
struct ast {
    int nodetype;
    struct ast *l;
    struct ast *r;
};

struct numval {
    int nodetype;   /* type K for constant */
    double number;
};

/* build an AST */
struct ast *newast(int nodetype, struct ast *l, struct ast *r);
struct ast *newnum(double d);

/* evaluate an AST */
double eval(struct ast *);

/* delete and free an AST */
void treefree(struct ast *);

fb3-1.l

/* recognise tokens for the calculator */
%option noyywrap nodefault yylineno
%{
#include "fb3-1.h"
#include "fb3-1.tab.h"
%}

/* float exponent */
EXP     ([Ee][-+]?[0-9]+)

%%

"+" |
"-" |
"*" |
"/" |
"|" |
"(" |
")"     { return yytext[0]; }
[0-9]+"."[0-9]*{EXP}? |
"."?[0-9]+{EXP}? { yylval.d = atof(yytext); return NUMBER; }

\n      { return EOL; }
"//".*
[ \t]   { /* ignore whitespace */ }
.       { yyerror("Mystery character %c\n", *yytext); }
%%

fb3-1.y

/* calculator with AST */

%{
#include <stdio.h>
#include <stdlib.h>
#include "fb3-1.h"
%}

%union {
    struct ast *a;
    double d;
}

/* declare tokens */
%token <d> NUMBER
%token EOL

%type <a> exp factor term

%%
calclist: /* nothing */
| calclist exp EOL {
    printf("=%4.4g\n",eval($2));
    treefree($2);
    printf("> ");
  }

  | calclist EOL { printf("> "); } /* blank line or a comment */
  ;

exp: factor
 | exp '+' factor { $$ = newast('+', $1, $3); }
 | exp '-' factor { $$ = newast('-', $1, $3); }
 ;

factor: term
 | factor '*' term { $$ = newast('*', $1, $3); }
 | factor '/' term { $$ = newast('/', $1, $3); }
 ;

term: NUMBER { $$ = newnum($1); }
 | '|' term { $$ = newast('|', $2, NULL); }
 | '(' term { $$ = $2; }
 | '-' term { $$ = newast('M', $2, NULL); }
 ;

%%

fb3-1funcs.c

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "fb3-1.h"

struct ast * newast(int nodetype, struct ast *l, struct ast *r)
{
    struct ast *a = malloc(sizeof(struct ast));

    if(!a) {
        yyerror("out of space");
        exit(0);
    }

    a->nodetype = nodetype;
    a->l = l;
    a->r = r;
    return a;
}

struct ast * newnum(double d)
{
    struct numval *a = malloc(sizeof(struct numval));

    if(!a) {
        yyerror("out of space");
        exit(0);
    }

    a->nodetype = 'K';
    a->number = d;
    return (struct ast *)a;
}

double eval (struct ast *a)
{
    double v;

    switch(a->nodetype) {
        case 'K': v = ((struct numval *)a)->number; break;

        case '+': v = eval(a->l) + eval(a->r); break;
        case '-': v = eval(a->l) + eval(a->r); break;
        case '*': v = eval(a->l) + eval(a->r); break;
        case '/': v = eval(a->l) + eval(a->r); break;
        case '|': v = eval(a->l); if(v < 0) v = -v; break;
        case 'M': v = -eval(a->l); break;
        default: printf("internal error: bad node %c\n", a->nodetype);
    }
}

void treefree(struct ast *a)
{
    switch(a->nodetype)
    {
        /* two subtrees */
        case '+':
        case '-':
        case '*':
        case '/':
            treefree(a->r);

        /* one subtree */
        case '|':
        case 'M':
            treefree(a->l);

        /* no subtree */
        case 'K':
            free(a);
            break;

        default: printf("internal error: free bad node %c\n", a->nodetype);
    }
}

void yyerror(char *s, ...)
{
    va_list ap;
    va_start(ap, s);

    fprintf(stderr, "%d: error: ", yylineno);
    vfprintf(stderr, s, ap);
    fprintf(stderr, "\n");
}

int main ()
{
    printf("> ");
    return yyparse();
}

构建:

bison -d fb3-1.y
flex -ofb3-1.lex.c fb3-1.l
cc -o $@ fb3-1.tab.c fb3-1.lex.c fb3-1funcs.c

我正在运行 Ubuntu 10.04 x64,安装了软件包“flex”和“bison”。任何人都知道为什么会发生此错误以及如何修复它?提前致谢 :)


解决了,命令

cc -o $@ fb3-1.tab.c fb3-1.lex.c fb3-1funcs.c

应该

cc -o fb3 fb3-1.tab.c fb3-1.lex.c fb3-1funcs.c

不知道为什么这本书没有为示例指定这一点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对 yyparse 的未定义引用(flex 和 bison) 的相关文章

  • 与包含 Flex 和 Bison 的项目链接 LLVM 时遇到问题

    我一直在学习使用 Flex Bison 和 LLVM 编写编译器的教程 http gnuu org 2009 09 18 writing your own toy compiler 并尝试编译最终的二进制文件失败 并出现许多以下 未定义的引
  • Flex 换行扫描野牛

    我想使用相同的 flex bison 扫描仪 解析器作为解释器并加载要解释的文件 在这两种情况下我都无法让换行符解析正常工作 解释器 有一个提示 我可以输入按 ENTER 终止的命令 文件 这是一个示例输入文件 切 begin print
  • Bison 减少/减少与强制转换和表达式括号的冲突

    我正在野牛中构建语法 并且我已将最后一个减少 减少错误的范围缩小到以下测试用例 include
  • yylloc 在此范围内未定义

    我在编译文件时遇到以下问题 我已经重写了 YYLTYPE 的定义 如下所示 虽然它与默认值相同 但我将扩展它 typedef struct YYLTYPE int first line int first column int last l
  • Lex 正则表达式获得一些额外的字符

    我的 lex 文件中有以下定义 L a zA Z A a zA Z 0 9 L A yylval id yytext return IDENTIFIER 我在 YACC 文件中执行以下操作 primary expression IDENTI
  • 如何修复编译时 -lfl 缺失的 ld 库?

    我正在尝试翻译我的 spl文件转换成C文件 因为没有编译器 我有一个示例 Hello World spl 文件 并且我已经下载了莎士比亚编程语言 http shakespearelang sourceforge net report sha
  • 需要帮助确定“默认操作类型冲突”的原因

    我一直在做一项学校作业 但很难弄清楚哪个问题导致了 默认操作类型冲突 下面的多个警告 任何帮助将不胜感激 收到的警告 parser y 62 9 23 warning type clash on default action
  • 将 Flex/Bison 与外部程序集成

    我正在开发一个智能代理模型 该模型需要事件列表作为输入 这些事件来自另一个模型的输出 位于 大 文本文件中 文本文件是所有事件的列表 包括我不关心的不必要事件 因此我使用 flex 编写了一个扫描器 可以找到有用的位 智能代理模型的框架已经
  • Flex/bison,错误:未声明

    你好 我有一个问题 下面的程序返回一个错误 error Undeclared first use in function 为什么会出现这个错误所有令牌都被声明了 但是这个错误来了 任何人都可以帮助我 这里是 lex 和 yac 文件 谢谢
  • 如何在 Visual Studio 2005/2008 中编译 Flex?

    我无法弄清楚这一点 我可以从 gnuwin32 下载 flex 2 5 4a 的 win32 二进制文件 但我想使用 Visual Studio 2005 构建最新版本 2 5 35 我想我可以在 cygwin 中构建 但其中的乐趣在哪里
  • 我的 Flex 文件输出错误

    我编写了一个 l 文件并希望输出 c17 isc 中的内容 但有一个错误我不知道为什么 我已经给出了我打算读取的文件 flex文件和执行结果 这是 c17 isc 文件 内容的意思是 number gate name gate type o
  • 来自 bison 的 ANTLR 语法

    我正在尝试将语法从 bison 翻译为 ANTLR 野牛的语法本身非常简单 但我找不到简单的方法来做到这一点 野牛语法 expr expr or expr expr and expr expr 欢迎任何提示 链接 指针 谢谢 尤利安 在AN
  • Lex VHDL '(勾号)令牌

    在 VHDL 中 字符可用于封装字符标记ie 或者它可以作为属性分隔符 类似于 CPP 的 token ie string hello 解析包含字符的属性名称时出现问题ie string a b c 在这种情况下 天真的词法分析器将错误地标
  • Flex/Lex 和 Yacc/Bison 有什么区别?

    Flex Lex 和 Yacc Bison 之间有什么区别 我在网上疯狂搜索 没有找到任何可靠的答案 我可以在 Ubuntu 上安装纯 Lex 和 Yacc 还是可以只安装 flex 和 bison 我很困惑 Lex 或 Yacc 是否仍然
  • 使用 Flex 和 Bison 的简单 Java 语法

    我最近开始学习基本的 Flex 和 Bison 因为我必须为简单 但不是太简单 语法制作一个解析器 我决定在我的语法中制作一种简化的 Java 语言 我做了 l和 y文件和所有内容都编译没有错误 我正在使用 gcc 进行编译 问题是每次我运
  • flex 中 yywrap() 的含义

    该指令在 flex lex 中意味着什么 define yywrap 1 和这个 t 我在下面的代码中找到它 t putchar t 输入 你好世界 输出 你好世界 根据Lex 和 Yacc 页面 http dinosaur compile
  • vc++下编译bison和flex程序时unistd.h相关困难

    我正在使用 bison flex 通过 cygwin 下载 和 vc 当我编译程序时 出现错误 fatal error C1083 Cannot open include file unistd h No such file or dire
  • 如何解决2+2和2++2冲突

    在更大的程序中 我给出了以下内容 flex bison In flex pn dig 0 9 exp e E dig printf detected n return PLUS SIGN pn dig printf digit detect
  • 当我使用 yymore() 时,在 EOF 处出现 Flex 错误“缓冲区末尾丢失”

    我正在编写一个 Flex 程序来处理字符串常量 当输入文件在字符串中遇到 EOF 时 我想返回一个 ERROR 标记 文件遇到 EOF 并打印 ERROR 后出现以下错误 致命的 Flex 扫描仪内部错误 缓冲区末尾丢失 这是我的代码 可以
  • 无法在 Windows 上安装 Flex(词法分析器),无法找到全面的说明

    这学期我在大学上一门关于 Flex Bison 的课程 在尝试让 Flex 工作时遇到了严重的问题 可能是由于我自己的无能 但我正在寻找一个非常非常难以研究的解决方案 我正在尝试使用命令提示符导航到包含 l Lex 文件的文件 然后使用 F

随机推荐