-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.rkt
124 lines (117 loc) · 4 KB
/
lexer.rkt
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
118
119
120
121
122
123
124
#lang racket
(require parser-tools/lex
(prefix-in : parser-tools/lex-sre)
parser-tools/yacc)
(require racket/pretty)
(define simple-python-lexer
(lexer
(";" (token-semicolon))
("=" (token-assignto))
("+" (token-plus))
("-" (token-minus))
("*" (token-multiply))
("/" (token-divide))
("**" (token-power))
("==" (token-equals))
("<" (token-lessthan))
(">" (token-greaterthan))
("(" (token-opening-paranthesis))
(")" (token-closing-paranthesis))
("[" (token-opening-bracket))
("]" (token-closing-bracket))
(":" (token-colon))
("," (token-comma))
((:or "\"" "'") (token-double-quote))
("pass" (token-pass))
("break" (token-break))
("continue" (token-continue))
("return" (token-return))
("global" (token-global))
("print" (token-print))
("printval" (token-printval))
("evaluate" (token-evaluate))
("def" (token-def))
("if" (token-IF))
("else" (token-ELSE))
("for" (token-FOR))
("in" (token-IN))
("or" (token-OR))
("and" (token-AND))
("not" (token-NOT))
("True" (token-TRUE))
("False" (token-FALSE))
("None" (token-NONE))
((:or (:+ (char-range #\0 #\9))
(:: (:+ (char-range #\0 #\9)) #\.
(:+ (char-range #\0 #\9))))
(token-NUM (string->number lexeme)))
((::
(:or (char-range #\a #\z)
(char-range #\A #\Z)
#\_)
(:*
(:or (char-range #\a #\z)
(char-range #\A #\Z)
(char-range #\0 #\9)
#\_)))
(token-ID lexeme))
((:: (:or "\"" "'")
(complement (:or "\"" "'"))
(:or "\"" "'"))
(token-FILE-ADDRESS lexeme))
(whitespace (simple-python-lexer input-port))
((eof) (token-EOF))))
(define-tokens a (NUM ID FILE-ADDRESS))
(define-empty-tokens b (EOF
semicolon
assignto
plus
minus
multiply
divide
power
equals
lessthan
greaterthan
opening-paranthesis
closing-paranthesis
opening-bracket
closing-bracket
colon
comma
double-quote
pass
break
continue
return
global
print
printval
evaluate
def
IF
ELSE
FOR
IN
OR
AND
NOT
TRUE
FALSE
NONE))
;test
;(define test-program (open-input-string "evaluate('./test.plpy');"))
(define test-program (open-input-file "./test.plpy"))
(define lex-this (lambda (lexer input) (lambda () (lexer input))))
(define my-lexer (lex-this simple-python-lexer test-program))
(define (lex-all my-lexer)
(let ((lex-res (my-lexer)))
(if (equal? 'EOF lex-res)
(list lex-res)
(cons lex-res (lex-all my-lexer)))))
(define test-lex-output (open-output-file "./testlex.txt" #:exists 'replace))
(pretty-print (lex-all my-lexer) test-lex-output)
(close-output-port test-lex-output)
(provide a)
(provide b)
(provide simple-python-lexer)