CS321 Prog Lang & Compilers                  Assignment # 6
Assigned: Jan 31, 2007               Due: Mon. Feb. 5, 2007


1) You are to write a simple lexical anlyzer for a language of
numeric expressions. Example expressions include:
    "x+3"
    "(x- 3) *Ys"
    "w12"

Expressions contain the following lexical elements:

  1) identifiers         a letter (lower or upper case) followed by
                         possibly more letters or digits. Maximun length 10.
  2) operators           + - * /
  3) integer constants   one to nine digits
  4) whites space        spaces, tabs, or newlines
  5) parenthesis         ( and )

Datatypes for storing expression tokens follows:

datatype Operation = Plus | Minus | Times | Divide

datatype Etoken =
    Ident of string
  | Oper of Operation
  | Const of int
  | LParen
  | RParen
  | Eof
  | Error of string

You are to use ml-lex to build a function "MakeExprLexer:: string -> () -> Etoken".
The reccomended use of the this function is to partially apply it to a string, to
return a function that lexes that string.

val lex = MakeExprLexer "(x - %sd) 1234567890"

Then every time you apply lex to (), the function returns the next Etoken. For
example, given the above:

lex() ----->  LParen

lex() -----> Ident "x"

lex() -----> Oper Minus

lex() -----> Error "Unexpected character %"

lex() -----> Ident "sd"

lex() -----> RParen

lex() -----> Error "Integer to long: 1234567890"

lex() -----> Eof

lex() -----> Eof

This should be easy to do. It requires simple modification of the
regexpLex example done in class, Which is available for download on the class
web page at http://web.cecs.pdx.edu/~sheard/course/Cs321/LexYacc/regexpLex/


2) Cut and paste the following into your solution file.
==================================================================

datatype Gender = Male | Female;

datatype Color = Red | Blue | Green | Yellow | Orange;

val people =
  [("tim",25,Male,Red)
  ,("mary",30,Female,Orange)
  ,("john",14,Male,Yellow)
  ,("bob",55,Male,Green)
  ,("jane",19,Female,Blue)
  ,("alexi",25,Male,Green)
  ,("joan",31,Female,Blue)
  ,("jill",16,Female,Green)];

================================================================
Use the library functions List.map, List.filter, List.find,
List.exists, String.extract and other library functions
discussed in class, as well as anonymous functions,  to write
programs that do the following. Test each one on the data above.

Write functions from (people list) to (people list) that:

1) Returns all people with favorite color Blue.

2) All Males older than 18

3) All people whose name starts with the letter "j" who like Red.

4) All people who are Male or who like Blue.

Write functions from (people list) to boolean that:

5) Test if anyone is older than 30 and who likes Blue.

6) Test if everyone is Male or named "tim"

Write functions from (people list) to people that:
 Note if no-one meets the criteria raise an exception.

7) Finds a person that likes Blue whose name has second letter "o".

8) Finds a person older than 12 who is Female.



Back to CS 321, Languages and Compiler Design, Assignment Page