对 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) 的相关文章

  • 需要帮助确定“默认操作类型冲突”的原因

    我一直在做一项学校作业 但很难弄清楚哪个问题导致了 默认操作类型冲突 下面的多个警告 任何帮助将不胜感激 收到的警告 parser y 62 9 23 warning type clash on default action
  • Flex 可重入以用户特定状态启动

    Flex 设置YY STATE to INITIAL默认情况下 当yyscan t叫做 我正在尝试制作一个可重入扫描仪 可以从特定于用户的状态而不是 INITIAL 这是案例 comment start not passed into fl
  • 将 Flex/Bison 与外部程序集成

    我正在开发一个智能代理模型 该模型需要事件列表作为输入 这些事件来自另一个模型的输出 位于 大 文本文件中 文本文件是所有事件的列表 包括我不关心的不必要事件 因此我使用 flex 编写了一个扫描器 可以找到有用的位 智能代理模型的框架已经
  • 在 Bison 中为 && 和 || 启动短路规则运营

    我正在使用 C C 在 Bison 和 Flex 中编写一个简单的计算器 逻辑在 Bison 中完成 C C 部分负责数据结构 例如 STL 等 我有以下问题 在我的计算器中美元符号 表示 i 和 i 前缀和后缀 例如 int y 3 gt
  • 如何在 Visual Studio 2005/2008 中编译 Flex?

    我无法弄清楚这一点 我可以从 gnuwin32 下载 flex 2 5 4a 的 win32 二进制文件 但我想使用 Visual Studio 2005 构建最新版本 2 5 35 我想我可以在 cygwin 中构建 但其中的乐趣在哪里
  • Flex/Bison 错误:请求非结构或联合中的成员“str”

    我正在学习 flex bison 我编写了以下程序但出现错误 include
  • 编写编译器……什么是对的,什么是错的? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • Flex/Lex 和 Yacc/Bison 有什么区别?

    Flex Lex 和 Yacc Bison 之间有什么区别 我在网上疯狂搜索 没有找到任何可靠的答案 我可以在 Ubuntu 上安装纯 Lex 和 Yacc 还是可以只安装 flex 和 bison 我很困惑 Lex 或 Yacc 是否仍然
  • yytext[0]是什么意思?

    yytext 0 是什么意思 为什么我们要在 lex 和 yacc 程序中使用 我是学习者 所以不要介意这是一个愚蠢的问题 yytext 保存与当前标记匹配的文本 因此 yytext 0 保存与当前标记匹配的文本的第一个字符 有时您有一个可
  • GNU Flex 库 libfl 提供什么?

    我可以从 flex 和 bison 生成的文件编译一个程序 cc lex yy c program tab c o output 也由 cc lex yy c program tab c lfl o output 它们都运行顺利 没有任何问
  • vc++下编译bison和flex程序时unistd.h相关困难

    我正在使用 bison flex 通过 cygwin 下载 和 vc 当我编译程序时 出现错误 fatal error C1083 Cannot open include file unistd h No such file or dire
  • Flex 和 Bison 彼此需要什么?

    当 Flex 和 Bison 一起使用时 为什么 Flex 文件需要 includebison 创建的 C 头文件 编译需要 bison 和 flex 创建的 C 源文件 bison 和 flex 创建的 C 源文件相互需要什么 bison
  • LR(k) 到 LR(1) 语法转换

    我对以下内容感到困惑quote http en wikipedia org wiki LR parser Theory来自维基百科 换句话说 如果一种语言足够合理 允许 高效的单遍解析器 可以用 LR k 语法来描述 语法总是可以机械地转化
  • 构建 gcc 4.6 时遇到问题:对“yylex”的未定义引用

    我正在尝试构建 gcc 4 6 但我收到一些链接器错误 看起来这意味着 bison 或 flex 没有链接到 当 makefile 发出此命令时 gcc g fkeep inline functions DIN GCC W Wall Wwr
  • 解析器 - 调用 yytext 时出现分段错误

    我的解析器正在识别语法并使用 yylineno 指示正确的错误行 我想打印导致错误的符号 int yyerror string s extern int yylineno defined and maintained in lex yy c
  • 如何将 C++ flex 与 C++ Bison 连接起来?

    我正在尝试将 C flex 与 C bison 连接起来 但我被难住了 Bison 3 8 1 手册有一个带有 C Flex 的 C Bison 示例 Flex 2 6 4 没有示例 我试图解决的问题是如何向 C 或 C Bison 提供指
  • 无法弄清楚为什么 Bison 抛出“由于冲突,规则在解析器中无用”

    我正在为一种非常简单的编程语言编写 BNF 语法 并使用 Flex 和 Bison 进行编译 我只有 3 种变量和常量类型 实数 整数 字符串 我的 l 文件具有 ID 的标记定义 如下所示 DIGIT 0 9 LETTER a zA Z
  • Flex 和 Bison 是否有 Sublime Text 语法?

    我正在 Sublime Text 中寻找一种语法 以某种方式突出显示我的 Flex 和 Bison 文件 或 lex yacc 使它们可读 Sublime Text 自动为 Flex 文件选择 Lisp 但这并不能解决问题一切都很好 有什么
  • 为什么这些冲突出现在以下 XML 的 yacc 语法中

    我有以下 XML 语法 效果很好 program lt ID attribute list gt root root lt ID attribute list gt node list lt ID gt node list node s n
  • bison/lex YYSTYPE 声明为结构体

    我已经尝试了一段时间 通过使用 bison 和 lex 来实现语法解析器 我对 yylval 的类型重新声明有疑问 我自己解释一下 我有4个文件 lexico l parser y funcionesTabla c funcionesTab

随机推荐