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.lfile go on the C drive? -
Do I compile the Lex program and it generates a
.cprogram 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?
You first have to go to the directory which the file
wordcount.lis in usingcd. Then usinglex wordcount.lwill 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.outwhich can be run using./a.out