-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.lisp
302 lines (253 loc) · 12.6 KB
/
lexer.lisp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
;;;; -*- Mode: LISP, Syntax: COMMON-LISP, Package: MONTY.PARSER -*-
;;;;
;;;; Author: Gautham Ganapathy (gautham@lisphacker.org)
;;;;
;;;; TPTP language lexer.
(in-package :cl-tptp-parser.parser)
(defclass filepos ()
((file :initform ""
:initarg :file
:accessor pos-file
:documentation "File name")
(line :initform -1
:initarg :line
:accessor pos-line
:documentation "Line number")
(col :initform -1
:initarg :col
:accessor pos-col
:documentation "Column number"))
(:documentation "Position of a token in a file"))
(defun make-pos (file line col)
(make-instance 'filepos
:file file
:line line
:col col))
(defclass token ()
((terminal :initform nil
:initarg :terminal
:accessor token-terminal
:documentation "Token terminal")
(text :initform ""
:initarg :text
:accessor token-text
:documentation "Contained text")
(value :initform nil
:initarg :value
:accessor token-value
:documentation "Numeric value")
(pos :initform nil
:initarg :pos
:accessor token-pos
:documentation "Position in file"))
(:documentation "Token"))
(defun make-token (terminal text pos)
(make-instance 'token
:terminal terminal
:text text
:pos pos))
(defun make-token-with-value (terminal value text pos)
(make-instance 'token
:terminal terminal
:value value
:text text
:pos pos))
(defmethod print-object ((tkn token) stream)
(format stream "<Token ~s ~s @ ~a ~s ~s>"
(token-terminal tkn) (token-text tkn)
(pos-file (token-pos tkn)) (pos-line (token-pos tkn)) (pos-col (token-pos tkn))))
(defmacro define-keyword-lookup-table (keywords)
(let ((set-code (append '(progn) (loop for keyword in keywords
collecting (let ((sym (read-from-string keyword)))
`(setf (gethash ,keyword table) ',sym))))))
`(let ((table (make-hash-table :test 'equal)))
,set-code
table)))
(defparameter *keywords* (define-keyword-lookup-table (;; Formula types
"thf" "tff" "fof" "cnf" "tpi"
;; Formula roles
"axiom" "hypothesis" "lemma" "conjecture"
;; System
"include"
;; Boolean
"$true" "$false"
"$distinct"
;; Arithmetic
"$int" "$rat" "$real"
"$less" "$lesseq" "$greater" "$greatereq"
"$uminus" "$sum" "$difference" "$product"
"$quotient_e" "$quotient_t" "$quotient_f"
"$remainder_e" "$remainder_t" "$remainder_f"
"$floor" "$ceiling" "$truncate" "$round"
"$is_int" "$is_rat" "$to_int" "$to_rat" "$to_real")))
(defparameter *delimiters-operators* (stable-sort '(;; Punctuation
(|(| 1 (#\())
(|)| 1 (#\)))
(|,| 1 (#\,))
(|.| 1 (#\.))
(|[| 1 (#\[))
(|]| 1 (#\]))
(|:| 1 (#\:))
;; Operators
(|!| 1 (#\!))
(|?| 1 (#\?))
(|~| 1 (#\~))
(|\|| 1 (#\|))
(|&| 1 (#\&))
(|<=>| 3 (#\< #\= #\>))
(|<=| 2 (#\< #\=))
(|=>| 2 (#\= #\>))
(|>=| 2 (#\> #\=))
(|<~>| 3 (#\< #\~ #\>))
(|~\|| 2 (#\~ #\|)) ;; Watch for this
(|~&| 2 (#\~ #\&))
(|*| 1 (#\*))
(|+| 1 (#\+))
(|-->| 3 (#\- #\- #\>))
;; Predicates
(|=| 1 (#\=))
(|!=| 2 (#\! #\=)))
(lambda (x y) (>= (cadr x) (cadr y)))))
(defmacro match-regex (regex)
`(multiple-value-bind (s e) (cl-ppcre:scan ,regex line)
(if (and (not (null s)) (zerop s))
(progn
(setf start s
end e
token-text (subseq line start end)
token-len (- end start)
line (subseq line end))
t)
nil)))
(defun read-delim-op (line pos)
(let ((delim-op-list *delimiters-operators*)
(token nil)
(len nil))
(loop while (and (not token) delim-op-list) do
(let* ((row (car delim-op-list))
(terminal (car row))
(len1 (cadr row))
(chars (caddr row))
(c0 (car chars))
(c1 (if (>= len1 2) (cadr chars) #\space))
(c2 (if (>= len1 3) (caddr chars) #\space))
(len2 (length line))
(t0 (char line 0))
(t1 (if (>= len2 2) (char line 1) #\space))
(t2 (if (>= len2 3) (char line 2) #\space)))
(if (or (and (= len1 1) (>= len2 1) (char= c0 t0))
(and (= len1 2) (>= len2 2) (char= c0 t0) (char= c1 t1))
(and (= len1 3) (>= len2 3) (char= c0 t0) (char= c1 t1) (char= c2 t2)))
(setf token (make-token terminal (subseq line 0 len1) pos)
len len1))
(setf delim-op-list (cdr delim-op-list))))
(values token len)))
(defmacro match-sym ()
`(multiple-value-bind (tk l) (read-delim-op line (make-pos filepath line-num col))
(when tk
(setf token tk
token-len l
line (subseq line l)))
tk))
(defun tokenize-stream (code-stream &optional (filepath ""))
(let ((line-num 1)
(tokens '()))
;; Iterate over lines
(loop as line = (read-line code-stream nil nil)
while line do
(let ((col 0)
start end token-text token token-len)
(loop while (not (string= line "")) do
(cond
;; Match whitespace.
((match-regex "[ \t]+"))
;; Match single-line comments.
((match-regex "%.*$"))
;; Match multi-line comments.
((and (>= (length line) 2) (equal (subseq line 0 2) "/*"))
(let ((end-found nil))
(loop while (not end-found) do
(loop while (and (not (string= line "")) (not end-found)) do
(if (and (>= (length line) 2) (equal (subseq line 0 2) "*/"))
(setf end-found t)
(setf col (1+ col)
line (subseq line 1))))
(when (not end-found)
(setf line (read-line code-stream nil nil)
col 0
line-num (1+ line-num))))
(setf line (subseq line 2)
token-len 2)))
#|
((match-regex "/\\*")
(format t "[ ~a ] [ ~a ]~%" token-text line))
|#
;; Upper word
((match-regex "[A-Z][a-zA-Z0-9_]*")
(let ((kw-sym (gethash token-text *keywords*)))
(if kw-sym
(push (make-token kw-sym token-text (make-pos filepath line-num col)) tokens)
(push (make-token-with-value 'UPPER-WORD token-text token-text
(make-pos filepath line-num col)) tokens))))
;; Lower word - no dollar
((match-regex "[a-z][a-zA-Z0-9_]*")
(let ((kw-sym (gethash token-text *keywords*)))
(if kw-sym
(push (make-token kw-sym token-text (make-pos filepath line-num col)) tokens)
(push (make-token-with-value 'LOWER-WORD token-text token-text
(make-pos filepath line-num col)) tokens))))
;; Dollar-preefixed keyword
((match-regex "\\$[a-z][a-zA-Z0-9_]*")
(let ((kw-sym (gethash token-text *keywords*)))
(push (make-token kw-sym token-text (make-pos filepath line-num col)) tokens)))
;; Numbers
((or
(match-regex "0")
(match-regex "[1-9][0-9]*")
(match-regex "[0-9]+\\.[0-9]+(e[+-][0-9]+)?")
(match-regex "\\.[0-9]+(e[+-][0-9]+)?"))
(push (make-token-with-value 'NUMBER (read-from-string token-text) token-text
(make-pos filepath line-num col)) tokens))
;; Match string
((match-regex "'(?:[^'\\\\]|\\\\.)*'")
;;((match-regex "'[^']*'")
(push (make-token-with-value 'STRING (subseq token-text 1 (1- end)) token-text
(make-pos filepath line-num col)) tokens))
;; Symbols / operators / delimiters.
((match-sym)
(push token tokens))
;; Unmatched
(t (error "Lexer error at ~s" line)))
(setf col (+ col token-len))))
(incf line-num))
(nreverse tokens)))
(defun tokenize-file (filepath)
"Tokenizes a TPTP language source file"
(let ((s (open (merge-pathnames filepath))))
(if s (tokenize-stream s)
nil)))
(defun dump-token-list (tokens)
(when tokens
(loop for token in tokens do
(format t "~s~%" token))))
(defun test0 ()
(dump-token-list (tokenize-file "/home/gautham/work/tptp/TPTP-v6.0.0/Axioms/AGT001+0.ax")))
(defun test1 ()
(dump-token-list (tokenize-file "/home/gautham/work/tptp/TPTP-v6.0.0/Problems/AGT/AGT001+1.p")))
(defun test2 ()
(dump-token-list (tokenize-file "/home/gautham/work/tptp/TPTP-v6.0.0/tptp/AGT/AGT001+1.tptp")))
(defun make-stream-lexer (code-stream)
"Creates a TPTP language lexer to tokenize the code in the given stream"
(let ((token-list (tokenize-stream code-stream)))
;;(dump-token-list token-list)
(lambda ()
(let ((token (pop token-list)))
;;(when token (format t "LEXER returning ~s~%" token))
(if (null token)
(values nil nil)
(values (token-terminal token) token))))))
(defun make-file-lexer (filename)
"Creates a TPTP language lexer to tokenize the specified file"
(let ((s (open (merge-pathnames filename))))
(if s (make-stream-lexer s)
nil)))