CSE 423 Lab #1: Flex

Turnin: a .zip on Canvas.

Group Work

For each team:

Individual Work

1. Flex Basics

Use Flex to compile and run the "word counter" flex program from the Flex manual page, or flex-wc.l (HTML, raw .l: flex-wc.l), a version of the word counter that comes from github. In your .zip submission, you can include your final versions of source code files used here, plus a file lab1.1_answers.txt
  1. When you run flex flex-wc.l, what output file does flex write?
  2. When you run gcc on Flex's output .c file, what is the executable file named?
  3. Run your program on itself with ./a.out <flex-wc.l
  4. Fix your program to take the name of the file as a command line argument, instead of using < to redirect standard input.
  5. Convert to separate compilation! move main() into a separate main.c and write a makefile that compiles main.c and lex.yy.c separately and then links them.

2. Flex (Most of) your HW#2 Away

Well, you can save the "allocate tokens and build a linked list of them" part for later, but if you complete this lab, you should have your regular expressions identified, and have integer codes for each category. We should also get a good idea of how to handle some of Kotlin's more quirky rules.

Let's walk through Kotlin's description of Kotlin's lexical analysis, its so-called lexical grammar. Then we proceed as follows.

  1. Copy flex-wc.l to a file named kotlex.l
  2. Modify kotlex.l to discard whitespace, but otherwise count chars/words
  3. Modify kotlex.l to discard comments, but otherwise count chars/words
  4. Modify kotlex.l to return a 257 on each word, and modify the main() to call yylex() in a while loop. Print chars and words at the end.
  5. Change the while loop to print out a line showing each word after yylex() returns it. Print the return code as well as yytext
  6. Add regular expressions to recognize reserved words and return different integers for each one. Fail and stop with an error message if a Kotlin reserved word not in k0 appears.
  7. Add regular expressions for operators and punctuation and return different integers for each one.
  8. Add regular expressions for integer, float and string literals. Return a different integer code for each kind of literal.
  9. Figure out what to do about Kotlin's less obvious tokens. You may work on this part collaboratively.
  10. Ask questions about anything fishy in Kotlin that we haven't specified yet. We will have to get all decisions written down and shared.