First of all, I was not sure whether to post this question here or on stackoverflow, but here's my question anyway.

Besides using input redirection, i.e. ./code < inputFile.dat, is there any way I can load my data file into a program written in lex?

Basically, here's how I would like to be able to load my data: ./code inputFile.dat

Is there any way I can do that?

  • lex as in the lexical analyser generator? – Mark Wagner Mar 6 '14 at 20:09
  • yes, sir. I am referring to the lexical analyser lex under Linux – Fadi Hanna AL-Kass Mar 6 '14 at 20:12
  • 1
    Under Linux you won't find lex, that one is propietary (and quite bit-rotted by now AFAIU). You are using flex, which is quite different. And its manual (the info version) should be more than enough to show you how it is done. – vonbrand Mar 6 '14 at 21:17
  • ls -l /usr/bin/lex symlink to flex. – user55518 Mar 7 '14 at 12:32

Normally you would use

main() { yylex(); }

which reads the input from stdin.

To make read it from a file

main(int argc, char* argv[]) {
    FILE *fh;

    if (argc == 2 && (fh = fopen(argv[1], "r")))
        yyin = fh;
    return 0;

Source O'Reilly's lex & yacc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.