编译flex / bison程序错误

时间:2013-11-12 23:54:51

标签: c++ c bison flex-lexer

我得到了以下我要编译和运行的flex和bison代码:

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;
}

生成文件:

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

问题是,在Windows上编译时出现以下错误:

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

有人认出这个问题吗?我已经超过3个小时了解这个问题,尝试在网上搜索并发现没什么用处......

2 个答案:

答案 0 :(得分:1)

而不是

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

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

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

可能有其他方法可以做到,但我很确定这对你有用。


奇。我复制了你的文件,一旦我得到Makefile中的所有空格/标签问题,它似乎工作正常。

[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

可能是Windows上的make问题,我没有Windows机器。也许尝试谷歌搜索'多目标模式。停止'。

答案 1 :(得分:0)

很难在这里看到(或者甚至在默认情况下在你的编辑器中),但是对于在下一行的评论之前使用标签,make非常严格。

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

其中&lt; tab&gt;表示制表符。如果你有空格,那么很可能会失败。

P.S。冒号左侧的目标数量是有效的。这意味着命令一次生成所有这些目标。

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

相关问题