Lex - How to run / compile a lex program on the command line

I am very new to Lex and Yacc. I have a Lex program. Example: wordcount.l

I use windows and putties.

I am just trying to run this file.

  • Does wordcount.l on drive C?

  • I compile a Lex program and generate a .c program, and then what am I running?

I tried on the command line: Lex wordcount.l

but I just get the 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?

+4
source share
2 answers

First you need to go to the directory where the wordcount.l file is located using cd . Then, using lex wordcount.l , the file lex.yy.c will be created. To run the program, you need to compile it using the c compiler, such as gcc. With gcc, you can compile it with gcc -lfl lex.yy.c This will create a.out , which can be started using ./a.out

+12
source
 lex file.l gcc lex.yy.c -ly -ll ./a.out 

They also work. I use this on Ubuntu 14.04.

+3
source

Source: https://habr.com/ru/post/1390947/


All Articles