-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdcl.lhs
More file actions
506 lines (440 loc) · 21.1 KB
/
dcl.lhs
File metadata and controls
506 lines (440 loc) · 21.1 KB
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
~~ Lexer for Toki Pona ~~
Authors: Jesse Frohlich & Andrew Wilson
License: GPL3
< import Data.List.Split
> import Data.Char
> import Data.Maybe
> import Control.Monad
> import Data.List
> import Data.Tree as T
> import System.Environment
A rough classification of words in Toki Pona;
< data Lexeme = Content String -- Noun, verb, modifier
< | Borrow String -- (always modifiers)
< | Preposition String -- e, tawa, kepeken, lon, poka, &c.
< | Seperator String -- ",":".":"pi":[]:li
< | Nonsense String -- to collect errors
< deriving (Show)
Lexer based on the lex function. This splits the string into words and
punctuation, each as their own string. Note that lex is designed to parse
haskell source code; the fact that it works for us is luck, and if something
goes wrong, it may not be a bad idea to switch to lexer' below.
> lexerSymbol :: String -> [Symbol]
> lexerSymbol [] = []
> lexerSymbol s = Terminal n : lexerSymbol s'
> where [(n,s')] = lex s
> lexerTree :: String -> [SyntaxTree]
> lexerTree = map Spoon . lexerSymbol
Before discovering the lex function in Data.Char, we developed the function
lexer':
< cutAt :: Char -> String -> [String]
< cutAt a s = splitOn [a] s
<
< trimSeperator :: String -> [String]
< trimSeperator s = if not . isAlpha $ c
< then init s : [[c]]
< else [s]
< where c = last s
<
< lexer' :: String -> [String]
< lexer' = join . (map trimSeperator) . (cutAt ' ')
Context Free Grammar sturcture. Should contain nonterminals, terminals,
production rules, and a start symbol.
< data Tree node leaf = Fork node (Tree node leaf) (Tree node leaf)
< | Spoon leaf
< deriving (Eq,Show) -- Write custom show later
Tree data structure
> data SubBinaryTree a = Spoon a
> | Knife a (SubBinaryTree a)
> | Fork a (SubBinaryTree a) (SubBinaryTree a)
> deriving (Show)
>{-TODO: Implement Data.Tree's drawTree-}
> getHead :: SubBinaryTree a -> a
> getHead (Spoon x) = x
> getHead (Knife x _) = x
> getHead (Fork x _ _) = x
> type SyntaxTree = SubBinaryTree Symbol
> isStart :: SyntaxTree -> Bool
> isStart (Fork Start _ _) = True
> isStart _ = False
In Data.Tree, there exists an algorithm to draw a tree. For the sake of time, instead of using their tree data structure, what follows is a conversion from SubBinaryTree to T.Tree:
> fromSubBinaryTree :: SubBinaryTree Symbol -> T.Tree Symbol
> fromSubBinaryTree (Spoon x) = Node x []
> fromSubBinaryTree (Knife x t) = Node x [fromSubBinaryTree t]
> fromSubBinaryTree (Fork x t1 (Fork PiPhrase t2 t3))
> = Node x [fromSubBinaryTree t1
> ,fromSubBinaryTree t2
> ,fromSubBinaryTree t3
> ]
> fromSubBinaryTree (Fork x t1 t2) = Node x [fromSubBinaryTree t1
> ,fromSubBinaryTree t2
> ]
>{- > data Terminal = -}
> data Symbol = Start
> | Content
> | SubjectPhrase
> | Predicate
> | Preposition
> | PrepositionPhrase
> | PrepositionCluster
> -- | VerbPhrase
> | Li -- Only produces: Terminal "li"
> | Pi -- Only produces: Terminal "pi"
> | PiPhrase -- Only produces: (
> | Terminal String
> deriving (Eq)
> -- | Terminal String
> -- | Missing: taso as a particle, la as a context delimiter
> instance Show Symbol where
> show Start = "Start"
> show Content = "Content"
> show SubjectPhrase = "Subject Phrase"
> show Predicate = "Predicate"
> show Preposition = "Preposition"
> show PrepositionPhrase = "Preposition Phrase"
> show PrepositionCluster = "Preposition Cluster"
> show Li = "li" -- Only produces: Terminal "li"
> show Pi = "pi" -- Only produces: Terminal "pi"
> show PiPhrase = "pi Phrase" -- Only produces: (
> show (Terminal s) = s
< data Terminal = Terminal String deriving (Show,Eq)
Production rules:
> type RuleName = String
> data ProductionRule = ProductionRule Symbol
> (Either (Symbol,Symbol) Symbol)
> deriving (Show)
Playground
> grammar =
> [ProductionRule Start $ Left (SubjectPhrase,Predicate)
> ,ProductionRule SubjectPhrase $ Right (Terminal "mi")
> ,ProductionRule SubjectPhrase $ Right (Terminal "sina")
> ,ProductionRule SubjectPhrase $ Left (Content,Li)
> ,ProductionRule Li $ Right (Terminal "li")
> ,ProductionRule Pi $ Right (Terminal "pi")
Content -> Content "pi" Content
> ,ProductionRule Content $ Left (Content,Content)
> ,ProductionRule Content $ Left (Content,PiPhrase)
> ,ProductionRule PiPhrase $ Left (Pi,Content)
> ,ProductionRule Predicate $ Left (Content,Content)
> ,ProductionRule Predicate $ Left (Content,PrepositionCluster)
> ,ProductionRule PrepositionCluster $ Left
> (PrepositionPhrase,PrepositionCluster)
> ,ProductionRule PrepositionCluster $ Left
> (PrepositionPhrase,PrepositionCluster)
PrepositionCluster -> PrepositionPhrase
> ,ProductionRule PrepositionCluster $ Left (Preposition,Content)
> ,ProductionRule PrepositionPhrase $ Left (Preposition,Content)
Prepositions
> ,ProductionRule Preposition $ Right (Terminal "e")
> ,ProductionRule Preposition $ Right (Terminal "kepeken")
> ,ProductionRule Preposition $ Right (Terminal "lon")
> ,ProductionRule Preposition $ Right (Terminal "tawa")
> ,ProductionRule Preposition $ Right (Terminal "tan")
> ,ProductionRule Preposition $ Right (Terminal "sama")
> ,ProductionRule Preposition $ Right (Terminal "poka")
Content Words
> ,ProductionRule Content $ Right (Terminal "akesi")
> ,ProductionRule Predicate $ Right (Terminal "akesi")
> ,ProductionRule Content $ Right (Terminal "ala")
> ,ProductionRule Predicate $ Right (Terminal "ala")
> ,ProductionRule Content $ Right (Terminal "alasa")
> ,ProductionRule Predicate $ Right (Terminal "alasa")
> ,ProductionRule Content $ Right (Terminal "ali")
> ,ProductionRule Predicate $ Right (Terminal "ali")
> ,ProductionRule Content $ Right (Terminal "ale")
> ,ProductionRule Predicate $ Right (Terminal "ale")
> ,ProductionRule Content $ Right (Terminal "anpa")
> ,ProductionRule Predicate $ Right (Terminal "anpa")
> ,ProductionRule Content $ Right (Terminal "ante")
> ,ProductionRule Predicate $ Right (Terminal "ante")
> ,ProductionRule Content $ Right (Terminal "awen")
> ,ProductionRule Predicate $ Right (Terminal "awen")
> ,ProductionRule Content $ Right (Terminal "esun")
> ,ProductionRule Predicate $ Right (Terminal "esun")
> ,ProductionRule Content $ Right (Terminal "ijo")
> ,ProductionRule Predicate $ Right (Terminal "ijo")
> ,ProductionRule Content $ Right (Terminal "ike")
> ,ProductionRule Predicate $ Right (Terminal "ike")
> ,ProductionRule Content $ Right (Terminal "ilo")
> ,ProductionRule Predicate $ Right (Terminal "ilo")
> ,ProductionRule Content $ Right (Terminal "insa")
> ,ProductionRule Predicate $ Right (Terminal "insa")
> ,ProductionRule Content $ Right (Terminal "jaki")
> ,ProductionRule Predicate $ Right (Terminal "jaki")
> ,ProductionRule Content $ Right (Terminal "jan")
> ,ProductionRule Predicate $ Right (Terminal "jan")
> ,ProductionRule Content $ Right (Terminal "jelo")
> ,ProductionRule Predicate $ Right (Terminal "jelo")
> ,ProductionRule Content $ Right (Terminal "jo")
> ,ProductionRule Predicate $ Right (Terminal "jo")
> ,ProductionRule Content $ Right (Terminal "kala")
> ,ProductionRule Predicate $ Right (Terminal "kala")
> ,ProductionRule Content $ Right (Terminal "kalama")
> ,ProductionRule Predicate $ Right (Terminal "kalama")
> ,ProductionRule Content $ Right (Terminal "kama")
> ,ProductionRule Predicate $ Right (Terminal "kama")
> ,ProductionRule Content $ Right (Terminal "kasi")
> ,ProductionRule Predicate $ Right (Terminal "kasi")
> ,ProductionRule Content $ Right (Terminal "ken")
> ,ProductionRule Predicate $ Right (Terminal "ken")
> ,ProductionRule Content $ Right (Terminal "kili")
> ,ProductionRule Predicate $ Right (Terminal "kili")
> ,ProductionRule Content $ Right (Terminal "kiwen")
> ,ProductionRule Predicate $ Right (Terminal "kiwen")
> ,ProductionRule Content $ Right (Terminal "ko")
> ,ProductionRule Predicate $ Right (Terminal "ko")
> ,ProductionRule Content $ Right (Terminal "kon")
> ,ProductionRule Predicate $ Right (Terminal "kon")
> ,ProductionRule Content $ Right (Terminal "kule")
> ,ProductionRule Predicate $ Right (Terminal "kule")
> ,ProductionRule Content $ Right (Terminal "kulupu")
> ,ProductionRule Predicate $ Right (Terminal "kulupu")
> ,ProductionRule Content $ Right (Terminal "kute")
> ,ProductionRule Predicate $ Right (Terminal "kute")
> ,ProductionRule Content $ Right (Terminal "lape")
> ,ProductionRule Predicate $ Right (Terminal "lape")
> ,ProductionRule Content $ Right (Terminal "laso")
> ,ProductionRule Predicate $ Right (Terminal "laso")
> ,ProductionRule Content $ Right (Terminal "lawa")
> ,ProductionRule Predicate $ Right (Terminal "lawa")
> ,ProductionRule Content $ Right (Terminal "len")
> ,ProductionRule Predicate $ Right (Terminal "len")
> ,ProductionRule Content $ Right (Terminal "lete")
> ,ProductionRule Predicate $ Right (Terminal "lete")
> ,ProductionRule Content $ Right (Terminal "lili")
> ,ProductionRule Predicate $ Right (Terminal "lili")
> ,ProductionRule Content $ Right (Terminal "linja")
> ,ProductionRule Predicate $ Right (Terminal "linja")
> ,ProductionRule Content $ Right (Terminal "lipu")
> ,ProductionRule Predicate $ Right (Terminal "lipu")
> ,ProductionRule Content $ Right (Terminal "loje")
> ,ProductionRule Predicate $ Right (Terminal "loje")
> ,ProductionRule Content $ Right (Terminal "luka")
> ,ProductionRule Predicate $ Right (Terminal "luka")
> ,ProductionRule Content $ Right (Terminal "oko")
> ,ProductionRule Predicate $ Right (Terminal "oko")
> ,ProductionRule Content $ Right (Terminal "lukin")
> ,ProductionRule Predicate $ Right (Terminal "lukin")
> ,ProductionRule Content $ Right (Terminal "lupa")
> ,ProductionRule Predicate $ Right (Terminal "lupa")
> ,ProductionRule Content $ Right (Terminal "ma")
> ,ProductionRule Predicate $ Right (Terminal "ma")
> ,ProductionRule Content $ Right (Terminal "mama")
> ,ProductionRule Predicate $ Right (Terminal "mama")
> ,ProductionRule Content $ Right (Terminal "mani")
> ,ProductionRule Predicate $ Right (Terminal "mani")
> ,ProductionRule Content $ Right (Terminal "meli")
> ,ProductionRule Predicate $ Right (Terminal "meli")
> ,ProductionRule Content $ Right (Terminal "mi")
> ,ProductionRule Predicate $ Right (Terminal "mi")
> ,ProductionRule Content $ Right (Terminal "mije")
> ,ProductionRule Predicate $ Right (Terminal "mije")
> ,ProductionRule Content $ Right (Terminal "moku")
> ,ProductionRule Predicate $ Right (Terminal "moku")
> ,ProductionRule Content $ Right (Terminal "moli")
> ,ProductionRule Predicate $ Right (Terminal "moli")
> ,ProductionRule Content $ Right (Terminal "monsi")
> ,ProductionRule Predicate $ Right (Terminal "monsi")
> ,ProductionRule Content $ Right (Terminal "mun")
> ,ProductionRule Predicate $ Right (Terminal "mun")
> ,ProductionRule Content $ Right (Terminal "musi")
> ,ProductionRule Predicate $ Right (Terminal "musi")
> ,ProductionRule Content $ Right (Terminal "mute")
> ,ProductionRule Predicate $ Right (Terminal "mute")
> ,ProductionRule Content $ Right (Terminal "nanpa")
> ,ProductionRule Predicate $ Right (Terminal "nanpa")
> ,ProductionRule Content $ Right (Terminal "nasa")
> ,ProductionRule Predicate $ Right (Terminal "nasa")
> ,ProductionRule Content $ Right (Terminal "nasin")
> ,ProductionRule Predicate $ Right (Terminal "nasin")
> ,ProductionRule Content $ Right (Terminal "nena")
> ,ProductionRule Predicate $ Right (Terminal "nena")
> ,ProductionRule Content $ Right (Terminal "ni")
> ,ProductionRule Predicate $ Right (Terminal "ni")
> ,ProductionRule Content $ Right (Terminal "nimi")
> ,ProductionRule Predicate $ Right (Terminal "nimi")
> ,ProductionRule Content $ Right (Terminal "noka")
> ,ProductionRule Predicate $ Right (Terminal "noka")
> ,ProductionRule Content $ Right (Terminal "olin")
> ,ProductionRule Predicate $ Right (Terminal "olin")
> ,ProductionRule Content $ Right (Terminal "ona")
> ,ProductionRule Predicate $ Right (Terminal "ona")
> ,ProductionRule Content $ Right (Terminal "open")
> ,ProductionRule Predicate $ Right (Terminal "open")
> ,ProductionRule Content $ Right (Terminal "pakala")
> ,ProductionRule Predicate $ Right (Terminal "pakala")
> ,ProductionRule Content $ Right (Terminal "pali")
> ,ProductionRule Predicate $ Right (Terminal "pali")
> ,ProductionRule Content $ Right (Terminal "palisa")
> ,ProductionRule Predicate $ Right (Terminal "palisa")
> ,ProductionRule Content $ Right (Terminal "pan")
> ,ProductionRule Predicate $ Right (Terminal "pan")
> ,ProductionRule Content $ Right (Terminal "pana")
> ,ProductionRule Predicate $ Right (Terminal "pana")
> ,ProductionRule Content $ Right (Terminal "pilin")
> ,ProductionRule Predicate $ Right (Terminal "pilin")
> ,ProductionRule Content $ Right (Terminal "pimeja")
> ,ProductionRule Predicate $ Right (Terminal "pimeja")
> ,ProductionRule Content $ Right (Terminal "pini")
> ,ProductionRule Predicate $ Right (Terminal "pini")
> ,ProductionRule Content $ Right (Terminal "pipi")
> ,ProductionRule Predicate $ Right (Terminal "pipi")
> ,ProductionRule Content $ Right (Terminal "poka")
> ,ProductionRule Predicate $ Right (Terminal "poka")
> ,ProductionRule Content $ Right (Terminal "poki")
> ,ProductionRule Predicate $ Right (Terminal "poki")
> ,ProductionRule Content $ Right (Terminal "pona")
> ,ProductionRule Predicate $ Right (Terminal "pona")
> ,ProductionRule Content $ Right (Terminal "pu")
> ,ProductionRule Predicate $ Right (Terminal "pu")
> ,ProductionRule Content $ Right (Terminal "sama")
> ,ProductionRule Predicate $ Right (Terminal "sama")
> ,ProductionRule Content $ Right (Terminal "seli")
> ,ProductionRule Predicate $ Right (Terminal "seli")
> ,ProductionRule Content $ Right (Terminal "selo")
> ,ProductionRule Predicate $ Right (Terminal "selo")
> ,ProductionRule Content $ Right (Terminal "sewi")
> ,ProductionRule Predicate $ Right (Terminal "sewi")
> ,ProductionRule Content $ Right (Terminal "sijelo")
> ,ProductionRule Predicate $ Right (Terminal "sijelo")
> ,ProductionRule Content $ Right (Terminal "sike")
> ,ProductionRule Predicate $ Right (Terminal "sike")
> ,ProductionRule Content $ Right (Terminal "sin")
> ,ProductionRule Predicate $ Right (Terminal "sin")
> ,ProductionRule Content $ Right (Terminal "namako")
> ,ProductionRule Predicate $ Right (Terminal "namako")
> ,ProductionRule Content $ Right (Terminal "sina")
> ,ProductionRule Predicate $ Right (Terminal "sina")
> ,ProductionRule Content $ Right (Terminal "sinpin")
> ,ProductionRule Predicate $ Right (Terminal "sinpin")
> ,ProductionRule Content $ Right (Terminal "sitelen")
> ,ProductionRule Predicate $ Right (Terminal "sitelen")
> ,ProductionRule Content $ Right (Terminal "sona")
> ,ProductionRule Predicate $ Right (Terminal "sona")
> ,ProductionRule Content $ Right (Terminal "soweli")
> ,ProductionRule Predicate $ Right (Terminal "soweli")
> ,ProductionRule Content $ Right (Terminal "suli")
> ,ProductionRule Predicate $ Right (Terminal "suli")
> ,ProductionRule Content $ Right (Terminal "suno")
> ,ProductionRule Predicate $ Right (Terminal "suno")
> ,ProductionRule Content $ Right (Terminal "supa")
> ,ProductionRule Predicate $ Right (Terminal "supa")
> ,ProductionRule Content $ Right (Terminal "suwi")
> ,ProductionRule Predicate $ Right (Terminal "suwi")
> ,ProductionRule Content $ Right (Terminal "taso")
> ,ProductionRule Predicate $ Right (Terminal "taso")
> ,ProductionRule Content $ Right (Terminal "tawa")
> ,ProductionRule Predicate $ Right (Terminal "tawa")
> ,ProductionRule Content $ Right (Terminal "telo")
> ,ProductionRule Predicate $ Right (Terminal "telo")
> ,ProductionRule Content $ Right (Terminal "tenpo")
> ,ProductionRule Predicate $ Right (Terminal "tenpo")
> ,ProductionRule Content $ Right (Terminal "toki")
> ,ProductionRule Predicate $ Right (Terminal "toki")
> ,ProductionRule Content $ Right (Terminal "tomo")
> ,ProductionRule Predicate $ Right (Terminal "tomo")
> ,ProductionRule Content $ Right (Terminal "tu")
> ,ProductionRule Predicate $ Right (Terminal "tu")
> ,ProductionRule Content $ Right (Terminal "unpa")
> ,ProductionRule Predicate $ Right (Terminal "unpa")
> ,ProductionRule Content $ Right (Terminal "uta")
> ,ProductionRule Predicate $ Right (Terminal "uta")
> ,ProductionRule Content $ Right (Terminal "utala")
> ,ProductionRule Predicate $ Right (Terminal "utala")
> ,ProductionRule Content $ Right (Terminal "walo")
> ,ProductionRule Predicate $ Right (Terminal "walo")
> ,ProductionRule Content $ Right (Terminal "wan")
> ,ProductionRule Predicate $ Right (Terminal "wan")
> ,ProductionRule Content $ Right (Terminal "waso")
> ,ProductionRule Predicate $ Right (Terminal "waso")
> ,ProductionRule Content $ Right (Terminal "wawa")
> ,ProductionRule Predicate $ Right (Terminal "wawa")
> ,ProductionRule Content $ Right (Terminal "weka")
> ,ProductionRule Predicate $ Right (Terminal "weka")
> ,ProductionRule Content $ Right (Terminal "wile")
> ,ProductionRule Predicate $ Right (Terminal "wile")
> ]
> type Grammar = [ProductionRule]
Given a symbol or a pair of symbols (this should eventually become a *list* of
symbols in for non-CNF grammars), determine whether the given production rule
can produce the symbol(s).
> checkRuleSymbol::Either (Symbol,Symbol) Symbol -> ProductionRule -> Maybe Symbol
> checkRuleSymbol s (ProductionRule n t) = if s == t then Just n else Nothing
> checkRuleTree::Either(SyntaxTree,SyntaxTree)SyntaxTree->ProductionRule -> Maybe SyntaxTree
> checkRuleTree (Left (x,y)) (ProductionRule s (Left (u,v))) =
> if getHead x == u && getHead y == v
> then
> Just $ Fork s x y
> else
> Nothing
> checkRuleTree (Right x) (ProductionRule s (Right u)) =
> if getHead x == u
> then
> Just $ Knife s x
> else
> Nothing
> checkRuleTree _ _ = Nothing
Given a grammar, send a symbol to all symbols which connect to it via a
production rule. This is relevant for the terminal production rules.
< zerothparse :: Grammar -> Symbol -> [Symbol]
< zerothparse g s = mapMaybe (checkRule $ Right s) g
> zerothparse :: Grammar -> (Either (a,a) a -> ProductionRule -> Maybe a)
> -> a -> [a]
> zerothparse g f s = mapMaybe (f $ Right s) g
< firstparse :: Grammar -> [Symbol] -> [[Symbol]] -- List of lists
< firstparse g = map $ zerothparse g
Given a symbol, produce a list of lists
> firstparse :: Grammar -> (Either (a,a) a -> ProductionRule -> Maybe a)
> -> [a] -> [[a]]
> firstparse g f = map $ zerothparse g f
Given a grammar and list of symbols produced from the lexer, produce the
triangular array of the CYK algorithm.
> secondparse :: Grammar -> (Either (a,a) a-> ProductionRule -> Maybe a)
> -> (a,a) -> [a]
> secondparse g f s = mapMaybe (f $ Left s) g
> getPairs :: [a] -> [a] -> [(a,a)]
> getPairs u v = [(x,y) | x<-u, y<-v]
> nextLevel :: Grammar -> (Either (a,a) a -> ProductionRule -> Maybe a)
> -> [[[a]]] -> [[a]]
> nextLevel g f t =
> if length (last t) == 1
> then []
> else
> l : nextLevel g f (map tail t)
> where
> l = concat . concat $
> map (\i->(map $ secondparse g f) $
> getPairs (t !! i !! 0) (t !! (n-i) !! (1+i))) [0..n]
> n = length t - 1
> parse :: Grammar -> (Either (a,a) a -> ProductionRule -> Maybe a)
> -> [a] -> [[[a]]] -- "Array" of lists
> parse g f ss = p:ps
> where
> p = firstparse g f ss
> ps = take (length ss -1) $ map (nextLevel g f . (p:)) $ inits ps
> isGrammatical :: Grammar -> [Symbol] -> Bool
> isGrammatical g = (Start `elem`) . head . last . parse g checkRuleSymbol
Playground:
> getSyntaxTree :: String -> [SyntaxTree]
> getSyntaxTree s = filter isStart $ head $ last $
> parse grammar checkRuleTree $ lexerTree s
> drawSyntaxTree :: SyntaxTree -> String
> drawSyntaxTree t = map repl $ T.drawTree $ fmap show (fromSubBinaryTree t)
> where repl '|' = '│'
> repl '+' = '├'
> repl '`' = '└'
> repl '-' = '─'
> repl c = c
> main = do
> x:xs <- getArgs
> putStrLn $ unlines $ map drawSyntaxTree $ getSyntaxTree x
> s = "mi wile moku"
> p = lexerSymbol s
> p' = lexerTree s
> o = parse grammar checkRuleSymbol p
> o' = parse grammar checkRuleTree p'
< -- n = firstparse grammar p
< n = [[SubjectPhrase],[Predicate]]
< m = nextLevel grammar checkRuleSymbol [n]
< s = "mi wile e ni: mi kama sona e jan pi lon ni."
< p = lexer s