Lex - 如何在命令行上运行/编译 lex 程序
我对 Lex 和 Yacc 很陌生。我有一个 Lex 程序。示例:wordcount.l
我正在使用 Windows 和 Putty。
我只是想运行这个文件..
wordcount.l
文件是否位于 C 驱动器上?我是否编译 Lex 程序,它生成一个
.c
程序,然后运行什么?
我在命令行上尝试过:Lex wordcount.l
但我只是找不到文件...
wordcount.l
%{
#include <stdlib.h>
#include <stdio.h>
int charCount=0;
int wordCount=0;
int lineCount=0;
%}
%%
\n {charCount++; lineCount++;}
[^ \t\n]+ {wordCount++; charCount+=yyleng;}
. {charCount++;}
%%
main(argc, argv)
int argc;
char** argv;
{
if (argc > 1)
{
FILE *file;
file = fopen(argv[1], "r");
if (!file)
{
fprintf(stderr, "Could not open %s\n", argv[1]);
exit(1);
}
yyin = file;
}
yylex();
printf("%d %d %d\n", charCount, wordCount, lineCount);
}
在 putty 中如何编译和运行这个程序?
I am very new to Lex and Yacc. I have a Lex program. Example: wordcount.l
I am using windows and putty.
I am just trying to run this file..
Does the
wordcount.l
file go on the C drive?Do I compile the Lex program and it generates a
.c
program and then what do I run?
I tried on the command-line: Lex wordcount.l
but I just get file not found...
wordcount.l
%{
#include <stdlib.h>
#include <stdio.h>
int charCount=0;
int wordCount=0;
int lineCount=0;
%}
%%
\n {charCount++; lineCount++;}
[^ \t\n]+ {wordCount++; charCount+=yyleng;}
. {charCount++;}
%%
main(argc, argv)
int argc;
char** argv;
{
if (argc > 1)
{
FILE *file;
file = fopen(argv[1], "r");
if (!file)
{
fprintf(stderr, "Could not open %s\n", argv[1]);
exit(1);
}
yyin = file;
}
yylex();
printf("%d %d %d\n", charCount, wordCount, lineCount);
}
In putty how do I compile and run this program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您首先必须使用
cd
转到文件wordcount.l
所在的目录。然后使用lex wordcount.l
将生成文件lex.yy.c
。要运行程序,您需要使用c 编译器(例如 gcc)对其进行编译。使用 gcc,您可以使用 gcc -lfl lex.yy.c 来编译它。这将创建可以使用./a.out
运行的a.out
You first have to go to the directory which the file
wordcount.l
is in usingcd
. Then usinglex wordcount.l
will make the filelex.yy.c
. To the run the program you need compile it with a c compiler such as gcc. With gcc you can compile it usinggcc -lfl lex.yy.c
. This will createa.out
which can be run using./a.out
这些也有效。我在 Ubuntu 14.04 中使用这个。
These also works. I am using this in Ubuntu 14.04.