Search code examples
compiler-constructionyacclexflex-lexer

Lex (flex): how to provide input and where's output?


I'm extremely new to Flex and I'm stuck at this early point. I have a Lex file 20.l whose content is

%{
/* a Lex program that adds line numbers
   to lines of text, printing the new text
   to the standard output
*/
#include <stdio.h>
int lineno = 1;
%}
line .*\n
%%
{line} { printf("%5d %s", lineno++, yytext); }
%%
main()
{ yylex(); return 0; }

I copied the code from my textbook (it doesn't tell me how to deal with my question here). I have done

flex 20.l

and got file lex.yy.c. Then I compiled it with

gcc lex.yy.c -o ADD -lfl

and got the executable file ADD.

Now how can I use this ADD to add line numbers to other text files? For example, what commands should I use if the input file name is "try.c"? I tried "./ADD try.c" but it obviously didn't work. And how is the output represented?

Thank you. I know this is really a stupid question but it seems nobody is teaching how to do this online...


Solution

  • I tried "./ADD try.c"

    ./ADD < try.c
    

    The output appears on stdout. If you want different file handling you can write your own main().