An exercise to get you started with lex


We'll be in Hobbs 113.

  1. Log in to the Unix system using MobaXTerm.
  2. You'll want a command (terminal) window, and an emacs editor window
  3. In the private directory, create a subdirectory 322; in that, create a subdirectory yacc.
  4. Using the editor emacs, put in this lex file and see if it works.  It's from http://ds9a.nl/lex-yacc/cvs/lex-yacc-howto.html, which you'll have read.
    /* 
       Lex file to recognize integer literals and words
       Will Briggs
       CS 322
       Spring 2018
    */
    
    %{
    #include <stdio.h>
    %}
    
    %%
    [0123456789]+           printf("INT_LITERAL\n");
    [a-zA-Z][a-zA-Z0-9]*    printf("WORD\n");
    %%
    All you need to know about C I/O is here.
To run this:

5. Now, for grins, add the ability to your lex file to skip whitespace; and to recognize (),+-.  Hint:  if a character means something in lex (like + or ( ), put \ in front of it.

If you want to never have to type ./ for a command again:
cd ~
emacs .profile &
add this line to the end: $PATH = "$PATH:." and save.

To make editing easier, Escape, X, makefile-mode. There doesn't seem to be a lex mode, but makefile-mode is close enough. Modes tailor the editor to the type of program you're writing, especially in regard to tabbing.