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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
; "prec.scm", dynamically extensible parser/tokenizer -*-scheme-*-
; Copyright 1989, 1990, 1991, 1992, 1993, 1995, 1997 Aubrey Jaffer
;
;Permission to copy this software, to modify it, to redistribute it,
;to distribute modified versions, and to use it for any purpose is
;granted, subject to the following restrictions and understandings.
;
;1. Any copy made of this software must include this copyright notice
;in full.
;
;2. I have made no warranty or representation that the operation of
;this software will be error-free, and I am under no obligation to
;provide any services, by way of maintenance, update, or otherwise.
;
;3. In conjunction with products arising from the use of this
;material, there shall be no use of my name in any advertising,
;promotional, or sales literature without prior written consent in
;each case.
; This file implements:
; * a Pratt style parser.
; * a tokenizer which congeals tokens according to assigned classes of
; constituent characters.
;
; This module is a significant improvement because grammar can be
; changed dynamically from rulesets which don't need compilation.
; Theoretically, all possibilities of bad input are handled and return
; as much structure as was parsed when the error occured; The symbol
; `?' is substituted for missing input.
; References for the parser are:
; Pratt, V. R.
; Top Down Operator Precendence.
; SIGACT/SIGPLAN
; Symposium on Principles of Programming Languages,
; Boston, 1973, 41-51
; WORKING PAPER 121
; CGOL - an Alternative External Representation For LISP users
; Vaughan R. Pratt
; MIT Artificial Intelligence Lab.
; March 1976
; Mathlab Group,
; MACSYMA Reference Manual, Version Ten,
; Laboratory for Computer Science, MIT, 1983
(require 'string-search)
(require 'string-port)
(require 'delay)
(require 'multiarg-apply)
;@
(define *syn-defs* #f)
(define (tok:peek-char dyn) (peek-char (cadr dyn)))
(define (tok:read-char dyn)
(let ((c (read-char (cadr dyn))))
(set-car! (cddddr dyn)
(if (or (eqv? c #\newline) (eof-object? c))
0
(+ 1 (car (cddddr dyn)))))
c))
(define (prec:warn dyn . msgs)
(do ((j (+ -1 (car (cddddr dyn))) (+ -8 j)))
((> 8 j)
(do ((i j (+ -1 i)))
((>= 0 i)
(display "^ ")
(newline)
(for-each (lambda (x) (write x) (display #\space)) msgs)
(newline))
(display #\space)))
(display slib:tab)))
;; Structure of lexical records.
(define tok:make-rec cons)
(define tok:cc car)
(define tok:sfp cdr)
(define (tok:lookup alist char)
(if (eof-object? char)
#f
(let ((pair (assv char alist)))
(and pair (cdr pair)))))
;@
(define (tok:char-group group chars chars-proc)
(map (lambda (token)
;;; (let ((oldlexrec (tok:lookup *syn-defs* token)))
;;; (cond ((or (not oldlexrec) (eqv? (tok:cc oldlexrec) group)))
;;; (else (math:warn 'cc-of token 'redefined-to- group))))
(cons token (tok:make-rec group chars-proc)))
(cond ((string? chars) (string->list chars))
((char? chars) (list chars))
(else chars))))
(define (tokenize dyn)
(let* ((char (tok:read-char dyn))
(rec (tok:lookup (car dyn) char))
(proc (and rec (tok:cc rec)))
(clist (list char)))
(cond
((not proc) char)
((procedure? proc)
(do ((cl clist (begin (set-cdr! cl (list (tok:read-char dyn))) (cdr cl))))
((proc (tok:peek-char dyn))
((or (tok:sfp rec) (lambda (dyn l) (list->string l)))
dyn
clist))))
((eqv? 0 proc) (tokenize dyn))
(else
(do ((cl clist (begin (set-cdr! cl (list (tok:read-char dyn))) (cdr cl))))
((not (let* ((prec (tok:lookup (car dyn) (tok:peek-char dyn)))
(cclass (and prec (tok:cc prec))))
(or (eqv? cclass proc)
(eqv? cclass (+ -1 proc)))))
((tok:sfp rec) dyn clist)))))))
;;; PREC:NUD is the null denotation (function and arguments to call when no
;;; unclaimed tokens).
;;; PREC:LED is the left denotation (function and arguments to call when
;;; unclaimed token is on left).
;;; PREC:LBP is the left binding power of this LED. It is the first
;;; argument position of PREC:LED
(define (prec:nudf alist self)
(let ((pair (assoc (cons 'nud self) alist)))
(and pair (cdr pair))))
(define (prec:ledf alist self)
(let ((pair (assoc (cons 'led self) alist)))
(and pair (cdr pair))))
(define (prec:lbp alist self)
(let ((pair (assoc (cons 'led self) alist)))
(and pair (cadr pair))))
(define (prec:call-or-list proc . args)
(prec:apply-or-cons proc args))
(define (prec:apply-or-cons proc args)
(if (procedure? proc) (apply proc args) (cons (or proc '?) args)))
;;; PREC:SYMBOLFY and PREC:DE-SYMBOLFY are not exact inverses.
(define (prec:symbolfy obj)
(cond ((symbol? obj) obj)
((string? obj) (string->symbol obj))
((char? obj) (string->symbol (string obj)))
(else obj)))
(define (prec:de-symbolfy obj)
(cond ((symbol? obj) (symbol->string obj))
(else obj)))
;;;Calls to set up tables.
;@
(define (prec:define-grammar . synlsts)
(set! *syn-defs* (append (apply append synlsts) *syn-defs*)))
;@
(define (prec:make-led toks . args)
(map (lambda (tok)
(cons (cons 'led (prec:de-symbolfy tok))
args))
(if (pair? toks) toks (list toks))))
(define (prec:make-nud toks . args)
(map (lambda (tok)
(cons (cons 'nud (prec:de-symbolfy tok))
args))
(if (pair? toks) toks (list toks))))
;;; Produce dynamically augmented grammars.
(define prec:process-binds append)
;;(define (prec:replace-rules) some-sort-of-magic-cookie)
;;; Here are the procedures to define high-level grammar, along with
;;; utility functions called during parsing. The utility functions
;;; (prec:parse-*) could be incorportated into the defining commands,
;;; but tracing these functions is useful for debugging.
;@
(define (prec:delim tk)
(prec:make-led tk 0 #f))
;@
(define (prec:nofix tk sop . binds)
(prec:make-nud tk prec:parse-nofix sop (apply append binds)))
(define (prec:parse-nofix dyn self sop binds)
(let ((dyn (cons (prec:process-binds binds (car dyn)) (cdr dyn))))
(prec:call-or-list (or sop (prec:symbolfy self)))))
;@
(define (prec:prefix tk sop bp . binds)
(prec:make-nud tk prec:parse-prefix sop bp (apply append binds)))
(define (prec:parse-prefix dyn self sop bp binds)
(let ((dyn (cons (prec:process-binds binds (car dyn)) (cdr dyn))))
(prec:call-or-list (or sop (prec:symbolfy self)) (prec:parse1 dyn bp))))
;@
(define (prec:infix tk sop lbp bp . binds)
(prec:make-led tk lbp prec:parse-infix sop bp (apply append binds)))
(define (prec:parse-infix dyn left self lbp sop bp binds)
(let ((dyn (cons (prec:process-binds binds (car dyn)) (cdr dyn))))
(prec:call-or-list (or sop (prec:symbolfy self)) left (prec:parse1 dyn bp))))
;@
(define (prec:nary tk sop bp)
(prec:make-led tk bp prec:parse-nary sop bp))
(define (prec:parse-nary dyn left self lbp sop bp)
(prec:apply-or-cons (or sop (prec:symbolfy self))
(cons left (prec:parse-list dyn self bp))))
;@
(define (prec:postfix tk sop lbp . binds)
(prec:make-led tk lbp prec:parse-postfix sop (apply append binds)))
(define (prec:parse-postfix dyn left self lbp sop binds)
(let ((dyn (cons (prec:process-binds binds (car dyn)) (cdr dyn))))
(prec:call-or-list (or sop (prec:symbolfy self)) left)))
;@
(define (prec:prestfix tk sop bp . binds)
(prec:make-nud tk prec:parse-rest sop bp (apply append binds)))
(define (prec:parse-rest dyn self sop bp binds)
(let ((dyn (cons (prec:process-binds binds (car dyn)) (cdr dyn))))
(prec:apply-or-cons (or sop (prec:symbolfy self)) (prec:parse-list dyn #f bp))))
;@
(define (prec:commentfix tk stp match . binds)
(append
(prec:make-nud tk prec:parse-nudcomment stp match (apply append binds))
(prec:make-led tk 220 prec:parse-ledcomment stp match (apply append binds))))
(define (prec:parse-nudcomment dyn self stp match binds)
(let ((dyn (cons (prec:process-binds binds (car dyn)) (cdr dyn))))
(tok:read-through-comment dyn stp match)
(prec:advance dyn)
(cond ((prec:delim? dyn (force (caddr dyn))) #f)
(else (prec:parse1 dyn (cadddr dyn))))))
(define (prec:parse-ledcomment dyn left lbp self stp match binds)
(let ((dyn (cons (prec:process-binds binds (car dyn)) (cdr dyn))))
(tok:read-through-comment dyn stp match)
(prec:advance dyn)
left))
(define (tok:read-through-comment dyn stp match)
(set! match (if (char? match)
(string match)
(prec:de-symbolfy match)))
(cond ((procedure? stp)
(let* ((len #f)
(str (call-with-output-string
(lambda (sp)
(set! len (find-string-from-port?
match (cadr dyn)
(lambda (c) (display c sp) #f)))))))
(stp (and len (substring str 0 (- len (string-length match)))))))
(else (find-string-from-port? match (cadr dyn)))))
;@
(define (prec:matchfix tk sop sep match . binds)
(define sep-lbp 0)
(prec:make-nud tk prec:parse-matchfix
sop sep-lbp sep match
(apply append (prec:delim match) binds)))
(define (prec:parse-matchfix dyn self sop sep-lbp sep match binds)
(let ((dyn (cons (prec:process-binds binds (car dyn)) (cdr dyn))))
(cond (sop (prec:apply-or-cons sop (prec:parse-delimited dyn sep sep-lbp match)))
((equal? (force (caddr dyn)) match)
(prec:warn dyn 'expression-missing)
(prec:advance dyn)
'?)
(else (let ((ans (prec:parse1 dyn 0))) ;just parenthesized expression
(cond ((equal? (force (caddr dyn)) match)
(prec:advance dyn))
((prec:delim? dyn (force (caddr dyn)))
(prec:warn dyn 'mismatched-delimiter (force (caddr dyn))
'not match)
(prec:advance dyn))
(else (prec:warn dyn 'delimiter-expected--ignoring-rest
(force (caddr dyn)) 'expected match
'or-delimiter)
(do () ((prec:delim? dyn (force (caddr dyn))))
(prec:parse1 dyn 0))))
ans)))))
;@
(define (prec:inmatchfix tk sop sep match lbp . binds)
(define sep-lbp 0)
(prec:make-led tk lbp prec:parse-inmatchfix
sop sep-lbp sep match
(apply append (prec:delim match) binds)))
(define (prec:parse-inmatchfix dyn left self lbp sop sep-lbp sep match binds)
(let ((dyn (cons (prec:process-binds binds (car dyn)) (cdr dyn))))
(prec:apply-or-cons sop (cons left (prec:parse-delimited dyn sep sep-lbp match)))))
;;;; Here is the code which actually parses.
(define (prec:advance dyn)
(set-car! (cddr dyn) (delay (tokenize dyn))))
(define (prec:advance-return-last dyn)
(let ((last (and (caddr dyn) (force (caddr dyn)))))
(prec:advance dyn)
last))
(define (prec:nudcall dyn self)
(let ((pob (prec:nudf (car dyn) self)))
(cond
(pob (let ((proc (car pob)))
(cond ((procedure? proc) (apply proc dyn self (cdr pob)))
(proc (cons proc (cdr pob)))
(else '?))))
((char? self) (prec:warn dyn 'extra-separator)
(prec:advance dyn)
(prec:nudcall dyn (force (caddr dyn))))
((string? self) (string->symbol self))
(else self))))
(define (prec:ledcall dyn left self)
(let* ((pob (prec:ledf (car dyn) self)))
(apply (cadr pob) dyn left self (cdr pob))))
;;; PREC:PARSE1 is the heart.
(define (prec:parse1 dyn bp)
(do ((left (prec:nudcall dyn (prec:advance-return-last dyn))
(prec:ledcall dyn left (prec:advance-return-last dyn))))
((or (>= bp 200) ;to avoid unneccesary lookahead
(>= bp (or (prec:lbp (car dyn) (force (caddr dyn))) 0))
(not left))
left)))
(define (prec:delim? dyn token)
(or (eof-object? token) (<= (or (prec:lbp (car dyn) token) 220) 0)))
(define (prec:parse-list dyn sep bp)
(cond ((prec:delim? dyn (force (caddr dyn)))
(prec:warn dyn 'expression-missing)
'(?))
(else
(let ((f (prec:parse1 dyn bp)))
(cons f (cond ((equal? (force (caddr dyn)) sep)
(prec:advance dyn)
(cond ((equal? (force (caddr dyn)) sep)
(prec:warn dyn 'expression-missing)
(prec:advance dyn)
(cons '? (prec:parse-list dyn sep bp)))
((prec:delim? dyn (force (caddr dyn)))
(prec:warn dyn 'expression-missing)
'(?))
(else (prec:parse-list dyn sep bp))))
((prec:delim? dyn (force (caddr dyn))) '())
((not sep) (prec:parse-list dyn sep bp))
((prec:delim? dyn sep) (prec:warn dyn 'separator-missing)
(prec:parse-list dyn sep bp))
(else '())))))))
(define (prec:parse-delimited dyn sep bp delim)
(cond ((equal? (force (caddr dyn)) sep)
(prec:warn dyn 'expression-missing)
(prec:advance dyn)
(cons '? (prec:parse-delimited dyn sep bp delim)))
((prec:delim? dyn (force (caddr dyn)))
(if (not (equal? (force (caddr dyn)) delim))
(prec:warn dyn 'mismatched-delimiter (force (caddr dyn))
'expected delim))
(if (not sep) (prec:warn dyn 'expression-missing))
(prec:advance dyn)
(if sep '() '(?)))
(else (let ((ans (prec:parse-list dyn sep bp)))
(cond ((equal? (force (caddr dyn)) delim))
((prec:delim? dyn (force (caddr dyn)))
(prec:warn dyn 'mismatched-delimiter (force (caddr dyn))
'expecting delim))
(else (prec:warn dyn 'delimiter-expected--ignoring-rest
(force (caddr dyn)) '...)
(do () ((prec:delim? dyn (force (caddr dyn))))
(prec:parse1 dyn bp))))
(prec:advance dyn)
ans))))
;@
(define (prec:parse grammar delim column . ports)
(define port (if (null? ports) (current-input-port) (car ports)))
(set! delim (prec:de-symbolfy delim))
(let ((dyn (list (append (prec:delim delim) grammar)
port
#f
0
column)))
(prec:advance dyn) ; setup prec:token with first token
(cond ((eof-object? (force (caddr dyn))) (force (caddr dyn)))
((equal? (force (caddr dyn)) delim) #f)
(else
(let ((ans (prec:parse1 dyn 0)))
(cond ((eof-object? (force (caddr dyn))))
((equal? (force (caddr dyn)) delim))
(else (prec:warn dyn
'delimiter-expected--ignoring-rest
(force (caddr dyn)) 'not delim)
(do () ((or (equal? (force (caddr dyn)) delim)
(eof-object? (force (caddr dyn)))))
(prec:advance dyn))))
ans)))))
;@
(define tok:decimal-digits "0123456789")
(define tok:upper-case "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
(define tok:lower-case "abcdefghijklmnopqrstuvwxyz")
(define tok:whitespaces
(do ((i (+ -1 (min 256 char-code-limit)) (+ -1 i))
(ws "" (if (char-whitespace? (integer->char i))
(string-append ws (string (integer->char i)))
ws)))
((negative? i) ws)))
;;;;The parse tables.
;;; Definitions accumulate in top-level variable *SYN-DEFS*.
(set! *syn-defs* '()) ;Make sure *SYN-DEFS* is empty.
;;; Ignore Whitespace characters.
(prec:define-grammar (tok:char-group 0 tok:whitespaces #f))
;;; On MS-DOS systems, <ctrl>-Z (26) needs to be ignored in order to
;;; avoid problems at end of files.
(case (software-type)
((ms-dos)
(if (not (char-whitespace? (integer->char 26)))
(prec:define-grammar (tok:char-group 0 (integer->char 26) #f))
)))
;;;@ Save these convenient definitions.
(define *syn-ignore-whitespace* *syn-defs*)
(set! *syn-defs* '())
;;(begin (trace-all "prec.scm") (set! *qp-width* 333))
;;(pretty-print (grammar-read-tab (get-grammar 'standard)))
;;(prec:trace)
|