Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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
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() {
    FILE *fh;

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

Source O'Reilly's lex & yacc.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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