-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.x
117 lines (106 loc) · 4.9 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{
module Lexer where
import System.IO
import System.IO.Unsafe
}
%wrapper "posn"
$digit = 0-9 -- digits
$alpha = [a-zA-Z] -- alphabetic characters
tokens :-
"//".* ;
: { \p s -> Colon (getLC p)}
";" { \p s -> SemiColon (getLC p)}
int { \p s -> Type s (getLC p)}
if { \p s -> If (getLC p)}
"else" { \p s -> Else (getLC p)}
"loop" { \p s -> Loop (getLC p)}
then { \p s -> Then (getLC p)}
write { \p s -> Write (getLC p)}
> { \p s -> Greater (getLC p)}
"<" { \p s -> Less (getLC p)}
$digit+ { \p s -> Int (read s) (getLC p)}
"(" { \p s -> OpenPar (getLC p)}
")" { \p s -> ClosePar (getLC p)}
Float { \p s -> Float s (getLC p)}
Char { \p s -> Char s (getLC p)}
types { \p s -> TypesBlock (getLC p)}
vars { \p s -> VarsBlock (getLC p)}
subprograms { \p s -> SubprogramsBlock (getLC p)}
process { \p s -> ProcessBlock (getLC p)}
fields { \p s -> FieldsBlock (getLC p)}
operations { \p s -> OperationsBlock (getLC p)}
"{" { \p s -> OpenCurlyBrackets (getLC p)}
"}" { \p s -> CloseCurlyBrackets (getLC p)}
= { \p s -> Equals (getLC p)}
num { \p s -> Num s (getLC p)}
tempTemp { \p s -> NumWithSpecification (getLC p)}
return { \p s -> Return (getLC p)}
"," { \p s -> Comma (getLC p)}
"+" { \p s -> Add (getLC p)}
"*" { \p s -> Mult (getLC p)}
"-" { \p s -> Sub (getLC p)}
"^" { \p s -> Pow (getLC p)}
"/" { \p s -> Div (getLC p)}
"%" { \p s -> Mod (getLC p)}
"==" { \p s -> Equiv (getLC p)}
"!=" { \p s -> Diff (getLC p)}
print { \p s -> Print (getLC p)}
input { \p s -> Input (getLC p)}
"." { \p s -> Dot (getLC p)}
\" .* \" { \p s -> StringLit s (getLC p)}
$alpha [$alpha $digit \_ \']* { \p s -> Id s (getLC p)}
'"' { \p s -> Quote (getLC p)}
$white+ ;
{
-- Each action has type :: AlexPosn -> String -> Token
-- The token type:
data Token =
Colon (Int, Int) |
SemiColon (Int, Int) |
Type String (Int, Int) |
If (Int, Int) |
Else (Int, Int) |
Loop (Int, Int) |
Then (Int, Int) |
Write (Int, Int) |
Greater (Int, Int) |
Less (Int, Int) |
Id String (Int, Int) |
Int Int (Int, Int) |
StringLit String (Int, Int) |
OpenPar (Int, Int) |
ClosePar (Int, Int) |
Float String (Int, Int) |
Char String (Int, Int) |
TypesBlock (Int, Int) |
VarsBlock (Int, Int) |
SubprogramsBlock (Int, Int) |
ProcessBlock (Int, Int) |
FieldsBlock (Int, Int) |
OperationsBlock (Int, Int) |
OpenCurlyBrackets (Int, Int) |
CloseCurlyBrackets (Int, Int) |
Equals (Int, Int) |
Num String (Int, Int) |
NumWithSpecification (Int, Int) |
Comma (Int, Int) |
Add (Int, Int) |
Sub (Int, Int) |
Mult (Int, Int) |
Pow (Int, Int) |
Div (Int, Int) |
Mod (Int, Int) |
Equiv (Int, Int) |
Diff (Int, Int) |
Print (Int, Int) |
Input (Int, Int) |
Dot (Int, Int) |
Quote (Int, Int) |
Return (Int, Int)
deriving (Eq,Show)
getLC (AlexPn _ l c) = (l, c)
getTokens fn = unsafePerformIO (getTokensAux fn)
getTokensAux fn = do {fh <- openFile fn ReadMode;
s <- hGetContents fh;
return (alexScanTokens s)}
}