Bison Mac上的链接器错误Yacc
当我试图编译时,我遇到了这个错误。我以前编译过lex文件,没有任何问题,我似乎无法让yacc无误地编译Bison Mac上的链接器错误Yacc,bison,yacc,flex-lexer,lex,Bison,Yacc,Flex Lexer,Lex,当我试图编译时,我遇到了这个错误。我以前编译过lex文件,没有任何问题,我似乎无法让yacc无误地编译 :~ yacc project-5.y :~ lex project.l :~g++ -o $dragon project-5.tab.c lex.yy.c clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated Undefined symbols for arc
:~ yacc project-5.y
:~ lex project.l
:~g++ -o $dragon project-5.tab.c lex.yy.c
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
当使用-c标志编译时。我不明白为什么它似乎编译,但有错误
test-5.tab.c:1246:16: warning: implicit declaration of function 'yylex' is invalid in C99 [-Wimplicit-function-declaration]
yychar = YYLEX;
^
test-5.tab.c:601:15: note: expanded from macro 'YYLEX'
#define YYLEX yylex ()
^
test-5.tab.c:1374:7: warning: implicit declaration of function 'yyerror' is invalid in C99 [- Wimplicit-function-declaration]
yyerror (YY_("syntax error"));
^
test-5.y:35:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main(int argc, char **argv)
^~~~
test-5.y:40:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
yyerror(char *s)
^~~~~~~
test-5.y:42:1: warning: control reaches end of non-void function [-Wreturn-type]
}
法律法规
%option noyywrap
%{
# include "project.tab.h"
%}
Delimiter [ \t\n]
WhiteSpace {Delimiter}+
Letter [A-za-z]
Digit [0-9]
E_Notation E("+"|"-")?({Digit})+
Var ("_"|{Letter})(({Letter}|{Digit})+("_")?)*
UnsignedInteger {Digit}*
UnsignedFloatingPoint (({Digit})+(".")({Digit})*({E_Notation})?|({Digit}*(".")?({Digit})+ {E_Notation}?))
factor {UnsignedInteger}|{UnsignedFloatingPoint}|{Var}
term {factor}(("*"|"/"|"%"){factor})*
expression {term}(("+"|"-"){term})*
Op ("+"|"-")
Op2 ("*"|"/"|"%")
RelOp ("<"|"<="|"=<"|"=="|"!="|">"|">="|"=>"|".LT."|".LE."|".EL."|".EQ."|".NE."|".GT."|".GE."|".EG.")
LogOp ("!"|"||"|"&&"|".NOT."|".OR."|".AND.")
LogCons (".TRUE."|".FALSE.")
function {Var}+"("{factor}*")"
Punct (";"|"("|")")
UnaryOp ("+"|"-")|[^{factor}]
%%
{Var} {return Var;}
{UnsignedInteger} {return UnsignedInteger;}
{UnsignedFloatingPoint} {return UnsignedFloatingPoint;}
{Op} {return Op;}
{RelOp} {return RelOp;}
{LogOp} {return LogOp;}
{LogCons} {return LogCons;}
{Punct} {return Punct;}
{function} {return Function;}
\n {return EOL;}
%%
%选项noyywrap
%{
#包括“project.tab.h”
%}
分隔符[\t\n]
空白{分隔符}+
字母[A-za-z]
数字[0-9]
E_符号E(“+”|“-”)({Digit})+
Var(“{Letter}”)(({Letter}{digital})+(“{”)?)*
无符号整数{位}*
无符号浮点数(({Digit})+(“)({Digit})*({E_表示法})?|({Digit}*(“)?({Digit})+{E_表示法})
因子{UnsignedInteger}{UnsignedFloatingPoint}{Var}
术语{factor}((“*”|“/“|”%”{factor})*
表达式{term}((“+”|“-”{term})*
Op(“+”|“-”)
Op2(“*”|“/”|“%”)
RelOp(“|”.LT.“|”.LE.|”.EL.|“.EQ.|”.NE.|“.GT.|”.GE.|“.EG.”)
LogOp(“!”|“|”|“&&&&“|”。不是“|”或“|”和“)
LogCons(“.TRUE.”|“.FALSE.”)
函数{Var}+”(“{factor}*”)
点号(“;”|“(“|”))
一元运算(“+”|“-”|[^{factor}]
%%
{Var}{return Var;}
{UnsignedInteger}{返回UnsignedInteger;}
{UnsignedFloatingPoint}{返回UnsignedFloatingPoint;}
{Op}{return Op;}
{RelOp}{return RelOp;}
{LogOp}{return LogOp;}
{LogCons}{返回LogCons;}
{Punct}{return Punct;}
{function}{return function;}
\n{return EOL;}
%%
YACC代码
/* simplest version of calculator */
%{
#include <stdlib.h>
#include <stdio.h>
%}
/* declare tokens */
%token Var
%token UnsignedInteger
%token UnsignedFloatingPoint
%token Op
%token Op2
%token RelOp
%token LogOp
%token LogCons
%token Punct
%token Function
%token EOL
%%
stmt: /* empty */
|stmt exp EOL {printf("Ok\n");}
|stmt error EOL {printf("Error\n");}
exp: term
| term Op exp
;
factor: Var
| UnsignedInteger
| UnsignedFloatingPoint
;
term: factor
| factor Op2 term
;
%%
int main(int argc, char **argv)
{
yyparse();
return 0;
}
int yyerror(char *s)
{
return 0;
}
/*计算器的最简单版本*/
%{
#包括
#包括
%}
/*声明代币*/
%令牌变量
%令牌无符号整数
%令牌无符号浮点
%令牌Op
%令牌Op2
%令牌重发
%令牌标识
%令牌日志
%令牌点
%令牌函数
%代币下线
%%
stmt:/*空*/
|stmt exp EOL{printf(“Ok\n”);}
|stmt错误EOL{printf(“错误\n”);}
出口:期限
|术语Op exp
;
因子:Var
|无符号整数
|无符号浮点
;
术语:因子
|因子Op2项
;
%%
int main(int argc,字符**argv)
{
yyparse();
返回0;
}
int yyerror(字符*s)
{
返回0;
}
您似乎包括了yacc输出的不同版本,这可能会给您带来编译错误
您将yacc project-5.y
的输出放在编译行中:
g++ -o $dragon project-5.tab.c lex.yy.c
但是在project.l
中,您有以下内容:
%{
# include "project.tab.h"
%}
这可能是早期
project.y
文件中yacc的输出,该文件可能已更改,从而导致代码版本之间的某些不兼容….您似乎包含了yacc输出的不同版本,这可能会导致编译错误
您将yacc project-5.y
的输出放在编译行中:
g++ -o $dragon project-5.tab.c lex.yy.c
但是在project.l
中,您有以下内容:
%{
# include "project.tab.h"
%}
这可能是早期代码<项目> Y/C >文件的输出,可能是已经更改的,因此导致代码版本之间的一些不兼容。
< P> OSX上的BISY的默认版本是一个相当古老的V2.3,它生成C89代码(与C99或C++不兼容,因为您得到的错误/警告). 您可以使用-std=c89
进行编译,也可以通过向.y文件的第一部分添加前向声明使其更兼容:
%{
int yylex();
int yyerror(char *);
%}
这将修复关于未定义函数的C99警告/C++错误,但会留下关于滥用字符串文本的警告。如果您还修复了Brian Tompsett标识的文件名不匹配以及.l文件中
UnsignedFloatingPoint
定义中的(可能由剪切粘贴引起的)键入空格,您应该会得到一个可运行的可执行文件。OSX上bison的默认版本是一个相当古老的v2.3,它生成C89代码(与C99或C++不兼容,因为您得到的错误/警告)。可以用<代码> -STD= C89<代码>编译,或者可以通过向.y文件的第一部分添加前声明:使其更兼容:
%{
int yylex();
int yyerror(char *);
%}
这将修复关于未定义函数的C99警告/C++错误,但会留下关于滥用字符串文本的警告。如果您还修复了Brian Tompsett识别的文件名不匹配和(可能导致剪切粘贴)“.Cux.UnSigNeDeloLooPotoStuto/<代码>定义中的类型空间,你应该得到一个可运行的可执行文件。让我们继续跟踪这些错误信息。从第一个开始:为什么你使用代码> G++<代码>来编译不是C++的东西?我实际上是在测试G++,但是我使用GCC并得到了同样的错误。让我们继续下去。这些错误消息。从第一个开始:为什么使用<代码> g++<代码>编译不是C++的东西?我实际上是在测试g++,但是我使用了GCC并得到了同样的错误。