Run sml and compile the code using CM.make "sources.cm";
Build a lexer for the Fun language. In order to do this, you will need to figure out all of the tokens that you need to lex, by reading The definition of Fun.
You will be building your lexer using ML-LEX. There is online documentation on ML-LEX here. There is also some help in your textbook (Appel, chapter 2).
(Actually, you can get by without understanding the stuff about "('svalue,'pos) token" in tokens.sig. However, do the experiment of writing a little program that just calls Tokens.ARROW(0,0) just to see what it's doing. You can even make this call from the SML/NJ interactive top-level prompt.)(Also, I wouldn't think any less of you if you didn't bother to understand the first four lines of fun.lex; that's just boilerplate for attaching the lexer to the parser.)
-> | ARROW | ! | BANG | |
:= | ASSIGN | ) | RPAREN | |
( | LPAREN | || | OR | |
& | AND | = | EQ | |
> | GT | < | LT | |
* | TIMES | - | MINUS | |
+ | PLUS | ; | SEMICOLON | |
, | COMMA | : | COLON | |
#i | PROJ | (where i is a nonnegative integer without leading 0's: 0,1,...) |
Fun keywords should be represented using tokens with the same name. The end-of-file token should be represented using the token EOF.
Each token takes two integers: the line number and column number of the beginning of the token. These are used for error reporting. In the example below, x is on the second line in column 7. Notice, the first row is row 1 and the first column is column 1 (as opposed to 0).
if true then
let x = ...
The make_pos function is a convenient way to convert the things ML-Lex knows (yypos and yytext) into the file positions of the beginning and end of the token.
.... fun eof () = ... %% alpha = [A-Za-z]; .... %%fun => (Tokens.FUN(make_pos(yypos,yytext)));
....