【发布时间】:2019-12-28 20:29:15
【问题描述】:
在为简单的扫描仪编译 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", $1);
}
| INT {
printf("The integer value you entered is - %d", $1);
}
| 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 Bison)2.3
- flex 2.5.35 苹果(flex-31)
- gcc 4.2.1
【问题讨论】: