-
Notifications
You must be signed in to change notification settings - Fork 2
/
boolf.lex
77 lines (71 loc) · 2.59 KB
/
boolf.lex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(* User Declarations*)
structure T = Tokens
type pos = (int*int)
type svalue = T.svalue
type ('a,'b) token = ('a,'b) T.token
type lexresult = (svalue, pos) token
val linp = ref 1;
val colp = ref 1;
val eolpos = ref 0;
exception UnknownToken;
val lexerError : string * int * int -> unit =
fn (badToken ,lineNo, colNo) => (TextIO.print(
"Unknown Token:"^(Int.toString lineNo)^":"^(Int.toString colNo)^":"^badToken^"\n");
raise UnknownToken)
val eof = fn () => (
T.EOF ((!linp, !colp), (!linp, !colp))
)
val keywords = [
("PLUS", T.PLUS),
("MINUS", T.MINUS),
("TIMES", T.TIMES),
("NEGATE", T.NEGATE),
("EQUALS", T.EQUALS),
("LESSTHAN", T.LESSTHAN),
("GREATERTHAN", T.GREATERTHAN),
("AND", T.AND),
("OR", T.OR),
("XOR", T.XOR),
("NOT", T.NOT),
("IMPLIES", T.IMPLIES),
("if", T.IF),
("then", T.THEN),
("else", T.ELSE),
("fi", T.FI),
("let", T.LET),
("in", T.IN),
("end", T.END),
("int", T.INTTYP),
("bool", T.BOOLTYP),
("fun", T.FUNDECL),
("fn", T.LAMBDA)
]
fun findKeyword(text: string, pos1: pos, pos2: pos) =
case List.find (fn (s, _) => s=text) keywords of
SOME (str, tk) => tk(pos1, pos2)
|NONE => T.ID(text, pos1, pos2)
%%
%header (functor BoolfLexFun(structure Tokens : Boolf_TOKENS));
%s BOOLF;
alpha = [a-zA-Z];
digit = [0-9];
ws = [\ \t];
eol = ("\013\010"|"\010"|"\013");
%%
<INITIAL>{ws}* => (linp:=1; eolpos:=0; YYBEGIN BOOLF; lex());
<BOOLF>{ws}+ => ( lex() );
<BOOLF>{eol} => (linp:=(!linp)+1; eolpos:=yypos+size yytext; colp:= 1; lex());
<BOOLF>{digit}+ => (colp:=yypos-(!eolpos)+1; T.NUM(
List.foldl (fn (a,r) => ord(a) - ord(#"0") + 10*r) 0 (explode yytext),
(!linp, !colp), (!linp, !colp)));
<BOOLF>"TRUE" => (colp:=yypos-(!eolpos)+1; T.BOOLCONST(true, (!linp, !colp), (!linp, !colp)));
<BOOLF>"FALSE" => (colp:=yypos-(!eolpos)+1; T.BOOLCONST(false, (!linp, !colp), (!linp, !colp)));
<BOOLF>{alpha}({alpha}|{digit})* => (colp:=yypos-(!eolpos)+1; findKeyword(yytext, (!linp, !colp), (!linp, !colp)));
<BOOLF>":" => (colp:=yypos-(!eolpos)+1; T.TYPDECL((!linp, !colp), (!linp, !colp)));
<BOOLF>"->" => (colp:=yypos-(!eolpos)+1; T.ARROW((!linp, !colp), (!linp, !colp)));
<BOOLF>"=>" => (colp:=yypos-(!eolpos)+1; T.DARROW((!linp, !colp), (!linp, !colp)));
<BOOLF>"=" => (colp:=yypos-(!eolpos)+1; T.ASSIGN((!linp, !colp), (!linp, !colp)));
<BOOLF>";" => (colp:=yypos-(!eolpos)+1; T.TERM((!linp, !colp), (!linp, !colp)));
<BOOLF>"(" => (colp:=yypos-(!eolpos)+1; T.LPAREN((!linp, !colp), (!linp, !colp)));
<BOOLF>")" => (colp:=yypos-(!eolpos)+1; T.RPAREN((!linp, !colp), (!linp, !colp)));
<BOOLF>. => (colp:=yypos-(!eolpos)+1; lexerError(yytext, !linp, !colp); T.ERROR((!linp, !colp), (!linp, !colp)));