YACC& Lex未定义对`yylex'的引用

时间:2012-12-26 02:10:13

标签: c reference makefile yacc lex

我正在尝试使用flex和bison来创建一个简单的计算,但是当我尝试编译时我收到错误,我没有在YACC或FLEX中遇到这是我的第一个程序。

flex 2.5.35,bison(GNU bison)2.3,gcc(Debian 4.3.2-1.1)4.3.2,linux 2.6.26-1-686

这是错误:

bison -d -y math.yacc -b math
lex -P math math.lex
gcc lex.math.c math.tab.c -g -Wall -lfl -ll -o math.parse.so
math.tab.c: In function ‘yyparse’:
math.tab.c:1244: warning: implicit declaration of function ‘yylex’
/tmp/ccOhwzV0.o: In function `yyparse':
/root/.netbeans/remote/192.168.56.101/silenobrito-nb-Windows-x86_64/D/source/lib-        math/src/model/math.tab.c:1244: undefined reference to `yylex'
collect2: ld returned 1 exit status
make: ** [math.parse.so] Erro 1
athenas:~/.netbeans/remote/192.168.56.101/silenobrito-nb-Windows-x86_64/D/source/lib-math/src/model#

这是我的Makefile:

## -*- Makefile -*-
##
## Usuário: silenobrito
## Hora: 25/12/2012 17:21:13
## Makefile created by Oracle Solaris Studio.
##
## Este arquivo é gerado automaticamente.
##
CC=gcc
CFLAGS=-g -Wall -lfl -ll
LEX=lex
#YACC=yacc -d
#LEX=flex
YACC=bison -d -y

all: lex.math.c math.tab.c math.parse.so 

## Destino: lex.math.c
lex.math.c: math.tab.c
    $(LEX) -P math math.lex 

## Destino: math.tab.c
math.tab.c: math.yacc  math.lex
    $(YACC) math.yacc -b math

## Destino: math.parse.o
math.parse.so: math.tab.c math.tab.h lex.math.c
    $(CC) lex.math.c math.tab.c $(CFLAGS) -o $@

#### Limpar o destino deleta todos os arquivos gerados ####
clean:
    rm -fr math.tab.c math.tab.h lex.math.c 

# Ativar verificação de dependências
.KEEP_STATE:
.KEEP_STATE_FILE:.make.state.GNU-amd64-Linux

这是我的YACC文件:

%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define YYSTYPE double

#ifdef  __cplusplus
extern "C" {
#endif
    int yyerror(char *message);
#ifdef  __cplusplus
}
#endif


%}
%start solve
%token END_OF_LINE ADD SUB OPEN_PARENTHESIS CLOSE_PARENTHESIS NUMBER
%left ADD SUB
%%

solve   :   expr END_OF_LINE
        {
            printf("resposta: %lf\n", $1);
            return EXIT_SUCCESS;
        }
expr    :   expr ADD expr
        {
            $$ = $1 + $3;
        }|  expr SUB expr
        {
            $$ = $1 - $3;
        }|  OPEN_PARENTHESIS expr CLOSE_PARENTHESIS
        {
            $$ = $2;
        }|  NUMBER;
%%

int yyerror(char *message) {
    fprintf(stderr, "%s\n", message);
    return EXIT_FAILURE;
}

int main(int argc, char ** argv) {
    return yyparse();
}

这是我的LEX文件:

%{
#include <stdio.h>
#include <stdlib.h>
#include "math.tab.h"
#define _POSIX_SOURCE 1

int decValue;
%}

%option nounput
%option noinput
%option noyywrap

%%
add                 return ADD;
sub                 return SUB;
[0-9]+              {yylval = atof(yytext); return NUMBER;};
0x[0-9a-fA-F]+      {sscanf(yytext, "%x", &decValue); yylval = decValue; return NUMBER;};
\(                  return OPEN_PARENTHESIS;
\)                  return CLOSE_PARENTHESIS;
\n                  return END_OF_LINE;
.                   ;
%%

1 个答案:

答案 0 :(得分:3)

问题在于你的Makefile:

    $(LEX) -P math math.lex 

'-P'选项定义了一个前缀用于代替'yy',然后'yylex'被改为'mathlex'。

改为使用$(LEX) --outfile=lex.math.c math.lex