CS 433 - Homework 2 - 2/18/08

Scanning with (f)lex

Due: Monday, 2/25/08, at 2pm

Task 1

Create a scanner for our Omg language using the scanner generator "flex". Start by copying my directory ~schar/cs433/hw2, and add your symbol table implementation from homework 1:

    ~> cd cs433
    ~/cs433> cp -r ~schar/cs433/hw2 .
    ~/cs433> cp hw1/symtab.c hw2/
    ~/cs433> cp hw1/symtab.h hw2/
    ~/cs433> cd hw2
    ~/cs433/hw2> ls
    Makefile  scanner.l  symtab.h  syntree.h    tokennames.h
    main.c    symtab.c   syntax.h  testhw2.omg  tokens.h
Your task is to turn the file "scanner.l" into a valid flex input file for the Omg language. Before you start programming, look at all the other files and try to make sense of them. The explanation I gave in class should help. Also read Chapter 3.5 and the man page for flex (on bj). To compile everything, type "make depend" (once) and "make" (after each change).

When you're done, your program should work as follows

    ~/cs433/hw2> ./omg testhw2.omg

    line   3:  ID(omg) LPAREN RPAREN LBRACE
    line   4:  NUM ID(a) ASSIGN CONST(3) SEMI
    line   5:  NUM ID(b) ASSIGN MINUS ID(a) PLUS CONST(97) MULT CONST(92) SEMI
    line   6:  ID(prn) LPAREN ID(b) RPAREN SEMI
    line   7:  RBRACE
Test your scanner on the files in "~schar/cs433/omg/", and also using your own omg files as described next. Note: Your scanner should tolerate multi-line comments, but it's OK if the line numbers get messed up. But if you can figure out how to avoid that, you'll get some extra credit.

Task 2

Write two Omg programs, and make sure your scanner can read them. We will use everyone's Omg files for testing the parser next week, so try to adhere to Omg specification. Name your files using your initials followed by a dash and a word describing what the program does. For example, if I had written quick sort, I would use the name "ds-qsort.omg". Keep your Omg files in your hw2 directory and add their names to the line in the Makefile starting with "OMG =".

Also write a third Omg file containing illegal symbols and make sure your scanner produces error messages. You should not add this Omg file to the Makefile.

Task 3

Write a formal grammar (CFG) for Omg. Use the grammar in Appendix A.1 of our textbook as a model. Use a word processor to type in your grammar, and use normal font (or italics) for nonterminals, and bold font for terminals. Use the terminals ID and CONST for identifiers and integers, and use the keywords and symbols themselves for all other terminals (i.e., use :=, not assign).


What to hand in

Hand in printouts of your completed file scanner.l, of your two Omg programs, and of your grammar. Use the "pretty-print" function from XEmacs for printing the programs. Also submit your programs electronically by typing "make submit".

Please note on your submission how much time it took you.