forked from lives-group/redexPEG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pegGenerator.rkt
349 lines (268 loc) · 9.9 KB
/
pegGenerator.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
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
#lang racket
(require redex)
(require "./peg.rkt")
(define +Gen (make-pseudo-random-generator))
(require rackcheck)
#;(define-metafunction Peg
[(Gpeg natural) peg]
)
;n -> lista de nomes de nao terminais
;L -> lista
;P -> profundidade lista
;Σ -> lista de elementos de um alfabeto
(define (Gpeg n L P Σ)
(if (equal? P 0)
(random 10 +Gen)
(display "lol")
)
)
(define myGen (make-pseudo-random-generator))
;assando a peg pra ficar prontinha!
;Σ -> Terminal alphabet (ex: 0 1 2)
;V -> Non-Terminals list (ex: A B)
;p -> Peg depth
;n -> Number of pegs we want it to generate with SAMPLE
(define (bakePeg Σ V p n)
(define X0 (E0 Σ V))
(display X0)
(display "\n")
(sample (mkPegExpr X0 p) n)
)
;E0: list of terms: (expression, nullable, headset)
;ex:((ε #t ()) (0 #f ()) (1 #f ()) (2 #f ()) (A #t (A)) (B #t (B)))
(define (E0 Σ V)
(append (list (list 'ε #t empty) )
(map (lambda (e) (list e #f empty)) Σ)
(map (lambda (e) (list e (car (sample (gen:one-of (list #t #f)) 1)) (list e) )) V)
)
)
(define (gen:non-ε g)
(gen:filter g (lambda (t) (not (eq? (car t) 'ε) )) ) )
(define (kruskal V return-list rest cont)
(define raiz (list-ref V (random (length V))))
(if (equal? cont 0)
(set! rest (remove raiz rest))
rest)
(display "Raiz: ")
(display raiz)
(display " - ")
(define copy-rest rest)
(set! copy-rest (remove raiz copy-rest))
(display "Rest: ")
(display copy-rest)
(display "\n")
(define tam (length copy-rest))
(if (equal? (length copy-rest) 0)
return-list
(kruskal V
(list return-list (list raiz (list-ref copy-rest (- tam 1))))
(remove (list-ref copy-rest (- tam 1)) rest)
(add1 cont)))
)
;(kruskal '(A B C D) '() '(A B C D) 0)
;Generates Peg
;♣: E0 -> Initial list generated with the function E0
(define (mkPegExpr ♣ p)
(if (= p 0)
(gen:one-of ♣)
(gen:choice (gen:bind (mkPegExpr ♣ (- p 1))
(lambda (x) (gen:bind ( mkPegExpr ♣ (- p 1) )
(lambda (y) (gen:const (mkSeq x y) )) ) ))
(gen:bind (mkPegExpr ♣ (- p 1))
(lambda (x) (gen:bind (mkPegExpr ♣ (- p 1))
(lambda (y) (gen:const (mkAlt x y) )) ) ))
(gen:bind (gen:filter (mkPegExpr ♣ (- p 1)) (lambda (x) (not (cadr x)) ))
(lambda (y) (gen:const (mkKle y) ) ))
(gen:bind (mkPegExpr ♣ (- p 1))
(lambda (y) (gen:const (mkNot y) ) ))
(mkPegExpr ♣ (- p 1))
)
)
)
;Generates peg with •
(define (mkSeq e1 e2)
(if(eq? (car e1) 'ε)
e2
(if (eq? (car e2) 'ε)
e1
(list `(• ,(car e1) ,(car e2)) (and (cadr e1) (cadr e2)) ( if (cadr e1)
(set-union (caddr e1) (caddr e2))
(caddr e1)
) )
)
)
)
;Generates peg with /
(define (mkAlt e1 e2)
(list `(/ ,(car e1) ,(car e2)) (and (cadr e1) (cadr e2)) (set-union (caddr e1) (caddr e2) ) )
)
;Generates peg with *
(define (mkKle e1)
(list `(* ,(car e1) ) #t (caddr e1) )
)
;Generates peg with !
(define (mkNot e1 )
(list `(! ,(car e1) ) #t ( caddr e1 ) )
)
(bakePeg '(0 1 2) '(A B) 2 1)
;NAO FUNCIONA MAS É UMA TENTATIVA
(define (filterGrammar g)
(gen:filter g (lambda (t) (equal? #f (cadr t)))))
(define (filterE0 ♣)
(filter (lambda (e)
(not (cadr e))
)
♣
)
)
(define (En e0 n)
(if (= n 0)
e0
( append (En e0 (- n 1))
(for/list [(ee (En e0 (- n 1)) )]
(append (for/list [(ed (En e0 (- n 1)) )]
(list (mkAlt ee ed) (mkSeq ee ed))
)
(list (mkNot ee) (mkKle ee))
)
)
)
)
)
;(display "funçao do elton\n")
;(En (E0 '(0 1) '(A B)) 1)
;(retorna)
(define (mkGrammar ♣ p)
(if (= p 0)
(gen:one-of (filterE0 ♣))
(gen:choice (gen:bind ( mkGrammar ♣ (- p 1))
(lambda (x) (gen:bind ( mkGrammar ♣ (- p 1) )
(lambda (y) (gen:const (mkSeq x y) )) ) ))
(gen:bind (mkGrammar ♣ (- p 1))
(lambda (x) (gen:bind (mkGrammar ♣ (- p 1))
(lambda (y) (gen:const (mkAlt x y) )) ) ))
(gen:bind (gen:filter (mkGrammar ♣ (- p 1)) (lambda (x) (not (cadr x)) ))
(lambda (y) (gen:const (mkKle y) ) ))
(gen:bind (mkGrammar ♣ (- p 1))
(lambda (y) (gen:const (mkNot y) ) ))
(mkGrammar ♣ (- p 1))
)
)
)
#|
;gerar o par do parsing com o tipo:
;lista de pares (nt, (nullable, prefixo de variaveis))
;definir quantos nao terminais
;pra cada n terminal damos o genPeg
; e -> peg correspondente pra pegar os NT e achar a gramática
;receber a lista de gramática - L
;usar o map pra gerar uma peg pra cada nt
;é pra gerar bem formada já
;usa o is-WF ??
;gerar uma expressão pro nt: verifica se a exp é bem formada (se caso a exp tiver um nt tbm, verifica se o não terminal ja existe.....)
#;(define (genGramar Σ p n L e rep grammar)
(if (and (not (empty? e)) (member (car (list e)) n) )
(genGrammar Σ p n L (rest (list e)) (list rep (car (list e))) (cons grammar (cons (car (list e))
(sample (genPeg Σ (car (sample (gen:one-of '(0 1 2)) 1)) n L) 1))))
(if (empty? e)
"null";;retorna null pq cdr do e fica '()
(genGrammar Σ p n L (rest e) rep grammar)) )
)
#;(define (genGramar Σ p n L e rep grammar)
(map (lambda (i)
(list* i (sample (genPeg Σ p n L) 1))) ;;ta gerando gramatica errada, "fechando os termos"
n)
)
|#
;GERA PEG NO JEITO ANTIGO
;nullable: anulavel ou nao (consome ou nao) proibe de gerar algo nulavel?
;L: lista de nao terminais
(define (genPeg Σ p L nullable)
(if (equal? p 0)
(if nullable
(gen:choice (gen:one-of Σ) (gen:const 'ε))
(gen:one-of Σ))
(gen:choice (gen:bind (genPeg Σ (- p (+ (random p myGen) 1)) L nullable)
(lambda (t) (gen:bind (genPeg Σ (- p (+ (random p myGen) 1) ) L nullable) (lambda (s) (gen:const `(• ,t ,s)) ) ) ) );;colocar condiçao aqui?
(gen:bind (genPeg Σ (- p (+ (random p myGen) 1)) L nullable)
(lambda (t) (gen:bind (genPeg Σ (- p (+ (random p myGen) 1)) L nullable) (lambda (s) (gen:const `(/ ,t ,s))) ) ) )
(gen:bind (genPeg Σ (- p 1) L nullable)
(lambda (t) (gen:const `(! ,t)) ))
(gen:bind (genPeg Σ (- p 1) L #f)
(lambda (t) (gen:const `(* ,t)) ))
(gen:one-of L)
)
)
)
;GERA GRAMÁTICA (ERRADO)
(define (genGrammar Σ p n L e rep grammar)
(if (equal? (length n) 1)
(list (car n) (car (sample (genPeg Σ p n L) 1)) '∅)
(list (car n) (car (sample (genPeg Σ p n L) 1)) (genGrammar Σ p (cdr n) L e rep grammar)))
)
;GERA SÍMBOLOS (X0, X1, X2...)
;n -> numero de NT
(define (genSymbols n)
(define cont (build-list n values))
(map (lambda (i)
(string->symbol (format "X~a" i)))
cont)
)
;GERA O STATE COMPLETO (GRAMÁTICA PEG-ENTRADA ELEMENTO-CONSUMIDO...)
#;(define (genState Σ p n L cont)
(define peg (sample (genPeg Σ p n L) cont))
(define empty-grammar '(∅))
(define grammar (genGrammar Σ p n L (car peg) '() '()))
(define entrada (sample (gen:one-of '(0 1 2 3)) cont))
(if (test-WF (list grammar '⊢ '() (car peg) '↓ entrada '() '⊥ '(0)))
(list grammar '⊢ '() (car peg) '↓ entrada '() '⊥ '(0))
(genState Σ p n L cont))
)
;n -> numero de nao terminal
;L -> lista
;p -> profundidade lista
;Σ -> lista de elementos de um alfabeto
;cont -> deve ser 1
;(sample (gen:one-of '(a b c)) 10)
(display "\nGera Peg\n")
(sample (genPeg '(0 1 2) 3 (genSymbols 3) '()) 1)
#;(cdr (sample (genPeg '(A B C) 1 (genSymbols 3) '()) 1))
#;(display "\nGera Gramática\n")
#;(genGrammar '(0 1 2) 1 (genSymbols 3) '() (sample (genPeg '(0 1 2) 1 (genSymbols 3) '()) 1) '() '())
#;(display "\nGera Estado\n")
#;(genState '(0 1 2) 1 (genSymbols 3) '() 1)
#;(test-WF (genState '(0 1 2) 1 (genSymbols 3) '() 1))
#;(genState '(0 1 2) 1 (genSymbols 3) '() 1)
#;(genState '(0 1 2) 2 (genSymbols 3) '() 1)
#;(genState '(0 1 2) 3 (genSymbols 3) '() 1)
#;(genState '(0 1 2) 3 (genSymbols 3) '() 1)
#|
(define (get-result l)
(if (eq? (list-ref (car l) 7) 'suc)
(list (list-ref (car l) 5))
(list '⊥))
)
(display "\nFunçao setar Peg \n")
(define (setPeg L) ;fica na forma de (grammar exp input)
(define grammar (list-ref L 0))
(define peg (list-ref L 1))
(define entry (list-ref L 2))
(define redct-expr (list grammar '⊢ '() peg '↓ entry '() '⊥ '(0)))
(display redct-expr)
(display "\n")
(define judg-expr (list 'eval grammar (list peg entry) 's))
(display judg-expr)
(display "\n")
(test-equal
(judgment-holds (eval ,grammar (,peg ,entry) s) s)
(get-result (apply-reduction-relation* red redct-expr)))
(test-results)
)
(display "\n \n")
(setPeg '((A (/ 1 2) ∅) (/ A 2) (1 2 3)))
;testar mais a implementação do elton
;verificar o resultado, o tipo
;refazer os testes pra ver se ta tudo certo
;randPeg
;funcao pra verificar se oq saiu do a #f () condiz com a gramatica
|#