我对Lex和Yacc很新.我有一个Lex程序.例:wordcount.l
我正在使用窗户和腻子.
我只是想运行这个文件..
该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);
}
Run Code Online (Sandbox Code Playgroud)
在putty中如何编译和运行该程序?
Bil*_*ain 16
首先必须转到文件wordcount.l
所在的目录cd
.然后使用lex wordcount.l
将生成文件lex.yy.c
.要运行该程序,您需要使用g编译器(如gcc)对其进行编译.使用gcc,您可以使用它进行编译gcc -lfl lex.yy.c
.这将创建a.out
可以使用的运行./a.out
lex file.l
gcc lex.yy.c -ly -ll
./a.out
Run Code Online (Sandbox Code Playgroud)
这些也有效。我在 Ubuntu 14.04 中使用它。