2

Is anyone able to better inform me on successfully compiling a Lex source file on OS X in terminal. I have written in HelloWorld.l the following:

%%

"hello world"    printf("GOODBYE\n");
.                ;

%%

Within the console I executed lex HelloWorld.l followed by gcc lex.yy.c -ll and ./a.out receiving a blank state within my window and no result.

I am basing my knowledge on this topic from the source [1] and similar Stack question [2].

aitía
  • 217
  • 2
  • 8
  • 2
    Your `.` rule ignores unmatched inputs. For debugging, it's best to print ignored inputs (maybe to standard error, maybe with identifying text). However, even that won't help until you provide it some input to ignore — the program will wait for you to type something, anything. – Jonathan Leffler Jan 18 '18 at 23:23
  • Thank you for this clarification, would you be able to recommend a basic program to fulfil my general purpose or to further my understanding. – aitía Jan 18 '18 at 23:38
  • 1
    Simply change the `;` after the `.` to `printf("Ignored (%c)\n", yytext[0]);`. This will print a line of output for each ignored character (such as the newline after you type `hello world`). – Jonathan Leffler Jan 18 '18 at 23:39

1 Answers1

6

flex, will by default, read from standard input. You need to actually provide it some input. You can do echo "hello world" | ./a.out and you should see your result.

Ryan
  • 14,392
  • 8
  • 62
  • 102
  • Thank you for responding, may I ask you now about compiling the wordcount.l program from [link](https://stackoverflow.com/questions/8859710/lex-how-to-run-compile-a-lex-program-on-commandline)? Specifically, would you be able to explain how to provide input such as the file referenced within this code. – aitía Jan 18 '18 at 23:33
  • 1
    @aitia please ask a new question for that – Ryan Jan 18 '18 at 23:35
  • 1
    @aitía: If you have a file `xyz.data` that you want to send to your program, use `./a.out < xyz.data` (or, if you have multiple files, consider `cat file1.data file2.data | ./a.out`). – Jonathan Leffler Jan 18 '18 at 23:41