Writing lex, writing yacc, and running the programs


How to write your lex programs.

  1. Get into emacs with Unix.
  2. Name your program something.l (or anything_you_like.l).  That final character is a lower-case L.
  3. In emacs, type meta-x (escape followed by x) makefile-mode.
  4. To compile your lex file:  flex something.l .  This produces a C file called something.yy.c.
  5. To link it by itself:  gcc -o myProgram -ll something.yy.c .  gcc is the GNU C compiler.  -o myProgram means "name the output of the compiler 'myProgram.'"  -ll means link in the lex library.
  6. Run it by typing ./myProgram .

The action associated with a lex rule will probably be to print something.


How to write your lex & yacc programs.  Remember, flex is a version of lex, and bison is a version of yacc.

  1. Name your program something.y .
  2. In emacs, type meta-x (escape followed by x) makefile-mode.
  3. Compile it like this:  bison -d something.y .  The -d option causes it to create y.tab.h, which may be needed by something.l.
  4. flex something.l
  5. gcc -o myProgram y.tab.c lex.yy.c -ly.  The -ly option means link in the yacc library.
  6. Run it by typing ./myProgram .

The action associated with a lex rule will probably be to return a value (identifying what was matched).  The action associated with a yacc rule will probably be to print something.


How to run your lex or lex/yacc program:

./myProgram

This takes input from the keyboard, line by line, and prints the results on the screen.  To end the input, type Ctrl-D and hit enter.  This fakes an end-of-file.

./myProgram < myTestFile.txt

This takes input from myTestFile.txt, and prints the results on the screen.

./myProgram < myTestFile.txt > myOutput.txt.

This sends the output to myOutput.txt.