如何编译使用 Yacc 和 Lex 构建的程序?
我的 Yacc 源位于 pos.yacc,我的 Lex 源位于 pos1.lex,如图所示。
pos1.lex
%{
#include "y.tab.h"
int yylval;
%}
DIGIT [0-9]+
%%
{DIGIT} {yylval=atoi(yytext);return DIGIT;}
[\n ] {}
. {return *yytext;}
%%
pos.yacc
%token DIGIT
%%
s:e {printf("%d\n",$1);}
e:DIGIT {$$=$1;}
|e e "+" {$$=$1+$2;}
|e e "*" {$$=$1*$2;}
|e e "-" {$$=$1-$2;}
|e e "/" {$$=$1/$2;}
;
%%
main() {
yyparse();
}
yyerror() {
printf("Error");
}
编译错误
编译时我收到如下错误:
malathy@malathy:~$ cc lex.yy.c y.tab.c -ly -ll
pos.y: In function ‘yyerror’:
pos.y:16: warning: incompatible implicit declaration of built-in function ‘printf’
pos.y: In function ‘yyparse’:
pos.y:4: warning: incompatible implicit declaration of built-in function ‘printf’
- 是什么导致了这些错误?
- 我该如何编译 Lex 和 Yacc 源代码?
My Yacc source is in pos.yacc and my Lex source is in pos1.lex, as shown.
pos1.lex
%{
#include "y.tab.h"
int yylval;
%}
DIGIT [0-9]+
%%
{DIGIT} {yylval=atoi(yytext);return DIGIT;}
[\n ] {}
. {return *yytext;}
%%
pos.yacc
%token DIGIT
%%
s:e {printf("%d\n",$1);}
e:DIGIT {$=$1;}
|e e "+" {$=$1+$2;}
|e e "*" {$=$1*$2;}
|e e "-" {$=$1-$2;}
|e e "/" {$=$1/$2;}
;
%%
main() {
yyparse();
}
yyerror() {
printf("Error");
}
Compilation errors
While compiling I am getting errors like:
malathy@malathy:~$ cc lex.yy.c y.tab.c -ly -ll
pos.y: In function ‘yyerror’:
pos.y:16: warning: incompatible implicit declaration of built-in function ‘printf’
pos.y: In function ‘yyparse’:
pos.y:4: warning: incompatible implicit declaration of built-in function ‘printf’
- What causes those errors?
- How am I supposed to compile Lex and Yacc source code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
printf() 是在 stdio.h 中定义的,因此只需将其包含在 pos1.lex 中的 y.tab.h 上方即可:
printf() is defined in stdio.h so just include it above y.tab.h in pos1.lex:
您可以从 trojanfoe 直接回答您的问题 - 您需要包含来声明函数
printf()
。这在任何提供给 C 编译器的源代码中都是如此。但是,您还应该注意,Yacc 源代码的常规后缀是
.y
(而不是.yacc
),而 Lex 源代码的常规后缀是.l
>(而不是.lex
)。特别是,使用这些后缀意味着make
将知道如何处理您的源代码,而不必手动编写编译规则。给定文件
lex.l
和yacc.y
,make
使用以下方法将它们编译为目标代码:它位于一个带有 makefile 的目录中,该 makefile 设置 <代码>CFLAGS = -O -std=c99 -Wall -Wextra。 (这是在 MacOS X 10.6.6 上。)有时您会看到使用其他类似的规则;特别是,
lex
默认生成一个文件lex.yy.c
(至少在 MacOS X 上),并且您经常会看到如下规则:甚至:
替代方案有很多。使用
make
就可以了。You have the direct answer to your question from trojanfoe - you need to include
<stdio.h>
to declare the functionprintf()
. This is true in any source code presented to the C compiler.However, you should also note that the conventional suffix for Yacc source is
.y
(rather than.yacc
), and for Lex source is.l
(rather than.lex
). In particular, using those sufffixes means thatmake
will know what to do with your source, rather than having to code the compilation rules by hand.Given files
lex.l
andyacc.y
,make
compiles them to object code using:This is in a directory with a makefile that sets
CFLAGS = -O -std=c99 -Wall -Wextra
. (This was on MacOS X 10.6.6.) You will sometimes see other similar rules used; in particular,lex
generates a filelex.yy.c
by default (at least on MacOS X), and you'll often see a rule such as:Or even:
The alternatives are legion; use
make
and it gets it right.包含头文件stdio.h
进行编译
打开终端找到这两个文件并输入
您可以按照以下步骤操作。
Include header file stdio.h
for compilation
open terminal locate both files and type
You can follow these steps.
你只需要保存在文件中
pos1.l 和 pos.y
我希望它能起作用
you just need to save in file
pos1.l and pos.y
I hope it will work