1
votes

I got the following flex and bison code which I want to compile and run:

unari.lex:

%{
    #include "unari.tab.h"
    using namespace std;
%}
%option noyywrap

%%

a        {yylval=1;  return TOK_A;}
\n       return '\n';
\+       return '+';
.        /*ignore all rest*/

%%

unari.y:

%{
    #include <iostream>
    using namespace std;
    void yyerror(const char *errorinfo);
    int yylex();
%}

%left TOK_A 
%left '+'

%%
line: exp '\n'       {cout<<$1<<endl; return 0;}
      ;
exp: exp exp     {$$=$1+$2;}
      | exp '+' exp    {$$=$1+$3;}
      | TOK_A         {$$=yylval;}
      ;
%%
void yyerror(const char *errorinfo)  { 
      cout<<"problem"<<endl;
}


int main()  {
    while(yyparse()==0);
    return 0;
}

makefile:

calc: lex.yy.o unari.tab.o
    g++ unari.tab.o lex.yy.o -o calc.exe

unari.tab.o: unari.tab.c
    g++ -c unari.tab.c

lex.yy.o: lex.yy.c
    g++ -c lex.yy.c

lex.yy.c: unari.lex unari.tab.h
    flex unari.lex

unari.tab.c unari.tab.h: unari.y
    bison -d unari.y

clean:
    rm *.h *.c *.o  *.exe

The problem is, I get the following error when compiling on windows:

makefile1:: *** multiple target patterns. Stop.

Does anyone recognizes the problem? I'm breaking my head over this for more than 3 hours already, tried searching the web and found nothing useful....

2
The Flex tag is for the ADobe/Apache UI Framework. The Flex-lexer tag is used for the lexical analyzer.JeffryHouser
Are you sure this is your exact makefile?Michael Krelin - hacker
This may be your issue: 'unari.tab.c unari.tab.h: unari.y'. Usually there is only one word left of the ':'Charlie Burns
No, that should be fine…Michael Krelin - hacker
What's makefile1? Or did you type the error message incorrectly? Normally it would start makefile:[linenumber]: where [linenumber] is the number of the erroneous line (which would be useful to know). multiple target patterns is only produced (as far as I know) if you have a static rule with more than one target pattern. Static rules have two :'s, and I don't see any such rule in your makefile.rici

2 Answers

2
votes

Instead of

unari.tab.c unari.tab.h: unari.y
    bison -d unari.y

try

unari.tab.h: unari.y
    bison -d unari.y

unari.tab.c: unari.y
    bison unari.y

There may be other ways to do it, but I'm pretty sure that will work for you.


Odd. I copied your files, and once I got all the spaces/tab issues worked out in the Makefile it seem to work fine.

[Charlies-MacBook-Pro:~/junk] crb% make clean
rm *.h *.c *.o  *.exe
[Charlies-MacBook-Pro:~/junk] crb% make
bison -d unari.y
unari.y: conflicts: 2 shift/reduce
flex unari.lex
g++ -c lex.yy.c
g++ -c unari.tab.c
g++ unari.tab.o lex.yy.o -o calc.exe


[Charlies-MacBook-Pro:~/junk] crb% which make
/usr/bin/make
[Charlies-MacBook-Pro:~/junk] crb% make --version
GNU Make 3.81

Could be issues with make on windows, and I don't have a windows machine. Maybe try googling 'multiple target patterns. Stop.'

0
votes

Hard to see here (or even in your editor by default) but make is very strict in regard to having tabs before comments on the following line.

foo.c foo.h: foo.lex
<tab>command

where <tab> represents the tab character. If you have spaces, then it is likely to fail.

P.S. the number of targets to the left of the colon is valid. It means that the command(s) generate all those targets at once.

http://amake.m2osw.com/amake-rules.html