Imp_Lex.x 1.86 KB
Newer Older
Tadej Borovšak's avatar
Tadej Borovšak committed
1
-- vim: set syntax=alex autoindent ts=4 sts=4 sw=4 expandtab :
Alex Simpson's avatar
Alex Simpson committed
2

Tadej Borovšak's avatar
Tadej Borovšak committed
3
4
5
6
7
8
9
10
11
12
{
module Imp_Lex
    ( Token(..)
    , AlexPosn(AlexPn)
    , pos
    , imp_lex
    ) where
}

%wrapper "posn"
Alex Simpson's avatar
Alex Simpson committed
13

Tadej Borovšak's avatar
Tadej Borovšak committed
14
15
$digit  = [0-9]
$alpha  = [A-Za-z]
Alex Simpson's avatar
Alex Simpson committed
16

Tadej Borovšak's avatar
Tadej Borovšak committed
17
18
19
20
21
@number = $digit+
@ident  = [$alpha _] [$alpha $digit _]*
@keywords = if | then | else | while | do | skip
@operations = \+ | \- | \* | \=\= | \<
@booleans = True | False
Alex Simpson's avatar
Alex Simpson committed
22

Tadej Borovšak's avatar
Tadej Borovšak committed
23
tokens :-
Alex Simpson's avatar
Alex Simpson committed
24

Tadej Borovšak's avatar
Tadej Borovšak committed
25
    $white+           ;                           -- whitespace
Alex Simpson's avatar
Alex Simpson committed
26

Tadej Borovšak's avatar
Tadej Borovšak committed
27
28
29
30
31
32
33
34
35
36
37
    \(                { \p _ -> LPAREN p        } -- scope start
    \)                { \p _ -> RPAREN p        } -- scope end
    \;                { \p _ -> DELIM p         } -- command delimiter
    \:\=              { \p _ -> ASSIGN p        } -- assignment
    @operations       { \p s -> BINOP p s       } -- binary operations
    @keywords         { \p s -> KEYWORD p s     } -- keywords
    @number           { \p s -> NUM p $ read s  } -- integer literals
    @booleans         { \p s -> BOOL p $ read s } -- Boolean literals
    @ident            { \p s -> LOC p s         } -- locations (variables)

    .                 { \p s -> INVALID p s     } -- invalid char
Alex Simpson's avatar
Alex Simpson committed
38
39
40
41
42

{

-- The Token type - the action for each lexical class has type String -> Token

Tadej Borovšak's avatar
Tadej Borovšak committed
43
44
45
46
47
48
49
50
51
52
data Token = LPAREN  AlexPosn
           | RPAREN  AlexPosn
           | DELIM   AlexPosn
           | ASSIGN  AlexPosn
           | BINOP   AlexPosn String
           | KEYWORD AlexPosn String
           | NUM     AlexPosn Integer
           | BOOL    AlexPosn Bool
           | LOC     AlexPosn String
           | INVALID AlexPosn String
Tadej Borovšak's avatar
Tadej Borovšak committed
53
           deriving Show
Alex Simpson's avatar
Alex Simpson committed
54

Tadej Borovšak's avatar
Tadej Borovšak committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-- Position accessor
pos :: Token -> AlexPosn
pos (LPAREN  p  ) = p
pos (RPAREN  p  ) = p
pos (DELIM   p  ) = p
pos (ASSIGN  p  ) = p
pos (BINOP   p _) = p
pos (KEYWORD p _) = p
pos (NUM     p _) = p
pos (BOOL    p _) = p
pos (LOC     p _) = p
pos (INVALID p _) = p

-- Exported lexer
Alex Simpson's avatar
Alex Simpson committed
69
70
71
imp_lex = alexScanTokens

}