Skip to content

Instantly share code, notes, and snippets.

@anoopsarkar
Last active September 23, 2020 17:20
Show Gist options
  • Save anoopsarkar/6545633 to your computer and use it in GitHub Desktop.
Save anoopsarkar/6545633 to your computer and use it in GitHub Desktop.
Lex demos
# the following script assumes you have logged into a CSIL Linux machine or equivalent
# the first step is to make sure you are in your home directory
$ cd $HOME
# get the lexdemos source files
$ git clone https://github.com/anoopsarkar/compilers-class-hw.git
# or if you already have this directory, then pull the new files
$ cd compilers-class-hw/lexdemos
$ git pull
# build all the demo lex programs
$ make
# this will print out all the compilation steps as it builds the demo programs
# look at the makefile.simple example makefile to see how to write your own makefile
# START DEMO:
# look at each demo lex program and test it with some example inputs
$ cat demo1.lex
# each lex file starts with %%
# then each line has a regular expression followed by an action which is a C statement
# there are example inputs for each demo program in the file testfile.sh
# use grep to list all the lines for each demo program as follows:
$ grep demo1$ testfile.sh
# the above command will print out a command that is a test example for each demo.
# run each of the test examples for the demo.
# check your understanding of the regular expression to see if you understand it
# you should understand why the input works if it does,
# and why the input doesn't work when it doesn't
# then repeat for demo2, demo3, ... beginning from "START DEMO" above.
# testfile.output contains the expected output for each test example
# the following not print any differences:
$ sh testfile.sh > myoutput
$ diff myoutput testfile.output
# There are a couple of extra demos not covered by testfile.sh
# comments.lex
$ cat comments.lex
$ echo "/* this is a comment */ this is not " | ./comments # removes "some" C style comments from text
# arithexp-re:
$ cat arithexp-re.lex
$ echo "2+3*5" | ./arithexp-re # says 'yes' if the input is a valid arithmetic expression, 'no' if not
# lc:
$ cat lc.lex
$ cat lc.lex | ./lc # counts the number of lines and characters in a file
# vowelywords:
$ cat vowelywords.lex
$ ./vowelywords < /usr/share/dict/words # prints out words with all English vowels in them
# username:
$ cat username.lex
$ echo "username" | ./username # prints out your user name (in Linux or other Unix-like systems)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment