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

6

我正在尝试学习一些flex/bison,并阅读约翰·莱文(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

I've got four source files:

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'。有人知道为什么会出现这个错误以及如何解决吗?先谢谢了 :)

嘿,伙计,你从这个计算器得到正确的答案了吗?它给我奇怪的答案。 - Michael Ghorb
@MohammadGhorbani 抱歉,伙计,我记不起来了!自从2010年以来,我就没有接触过任何与flex-bison或相关的东西,所以现在所有这些对我来说都像胡言乱语! - Tom
2个回答

4
解决了,命令如下:
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

我不确定为什么书中没有在例子中明确说明这一点。

0

我也读了那一章,我认为作者是在暗示应该将代码放入“Makefile”中,以自动化构建所需的文件。

$@ 是在 bash shell 脚本(和其他地方)中使用的变量扩展,可能在 make 中执行相同的操作或者正是 make 实现的功能。


1
不,对于Shell来说,$@表示“此脚本/函数的参数”,而对于Make来说,$@表示“当前目标”,在这里实际上是指fb3 - akim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接