1
votes

I'm trying to use flex and bison to create a simple calc, but i receive errors when i try compile, i don't experience in YACC or FLEX this is my first program.

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

this is the error:

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#

this is my 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

this is my YACC file:

%{
#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();
}

this is my LEX file:

%{
#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
Your first program in a new language should be HelloWorld. If you try to write two non-trivial routines in two new languages and link them together, you ask for a miracle.Beta
Don't do development work as root; it's far too dangerous!Jonathan Leffler

1 Answers

4
votes

The problem is on your Makefile:

    $(LEX) -P math math.lex 

The '-P' option defines a prefix to be used in place of 'yy', then 'yylex' is being changed to 'mathlex'.

Use $(LEX) --outfile=lex.math.c math.lex instead.