Flex 和 Bison GCC 编译错误,ld: library not found for -lfl
Flex and Bison GCC compile error, ld: library not found for -lfl
在为简单扫描仪编译 flex 和 bison 源代码时,我遇到以下错误:
$ gcc -o lab5 lab5.tab.c lex.yy.c -lfl
ld: library not found for -lfl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
lex 源代码:
%{
#include <stdio.h>
#include <string.h>
#include "lab5.tab.h"
void showError();
%}
integer (\+?[1-9][0-9]*)
id ([a-zA-Z_][a-zA-Z0-9_]*)
%%
{id} {sscanf(yytext, "%s", yylval.name); return ID;}
{integer} {yylval.number = atoi(yytext); return INT;}
";" return SEMICOLON;
. {showError(); return OTHER;}
%%
void showError() {
printf("Other input");
}
野牛源码:
%{
#include <stdio.h>
int yylex();
int yyerror(char *s);
%}
%token ID INT SEMICOLON OTHER
%type <name> ID
%type <number> INT
%union {
char name[20];
int number;
}
%%
prog:
stmts
;
stmts:
| stmt SEMICOLON stmts
stmt:
ID {
printf("Your entered an id - %s", );
}
| INT {
printf("The integer value you entered is - %d", );
}
| OTHER
;
%%
int yyerror(char *s) {
printf("Syntax error on line: %s\n", s);
return 0;
}
int main() {
yyparse();
return 0;
}
运行以下命令获取所有源文件:
flex -l lab5.l
bison -dv lab5.y
我正在使用 macOS Mojave 10.14.6:
- 野牛(GNU 野牛)2.3
- flex 2.5.35 Apple(flex-31)
- 海湾合作委员会 4.2.1
在 OS X 上,默认的 flex 安装会安装一个名为 l
而不是 fl
的库,这使得它与原始的 "lex" 兼容(但不兼容任何其他模糊的 flex 现代安装)。
所以您可以使用 -ll
而不是 -lfl
但不要那样做。您只需要那个库,因为您没有告诉 flex 不需要 yywrap
的存在。将以下内容添加到 flex 文件的开头:
%option noinput nounput noyywrap nodefault
这些选项中的前两个通过删除它们的定义来抑制关于 input
和 unput
未被使用的 Clang/GCC 警告。 (如果你真的使用它们,你当然希望它们被定义。但你没有。)
第三个选项 noyywrap
告诉 flex 在扫描程序在其输入中遇到文件结束指示时抑制对 yywrap
的调用。您可以添加 yywrap
的定义,无条件地确认文件结束,但这是没有意义的;如果您不打算使用扫描仪的该功能,那么在对虚拟 yywrap
.
的调用中进行编译是没有意义的
第四个选项告诉 flex 如果有一些输入模式与您的规则匹配 none 则抱怨。默认情况下,flex 扫描仪通过在标准输出(或 yyout
指向的任何内容)上打印不匹配的字符来响应不匹配的输入,而不进行任何其他错误处理。这很少是你想要的,所以很高兴知道有一些输入错误会被默默地忽略,这样你就可以修复它。不幸的是,flex 没有告诉您是什么模式触发了错误,所以我会告诉您答案:.
不匹配换行符,而且您的 flex 文件中的任何其他内容都不会。你应该解决这个问题。 (.|\n
是 "anything else" 的模式)。
在为简单扫描仪编译 flex 和 bison 源代码时,我遇到以下错误:
$ gcc -o lab5 lab5.tab.c lex.yy.c -lfl
ld: library not found for -lfl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
lex 源代码:
%{
#include <stdio.h>
#include <string.h>
#include "lab5.tab.h"
void showError();
%}
integer (\+?[1-9][0-9]*)
id ([a-zA-Z_][a-zA-Z0-9_]*)
%%
{id} {sscanf(yytext, "%s", yylval.name); return ID;}
{integer} {yylval.number = atoi(yytext); return INT;}
";" return SEMICOLON;
. {showError(); return OTHER;}
%%
void showError() {
printf("Other input");
}
野牛源码:
%{
#include <stdio.h>
int yylex();
int yyerror(char *s);
%}
%token ID INT SEMICOLON OTHER
%type <name> ID
%type <number> INT
%union {
char name[20];
int number;
}
%%
prog:
stmts
;
stmts:
| stmt SEMICOLON stmts
stmt:
ID {
printf("Your entered an id - %s", );
}
| INT {
printf("The integer value you entered is - %d", );
}
| OTHER
;
%%
int yyerror(char *s) {
printf("Syntax error on line: %s\n", s);
return 0;
}
int main() {
yyparse();
return 0;
}
运行以下命令获取所有源文件:
flex -l lab5.l
bison -dv lab5.y
我正在使用 macOS Mojave 10.14.6:
- 野牛(GNU 野牛)2.3
- flex 2.5.35 Apple(flex-31)
- 海湾合作委员会 4.2.1
在 OS X 上,默认的 flex 安装会安装一个名为 l
而不是 fl
的库,这使得它与原始的 "lex" 兼容(但不兼容任何其他模糊的 flex 现代安装)。
所以您可以使用 -ll
而不是 -lfl
但不要那样做。您只需要那个库,因为您没有告诉 flex 不需要 yywrap
的存在。将以下内容添加到 flex 文件的开头:
%option noinput nounput noyywrap nodefault
这些选项中的前两个通过删除它们的定义来抑制关于 input
和 unput
未被使用的 Clang/GCC 警告。 (如果你真的使用它们,你当然希望它们被定义。但你没有。)
第三个选项 noyywrap
告诉 flex 在扫描程序在其输入中遇到文件结束指示时抑制对 yywrap
的调用。您可以添加 yywrap
的定义,无条件地确认文件结束,但这是没有意义的;如果您不打算使用扫描仪的该功能,那么在对虚拟 yywrap
.
第四个选项告诉 flex 如果有一些输入模式与您的规则匹配 none 则抱怨。默认情况下,flex 扫描仪通过在标准输出(或 yyout
指向的任何内容)上打印不匹配的字符来响应不匹配的输入,而不进行任何其他错误处理。这很少是你想要的,所以很高兴知道有一些输入错误会被默默地忽略,这样你就可以修复它。不幸的是,flex 没有告诉您是什么模式触发了错误,所以我会告诉您答案:.
不匹配换行符,而且您的 flex 文件中的任何其他内容都不会。你应该解决这个问题。 (.|\n
是 "anything else" 的模式)。