CSE 423 Lab #1: Flex
Turnin: a .zip on Canvas.
Group Work
For each team:
- Find and Introduce yourselves, then spend 15 minutes on the following
- Assign one person to build a list of integer codes (#define or enum names)
for all of the Kotlin operators
- Assign one person to build a list of integer codes (#define or enum names)
for all of the Kotlin reserved words
- [If your team has three members] Assign one person to build a list of integer codes (#define or enum names)
for all other Kotlin tokens (literal constants, identifiers, etc.)
It is recommended that you use the mnemonic names given in the Kotlin official
lexical grammar when such are available.
- Some of these are longer/harder than others, just do the best you can
in 15 minutes.
- After 15 minutes, report to your teammates what your list consists of,
and what questions or obstacles you have. Exchange your lists with each other.
Discuss any corrections/refinements needed, and make changes if you can see
things to fix.
- End result of group section of lab: each group member has a .h file
with all the integer categories/tokens needed for lexical analysis of Kotlin
- Turnin this .h file within the .zip for your individual lab efforts.
Include a README file that rates your teammates as
2=full participation, 1=partial participation, or 0= not present or did
not contribute
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
- When you run
flex flex-wc.l, what output file
does flex write?
- When you run
gcc on Flex's output .c file,
what is the executable file named?
- Run your program on itself with
./a.out <flex-wc.l
- Fix your program to take the name of the file as a command line
argument, instead of using < to redirect standard input.
- 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.
- Copy flex-wc.l to a file named kotlex.l
- Modify kotlex.l to discard whitespace, but otherwise count chars/words
- Modify kotlex.l to discard comments, but otherwise count chars/words
- 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.
- Change the while loop to print out a line showing each word after
yylex() returns it. Print the return code as well as
yytext
- 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.
- Add regular expressions for operators and punctuation and
return different integers for each one.
- Add regular expressions for integer, float and string
literals. Return a different integer code for each kind of literal.
-
Figure out what to do about Kotlin's less obvious tokens.
You may work on this part collaboratively.
- Ask questions about anything fishy in Kotlin that we haven't specified
yet. We will have to get all decisions written down and shared.