c - 我在函数 `yylex' : lex. yy.c :(. text+0x2ac) 中遇到错误: undefined reference

标签 c compiler-construction flex-lexer yacc lex

我是 lex 和 yacc 的新手,我正在关注 "lex & yacc 1992" book .

我正在处理第3章的示例,在编译过程中出现错误,但我找不到解决方案; 这是代码:

lex 文件.l:

%{

#include "y.tab.h"
#include "symboletable.h"
#include <math.h>
extern int yylavl;

%}


%%
([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {
            yylval.dval = atof(yytext);
            return NUMBER;
        }
[ \t] ;       /* ignore whitespace */ 

[A-Za-z][A-Za-z0-9]* { /* return symbol pointer */
            yylval.symp = symlook(yytext);
            return NAME;
        }
"$" { return 0; }
\n |  
. return yytext[0];

%%

这里是 yacc 文件.y

%{
#include "symboletable.h"
#include <string.h>
#include <stdio.h>     /* C declarations used in actions */
int yylex();
void yyerror(const char *s);
%}

%union {
    double dval;
    struct symtab *symp;
}

%token <symp> NAME
%token <dval> NUMBER

%left '+' '-'
%left '*' '/'
%nonassoc UMINUS

%type <dval> expression
%%

statement_list : statement '\n'
               | statement_list statement '\n'
               ;

statement  : expression   { printf("= %g\n", $1); } 
           | NAME '=' expression {$1->value = $3; }
           ;


expression : NAME {$$ = $1->value; }
           | expression '+' expression {$$ = $1 + $3; }
           | expression '-' expression {$$ = $1 - $3; }
           | expression '*' expression {$$ = $1 * $3; }
           | expression '/' expression
                {   if ($3 ==0.0)
                        yyerror("divide by zero");
                    else
                        $$ = $1 / $3; 
                }
           | '-' expression %prec UMINUS {$$ = -$2; }
           | '(' expression ')' {$$ = $2; }
           | NUMBER
           ;
%%

根据书上的例子,我需要编写一个符号表例程,来获取字符串并为字符串分配动态空间,这里是file.h

符号表.h

#define NSYMS 20 /* maximum number of symbols */
struct symtab {
        char *name;
        double value;
} symtab[NSYMS];

struct symtab *symlook();

和符号表.pgm:

/* look up a symbol table entry, add if not present */
struct symtab *
symlook(s)
char *s;
{
    char *p;
    struct symtab *sp;
    for (sp = symtab; sp < &symtab[NSYMS]; sp++){
        /* is it already here ? */
        if (sp->name && !strcmp(sp->name, s))
            return sp;

        /* is it free */
        if (!sp->name){
            sp->name = strdup(s);
            return sp;
        }
        /* otherwise continue to next */
    }
    yyerror("Too many symbols");
    exit(1);    /* cannot continue */
} /* symlook */

现在当我运行以下命令时:

yacc -d file.y 
lex file.l 
cc -c lex.yy.c -o newfile -ll
cc -o new y.tab.c lex.yy.c -ly -ll

但是我得到的错误是:

/tmp/ccGnPAO2.o: In function yylex': lex.yy.c:(.text+0x2ac): undefined reference tosymlook' collect2: error: ld returned 1 exit status

那么,为什么我会收到这个错误,我完全遵循这个例子?

最佳答案

您需要在编译命令中包含符号表实现。否则,链接器将如何找到该代码?

关于c - 我在函数 `yylex' : lex. yy.c :(. text+0x2ac) 中遇到错误: undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49507668/

相关文章:

在C编程中将 float 转换为整数

.net - 有哪些方法可以从非托管代码生成 MSIL

c++ - Bison : Line number included in the error messages

c - 如何避免在 pthreads 中进行轮询

c - C中带指针的冒泡排序单链表

c - 编写编译器时,如何检查标记?

compiler-construction - 如何链接两个nasm源文件

c - flex中的正则表达式有错误

c - flex 无法识别字符范围

级联预处理 :