-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.x
61 lines (54 loc) · 1.05 KB
/
Lexer.x
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
{
module Scanner (Token(..), lexFromFile, lexFromFileToFile, alexScanTokens) where
}
%wrapper "basic"
$digit = 0-9
$alpha = [a-zA-Z]
$upper = [A-Z]
$lower = [a-z]
tokens :-
$white+ ;
"(" { \s -> S_Lparen }
"\" { \s -> S_Bslash }
"." { \s -> S_Period }
")" { \s -> S_Rparen }
"+" { \s -> S_Plus }
"-" { \s -> S_Minus }
"->" { \s -> S_Arrow }
"if" { \s -> S_If }
"then" { \s -> S_Then }
"else" { \s -> S_Else }
$digit+ { \s -> S_Int (read s) }
$alpha { \s -> S_Var (head s) }
fix { \s -> S_Fix }
{
data Token =
S_Lparen
| S_Bslash
| S_Period
| S_Rparen
| S_Plus
| S_Minus
| S_Arrow
| S_If
| S_Then
| S_Else
| S_Int Int
| S_Var Char
| S_Fix
deriving (Eq,Show)
lexFromFile fil =
do
s <- readFile fil
let toks = (alexScanTokens s)
return toks
lexFromFileToFile infile outfile =
do
toks <- lexFromFile infile
let stoks = show toks
writeFile outfile stoks
main =
do
s <- getContents
print (alexScanTokens s)
}