-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
4775 lines (4246 loc) · 248 KB
/
index.html
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2024-01-31 Wed 12:29 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Notes</title>
<meta name="author" content="Martijn Voordouw" />
<meta name="generator" content="Org Mode" />
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/bigblow_theme/css/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/bigblow_theme/css/bigblow.css"/>
<link rel="stylesheet" type="text/css" href="https://fniessen.github.io/org-html-themes/src/bigblow_theme/css/hideshow.css"/>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery-ui-1.10.2.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery.localscroll-min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery.scrollTo-1.4.3.1-min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/jquery.zclip.min.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/bigblow.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/bigblow_theme/js/hideshow.js"></script>
<script type="text/javascript" src="https://fniessen.github.io/org-html-themes/src/lib/js/jquery.stickytableheaders.min.js"></script>
<script>
window.MathJax = {
tex: {
ams: {
multlineWidth: '85%'
},
tags: 'ams',
tagSide: 'right',
tagIndent: '.8em'
},
chtml: {
scale: 1.0,
displayAlign: 'center',
displayIndent: '0em'
},
svg: {
scale: 1.0,
displayAlign: 'center',
displayIndent: '0em'
},
output: {
font: 'mathjax-modern',
displayOverflow: 'overflow'
}
};
</script>
<script
id="MathJax-script"
async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
</head>
<body>
<div id="content" class="content">
<h1 class="title">Notes</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org3698752">1. Introduction</a>
<ul>
<li><a href="#org334df26">1.1. Definitions</a></li>
<li><a href="#org0d611a3">1.2. Course</a>
<ul>
<li><a href="#org78c4186">1.2.1. In this course</a></li>
<li><a href="#orgc2f01de">1.2.2. Learning goals</a></li>
</ul>
</li>
<li><a href="#orgbe6d2cb">1.3. Haskell</a></li>
<li><a href="#org582aedb">1.4. Language and sets</a></li>
<li><a href="#org2120b56">1.5. Summary</a></li>
</ul>
</li>
<li><a href="#orgef7506b">2. Grammars and parsing</a>
<ul>
<li><a href="#org84783e0">2.1. Grammar</a>
<ul>
<li><a href="#org462f4dd">2.1.1. Grammar and productions</a></li>
<li><a href="#org6cf7b44">2.1.2. Restricted grammars/context free</a></li>
<li><a href="#org5e63fbd">2.1.3. Examples:</a></li>
<li><a href="#orgfa47dfc">2.1.4. Ambiguity</a></li>
<li><a href="#orga41f385">2.1.5. Grammar transformations</a></li>
</ul>
</li>
<li><a href="#org95c882f">2.2. Parsing</a>
<ul>
<li><a href="#org1f403e3">2.2.1. Parsing problem</a></li>
<li><a href="#org56bcd26">2.2.2. Parse trees in haskell</a></li>
</ul>
</li>
<li><a href="#orge891f86">2.3. Summary</a></li>
</ul>
</li>
<li><a href="#orgc2ab7e4">3. Parser Combinators</a>
<ul>
<li><a href="#orgebd3860">3.1. Parser data type</a></li>
<li><a href="#org400fbf4">3.2. Actual parse data type is slightly different</a></li>
<li><a href="#org0a81d4f">3.3. Implementing <*> and <$></a></li>
<li><a href="#orgd5b1806">3.4. Examples <*> and <$></a></li>
<li><a href="#orgc401f92">3.5. Guard</a></li>
<li><a href="#orgbf4940b">3.6. Choice: <|></a></li>
<li><a href="#orgad540b2">3.7. Longest</a></li>
<li><a href="#org4390aa3">3.8. <$ <* and *></a></li>
<li><a href="#org40cf028">3.9. succeed and epsilon</a></li>
<li><a href="#org420a6ac">3.10. empty</a></li>
<li><a href="#orgc90fadb">3.11. satisfy and symbol</a>
<ul>
<li><a href="#org687d1ad">3.11.1. satify</a></li>
<li><a href="#orgdc3d4d9">3.11.2. symbol</a></li>
</ul>
</li>
<li><a href="#org90d7ff2">3.12. Biased choice: <<|></a></li>
<li><a href="#org041fdee">3.13. Bind: >>=</a></li>
<li><a href="#orge310ba1">3.14. do notation</a></li>
<li><a href="#orgecc9a25">3.15. Applicative functors and monads</a></li>
<li><a href="#org2665396">3.16. option</a></li>
<li><a href="#org531ec6c">3.17. many, some, listOf and greedy</a>
<ul>
<li><a href="#orgf5ca65f">3.17.1. many</a></li>
<li><a href="#org8b625fb">3.17.2. some</a></li>
<li><a href="#org4384121">3.17.3. listOf</a></li>
<li><a href="#orgb348b84">3.17.4. greedy</a></li>
<li><a href="#org6fc35ca">3.17.5. greedy1</a></li>
</ul>
</li>
<li><a href="#orgb647399">3.18. chainl and chainr</a></li>
</ul>
</li>
<li><a href="#org3d4890d">4. Parser design</a>
<ul>
<li><a href="#orgb228a48">4.1. Grammar transformations</a>
<ul>
<li><a href="#orgeab736b">4.1.1. Removing duplicates</a></li>
<li><a href="#orge1a0d0f">4.1.2. Left factoring</a></li>
</ul>
</li>
<li><a href="#org140c96a">4.2. Operators</a>
<ul>
<li><a href="#org78b602b">4.2.1. Parsing associative operators</a></li>
<li><a href="#org541fa3c">4.2.2. Parsing associative operators of different priorities </a></li>
<li><a href="#orgb1ad592">4.2.3. A general operator parser</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org89c9f84">5. Regular Expressions</a>
<ul>
<li><a href="#orgd621153">5.1. A simpler subset of parser combinators</a></li>
<li><a href="#orgdfce288">5.2. Regular Expression</a></li>
<li><a href="#org8904f8f">5.3. Limitations of regular expressions/languages</a></li>
</ul>
</li>
<li><a href="#org27631dc">6. Finite State Machines</a>
<ul>
<li><a href="#orgefd40c3">6.1. Moore Machine</a>
<ul>
<li><a href="#orgeb5c871">6.1.1. Example: moore machine for lamp</a></li>
<li><a href="#org1ba38e0">6.1.2. Advantages of Moore Machines</a></li>
<li><a href="#orgfeced2a">6.1.3. Running Moore Machines</a></li>
</ul>
</li>
<li><a href="#org091688a">6.2. Moore Machines for RegExp Matching</a>
<ul>
<li><a href="#org1624690">6.2.1. Examples</a></li>
<li><a href="#orgef2b9e3">6.2.2. Compiling Regular Expressions to DFA</a></li>
<li><a href="#org0f95758">6.2.3. Regex to Non Deterministic Finite Automaton (NFA)</a></li>
<li><a href="#orga5d045f">6.2.4. Running NFAε</a></li>
<li><a href="#org314ca8a">6.2.5. Performance of the NFA regex</a></li>
<li><a href="#org32cda94">6.2.6. Converting NFAε to DFA</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgb2d9e27">7. Folding</a>
<ul>
<li><a href="#org3249542">7.1. List folding</a></li>
<li><a href="#org424f031">7.2. Matched parentheses</a></li>
<li><a href="#org149455e">7.3. Arithmetic expressions</a></li>
<li><a href="#orga3b57dd">7.4. Building a fold for any datatype</a>
<ul>
<li><a href="#orgc60395e">7.4.1. Trees example</a></li>
</ul>
</li>
<li><a href="#org3ca4b42">7.5. <span class="todo TODO">TODO</span> Fix</a></li>
<li><a href="#orgff06400">7.6. Algebra for families of datatypes</a></li>
<li><a href="#orgebc4ee4">7.7. RepMax fold</a></li>
</ul>
</li>
<li><a href="#org531965f">8. Simple Stack Machine</a>
<ul>
<li><a href="#org55565f6">8.1. Documentation</a></li>
<li><a href="#org1a01c34">8.2. Architecture</a></li>
<li><a href="#org6c794b3">8.3. Instructions</a>
<ul>
<li><a href="#org42d807e">8.3.1. <code>LDC</code> - load constant</a></li>
<li><a href="#org01d762e">8.3.2. <code>LDR</code> - load from register</a></li>
<li><a href="#orga579b7c">8.3.3. <code>LDL</code> - loal local</a></li>
<li><a href="#orgcc57093">8.3.4. <code>LDS</code> - load from stack</a></li>
<li><a href="#org854ce0e">8.3.5. <code>LDLA</code> - load local adress</a></li>
<li><a href="#orgadd13b0">8.3.6. <code>LDA</code> - load via adress</a></li>
<li><a href="#org2c5445a">8.3.7. <code>LDRR</code> - load register from register</a></li>
<li><a href="#org253fee1">8.3.8. <code>NOP</code> - noop</a></li>
<li><a href="#org8a282f2">8.3.9. <code>HALT</code> - halt program</a></li>
<li><a href="#orgffa501a">8.3.10. <code>AJS</code> - adjust stack pointer</a></li>
<li><a href="#orgc5ccd77">8.3.11. <code>BRA</code> - unconditional branch</a></li>
<li><a href="#org5f2207e">8.3.12. <code>BSR</code> - branch to subroutine</a></li>
<li><a href="#orgf87a547">8.3.13. <code>RET</code> - return from subroutine</a></li>
<li><a href="#org9b26fdd">8.3.14. <code>STR</code> - store to register</a></li>
<li><a href="#org725e591">8.3.15. <code>STS</code> - store into stack</a></li>
<li><a href="#org3e230a7">8.3.16. <code>STL</code> - store local</a></li>
<li><a href="#org2e1cff1">8.3.17. Operators</a></li>
</ul>
</li>
<li><a href="#org8f27475">8.4. Translating programs</a>
<ul>
<li><a href="#orge6f7250">8.4.1. Translating expressions</a></li>
<li><a href="#orgab856a9">8.4.2. Statements</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org87df0d5">9. Validation</a>
<ul>
<li><a href="#org509feec">9.1. Example checks at AST level</a></li>
</ul>
</li>
<li><a href="#org7167768">10. Pumping Lemmas, proving (non)regular languages</a>
<ul>
<li><a href="#orge01b8b8">10.1. General strategy for proving a language (non) regular</a></li>
<li><a href="#org6a4e217">10.2. Proving a language non-regular</a>
<ul>
<li><a href="#org28db763">10.2.1. Strategy step 1: limitation in the formalism</a></li>
<li><a href="#org851f637">10.2.2. Step 2: property of language class</a></li>
<li><a href="#org6b66cf7">10.2.3. Step 3: pumping lemma</a></li>
</ul>
</li>
<li><a href="#org4dcadac">10.3. <span class="todo TODO">TODO</span> Proving context-free grammar</a>
<ul>
<li><a href="#org6f73ef9">10.3.1. Step 1: limitation in the formalism</a></li>
<li><a href="#orgba62083">10.3.2. Step 2: property of language class</a></li>
<li><a href="#org6ecad38">10.3.3. Step 3: pumping lemma</a></li>
</ul>
</li>
<li><a href="#orge3e1585">10.4. Normal forms</a>
<ul>
<li><a href="#org171b0be">10.4.1. Chomksy Normal Form</a></li>
<li><a href="#orgd717739">10.4.2. Greibach Normal Form</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org286a50f">11. Nanopass Compilation</a>
<ul>
<li><a href="#org6543bbb">11.1. Nanopass passes</a>
<ul>
<li><a href="#org6a07812">11.1.1. Parse</a></li>
<li><a href="#org3a1cef6">11.1.2. Type-Check</a></li>
<li><a href="#org972bf2c">11.1.3. for → while</a></li>
<li><a href="#org24f9084">11.1.4. λ → class</a></li>
<li><a href="#org5650775">11.1.5. class → struct</a></li>
<li><a href="#org68546ec">11.1.6. Insert Reference-Counting code</a></li>
<li><a href="#org5e0f78a">11.1.7. Constant folding</a></li>
<li><a href="#orgc778cf2">11.1.8. if,while, … → goto</a></li>
<li><a href="#orge31e81e">11.1.9. SSM instructions → x86<sub>64</sub> instructions</a></li>
</ul>
</li>
<li><a href="#orgfba7281">11.2. Nano parse abstract syntax tree?</a>
<ul>
<li><a href="#orgc1cba6b">11.2.1. Many ASTs</a></li>
<li><a href="#orgb13f85a">11.2.2. One AST</a></li>
<li><a href="#org80c5731">11.2.3. Generics</a></li>
<li><a href="#org0fa1756">11.2.4. One AST, with refinements</a></li>
<li><a href="#org68f5b4d">11.2.5. One AST, with parameters</a></li>
<li><a href="#org3033098">11.2.6. <span class="todo TODO">TODO</span> One AST, with parameter + type functions</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgd0d4052">12. Optimizations</a>
<ul>
<li><a href="#orgbef7bed">12.1. Optimization passes</a></li>
<li><a href="#org99a0ef4">12.2. Simple optimizations</a>
<ul>
<li><a href="#org53423eb">12.2.1. Unreachable/dead code elimination:</a></li>
<li><a href="#org2c99213">12.2.2. Tail call elimination:</a></li>
</ul>
</li>
<li><a href="#org066b2ec">12.3. Loop optimization</a>
<ul>
<li><a href="#org57a7975">12.3.1. Loop unrolling</a></li>
<li><a href="#orgc7d5156">12.3.2. Loop invariant code motion</a></li>
<li><a href="#org0068862">12.3.3. Loop fusion</a></li>
<li><a href="#orgff27d28">12.3.4. Loop fission</a></li>
</ul>
</li>
<li><a href="#org83586f1">12.4. Other optimizations</a>
<ul>
<li><a href="#orgae731a2">12.4.1. Inlining</a></li>
<li><a href="#orgc038c22">12.4.2. Common Subexpression Elimination</a></li>
</ul>
</li>
<li><a href="#org389e682">12.5. Compiler pipeline</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-org3698752" class="outline-2">
<h2 id="org3698752"><span class="section-number-2">1.</span> Introduction</h2>
<div class="outline-text-2" id="text-1">
</div>
<div id="outline-container-org334df26" class="outline-3">
<h3 id="org334df26"><span class="section-number-3">1.1.</span> Definitions</h3>
<div class="outline-text-3" id="text-1-1">
<ul class="org-ul">
<li>A <b>language</b> is a set of “correct” sentences</li>
<li>A <b>compiler</b> translates one language into another (possibly the same)</li>
</ul>
<p>
Computer science studies information processing.
</p>
<ul class="org-ul">
<li>We describe and transfer <b>information</b> by means of <b>language</b></li>
<li>Information is obtained by assigning <b>meaning</b> to <b>sentences</b></li>
<li>The <b>meaning</b> of a sentence is inferred from its <b>structure</b></li>
<li>The <b>structure</b> of a sentence is described by means of a <b>grammar</b></li>
</ul>
</div>
</div>
<div id="outline-container-org0d611a3" class="outline-3">
<h3 id="org0d611a3"><span class="section-number-3">1.2.</span> Course</h3>
<div class="outline-text-3" id="text-1-2">
</div>
<div id="outline-container-org78c4186" class="outline-4">
<h4 id="org78c4186"><span class="section-number-4">1.2.1.</span> In this course</h4>
<div class="outline-text-4" id="text-1-2-1">
<ul class="org-ul">
<li>Classes (“difficulty levels”) of languages
<ul class="org-ul">
<li>context-free languages</li>
<li>regular languages</li>
</ul></li>
<li>Describing languages formally, using
<ul class="org-ul">
<li>grammars</li>
<li>finite state automata</li>
</ul></li>
<li>Grammar transformations
<ul class="org-ul">
<li>for simplification</li>
<li>for obtaining more efficient parsers</li>
</ul></li>
<li>Parsing context-free and regular languages, using
<ul class="org-ul">
<li>parser combinators</li>
<li>parser generators</li>
<li>finite state automata</li>
</ul></li>
<li>How to go from syntax to semantics</li>
</ul>
</div>
</div>
<div id="outline-container-orgc2f01de" class="outline-4">
<h4 id="orgc2f01de"><span class="section-number-4">1.2.2.</span> Learning goals</h4>
<div class="outline-text-4" id="text-1-2-2">
<ul class="org-ul">
<li>To describe structures (i.e., “formulas”) using grammars;</li>
<li>To parse, i.e., to recognise (build) such structures in (from) a sequence of symbols;</li>
<li>To analyse grammars to see whether or not specific properties hold;</li>
<li>To compose components such as parsers, analysers, and code generators;</li>
<li>To apply these techniques in the construction of all kinds of programs;</li>
<li>To explain and prove why certain problems can or cannot be described by means of formalisms such as context-free grammars or finite-state automata.</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-orgbe6d2cb" class="outline-3">
<h3 id="orgbe6d2cb"><span class="section-number-3">1.3.</span> Haskell</h3>
<div class="outline-text-3" id="text-1-3">
<p>
Haskell is used because many concept from formal language theory have a direct correspondence in Haskell
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Formal languages</th>
<th scope="col" class="org-left">Haskell</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left">alphabet</td>
<td class="org-left">datatype</td>
</tr>
<tr>
<td class="org-left">sequence</td>
<td class="org-left">list type</td>
</tr>
<tr>
<td class="org-left">sentence/word</td>
<td class="org-left">a concrete list</td>
</tr>
<tr>
<td class="org-left">abstract syntax</td>
<td class="org-left">datatype</td>
</tr>
<tr>
<td class="org-left">grammar</td>
<td class="org-left">parser</td>
</tr>
<tr>
<td class="org-left">grammar transformation</td>
<td class="org-left">parser transformation</td>
</tr>
<tr>
<td class="org-left">parse tree</td>
<td class="org-left">value of abstract syntax type</td>
</tr>
<tr>
<td class="org-left">semantics</td>
<td class="org-left">fold function, algebra</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-org582aedb" class="outline-3">
<h3 id="org582aedb"><span class="section-number-3">1.4.</span> Language and sets</h3>
<div class="outline-text-3" id="text-1-4">
<p>
An <b>alphabet</b> is a set of symbols that can be used to form sentences
</p>
<p>
Given a set A. The set of <b>sequences over A</b>, written A*, is defined as follows:
</p>
<ul class="org-ul">
<li>The empyt sequence \(\epsilon\) is in \(A^*\)</li>
<li>If \(a\in A\) and \(z\in A^*\), then \(az\) is in \(A^*\)</li>
</ul>
<p>
Given an alphabat A, a <b>language</b> is a subset of \(A^*\)
</p>
<p>
We can define such a set in multiple ways:
</p>
<ul class="org-ul">
<li>By enumerating all elements</li>
<li>By using a predicate
<ul class="org-ul">
<li>\(PAL=\{s\in A^*|s=s^R\}\) is the language of palindromes over A</li>
</ul></li>
<li>By giving an inductive definition
<ul class="org-ul">
<li>ε is in PAL,</li>
<li>a, b, c are in PAL,</li>
<li>if P is in PAL, then aPa, bPb and cPc are also in PAL</li>
<li>An inductive definition gives us more structure and makes it easier to explain why a sentence is in the language</li>
</ul></li>
</ul>
</div>
</div>
<div id="outline-container-org2120b56" class="outline-3">
<h3 id="org2120b56"><span class="section-number-3">1.5.</span> Summary</h3>
<div class="outline-text-3" id="text-1-5">
<p>
<b>Alphabet:</b> A finite set of symbols.
</p>
<p>
<b>Language:</b> A set of words/sentences, i.e., sequences of symbols from the alphabet.
</p>
<p>
<b>Grammar:</b> A way to define a language inductively by means of rewrite rules.
</p>
</div>
</div>
</div>
<div id="outline-container-orgef7506b" class="outline-2">
<h2 id="orgef7506b"><span class="section-number-2">2.</span> Grammars and parsing</h2>
<div class="outline-text-2" id="text-2">
</div>
<div id="outline-container-org84783e0" class="outline-3">
<h3 id="org84783e0"><span class="section-number-3">2.1.</span> Grammar</h3>
<div class="outline-text-3" id="text-2-1">
</div>
<div id="outline-container-org462f4dd" class="outline-4">
<h4 id="org462f4dd"><span class="section-number-4">2.1.1.</span> Grammar and productions</h4>
<div class="outline-text-4" id="text-2-1-1">
<p>
A <b>grammar</b> is formalism to describe a language inductively.
Grammer consist of rewrite rules, called productions
<img src="Grammar/2023-11-16_13-27-59_screenshot.png" alt="2023-11-16_13-27-59_screenshot.png" />
</p>
<ul class="org-ul">
<li>A grammar consists of multiple <b>productions</b>. Productions can be seen as rewrite rules.</li>
<li>The grammer makes use of auxiliary symbols, called <b>nonterminals</b>, that are not part of the alphabet and hence cannot be part of the final word/sentence</li>
<li>The symbols from the alphabet are also called <b>terminals</b>.</li>
</ul>
<p>
Grammars can have multiple nonterminal
<img src="Grammar/2023-11-16_13-29-37_screenshot.png" alt="2023-11-16_13-29-37_screenshot.png" />
One nonterminal in the grammar is called the <b>start symbol</b>
</p>
</div>
</div>
<div id="outline-container-org6cf7b44" class="outline-4">
<h4 id="org6cf7b44"><span class="section-number-4">2.1.2.</span> Restricted grammars/context free</h4>
<div class="outline-text-4" id="text-2-1-2">
<p>
We consider only restricted grammars:
</p>
<ul class="org-ul">
<li>The left hand side of a production always consists of a single nonterminal</li>
</ul>
<p>
Grammars with this restriction are called <b>context-free</b>
</p>
<ul class="org-ul">
<li>Not all languages can be generated/described by a grammar.</li>
<li>Multiple grammars may describe the same language.</li>
<li>Grammars which generate the same language are equivalent.</li>
<li>Even fewer languages can be described by a context-free grammar.</li>
<li>Languages that can be described by a context-free grammar are called context-free languages.</li>
<li>Context-free languages are relatively easy to deal with algorithmically, and therefore most programming languages are context-free languages</li>
</ul>
</div>
</div>
<div id="outline-container-org5e63fbd" class="outline-4">
<h4 id="org5e63fbd"><span class="section-number-4">2.1.3.</span> Examples:</h4>
<div class="outline-text-4" id="text-2-1-3">
<p>
natural numbers without leading zeros
</p>
<ul class="org-ul">
<li>Dig-0 → 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9</li>
<li>Nat → 0 | Dig-0 Digs</li>
</ul>
<p>
Integers:
</p>
<ul class="org-ul">
<li>Sign → + | -</li>
<li>Int → Sign Nat | Nat
or..</li>
<li>Int → Sign? Nat</li>
</ul>
<p>
Fragment of C#:
</p>
<ul class="org-ul">
<li>Stat → Var = Expr ;</li>
<li>| if ( Expr ) Stat else Stat</li>
<li>| while ( Expr ) Stat</li>
<li>Expr → Integer</li>
<li>| Var</li>
<li>| Expr Op Expr</li>
<li>Var → Identifier</li>
<li>Op → Sign | *</li>
</ul>
</div>
</div>
<div id="outline-container-orgfa47dfc" class="outline-4">
<h4 id="orgfa47dfc"><span class="section-number-4">2.1.4.</span> Ambiguity</h4>
<div class="outline-text-4" id="text-2-1-4">
<p>
A grammar where every sentence corresponds to a unique parse tree is called <b>unambiguous</b>.
If this is not the case the grammar is called <b>ambiguous</b>.
</p>
<p>
<i>Example ambiguous grammar:</i>
</p>
<ul class="org-ul">
<li>S → SS</li>
<li>S → a</li>
</ul>
<p>
Famous ambiguity problem:
</p>
<ul class="org-ul">
<li>S → if b then S else S</li>
<li>| if b then S</li>
<li>| a</li>
</ul>
<p>
consider:
</p>
<ul class="org-ul">
<li>if b then if b then a else a</li>
</ul>
<p>
Ambiguity is a property of grammars:
</p>
<ul class="org-ul">
<li>All of these grammars describe the same language
<img src="Grammar/2023-11-16_14-02-37_screenshot.png" alt="2023-11-16_14-02-37_screenshot.png" /></li>
<li>Not al of these are ambiguous</li>
</ul>
</div>
</div>
<div id="outline-container-orga41f385" class="outline-4">
<h4 id="orga41f385"><span class="section-number-4">2.1.5.</span> Grammar transformations</h4>
<div class="outline-text-4" id="text-2-1-5">
<p>
A <b>grammar transformation</b> is a mapping from one grammar to another, such that the generated language remains the same.
</p>
<p>
Formally:
A grammar transformation maps a grammer G to another grammar G’ such that:
\(L(G)=L(G')\)
</p>
<p>
Grammar transformations can help us to transform grammars with undesirable properties (such as ambiguity) into grammars with other (hopefully better) properties.
</p>
<p>
Most grammar transformations are motivated by facilitating parsing
</p>
</div>
</div>
</div>
<div id="outline-container-org95c882f" class="outline-3">
<h3 id="org95c882f"><span class="section-number-3">2.2.</span> Parsing</h3>
<div class="outline-text-3" id="text-2-2">
</div>
<div id="outline-container-org1f403e3" class="outline-4">
<h4 id="org1f403e3"><span class="section-number-4">2.2.1.</span> Parsing problem</h4>
<div class="outline-text-4" id="text-2-2-1">
<p>
Given a grammar G and a string s, the <b>parsing problem</b> is to decide wether or not \(s\in L(G)\)
</p>
<p>
Furthermore, if \(s\in L(G)\), we want evidence/proof/an explantion why this is the case, usually in the form of a parse tree.
</p>
</div>
</div>
<div id="outline-container-org56bcd26" class="outline-4">
<h4 id="org56bcd26"><span class="section-number-4">2.2.2.</span> Parse trees in haskell</h4>
<div class="outline-text-4" id="text-2-2-2">
<p>
Consider this grammar:
</p>
<ul class="org-ul">
<li>S → S-D | D</li>
<li>D → 0 | 1</li>
</ul>
<p>
Represent nonterminals as <b>datatypes</b>:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #51afef;">data</span> <span style="color: #ECBE7B;">S</span> <span style="color: #dcaeea;">=</span> <span style="color: #ECBE7B;">Minus</span> <span style="color: #ECBE7B;">S</span> <span style="color: #ECBE7B;">D</span> <span style="color: #dcaeea;">|</span> <span style="color: #ECBE7B;">SingleDigit</span> <span style="color: #ECBE7B;">D</span>
<span style="color: #51afef;">data</span> <span style="color: #ECBE7B;">D</span> <span style="color: #dcaeea;">=</span> <span style="color: #ECBE7B;">Zero</span> <span style="color: #dcaeea;">|</span> <span style="color: #ECBE7B;">One</span>
</pre>
</div>
<p>
The string 1-0-1 corresponds to the parse tree
</p>
<div id="org5d85053" class="figure">
<p><img src="Parsing/2023-11-17_11-50-16_screenshot.png" alt="2023-11-17_11-50-16_screenshot.png" />
</p>
</div>
<p>
In haskell:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #ECBE7B;">Minus</span> (<span style="color: #ECBE7B;">Minus</span> (<span style="color: #ECBE7B;">SingleDigit</span> <span style="color: #ECBE7B;">One</span>) <span style="color: #ECBE7B;">Zero</span>) <span style="color: #ECBE7B;">One</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #c678dd;">printS</span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">S</span> <span style="color: #dcaeea;">→</span> <span style="color: #ECBE7B;">String</span>
<span style="color: #c678dd;">printS</span> (<span style="color: #ECBE7B;">Minus</span> s d) <span style="color: #dcaeea;">=</span> printS s <span style="color: #dcaeea;">++</span> <span style="color: #98be65;">"-"</span> <span style="color: #dcaeea;">++</span> printD d
<span style="color: #c678dd;">printS</span> (<span style="color: #ECBE7B;">SingleDigit</span> d) <span style="color: #dcaeea;">=</span> printD d
<span style="color: #c678dd;">printD</span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">D</span> <span style="color: #dcaeea;">→</span> <span style="color: #ECBE7B;">String</span>
<span style="color: #c678dd;">printD</span> <span style="color: #ECBE7B;">Zero</span> <span style="color: #dcaeea;">=</span> <span style="color: #98be65;">"0"</span>
<span style="color: #c678dd;">printD</span> <span style="color: #ECBE7B;">One</span> <span style="color: #dcaeea;">=</span> <span style="color: #98be65;">"1"</span>
<span style="color: #c678dd;">sample</span> <span style="color: #dcaeea;">=</span> <span style="color: #ECBE7B;">Minus</span> (<span style="color: #ECBE7B;">Minus</span> (<span style="color: #ECBE7B;">SingleDigit</span> <span style="color: #ECBE7B;">One</span>) <span style="color: #ECBE7B;">Zero</span>) <span style="color: #ECBE7B;">One</span>
<span style="color: #c678dd;">main</span> <span style="color: #dcaeea;">=</span> putStrLn (printS sample) <span style="color: #5B6268;">-- </span><span style="color: #5B6268;">"1-0-1"</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orge891f86" class="outline-3">
<h3 id="orge891f86"><span class="section-number-3">2.3.</span> Summary</h3>
<div class="outline-text-3" id="text-2-3">
<p>
<b>Grammar</b> A way to describe a language inductively.
</p>
<p>
<b>Production</b> A rewrite rule in a grammar.
</p>
<p>
<b>Context-free</b> The class of grammars/languages we consider.
</p>
<p>
<b>Nonterminal</b> Auxiliary symbols in a grammar.
</p>
<p>
<b>Terminal</b> Alphabet symbols in a grammar.
</p>
<p>
<b>Derivation</b> Successively rewriting from a grammar until we reach a sentence.
</p>
<p>
<b>Parse</b> tree Tree representation of a derivation.
</p>
<p>
<b>Ambiguity</b> Multiple parse trees for the same sentence.
</p>
<p>
<b>Abstract</b> syntax (Haskell) Datatype corresponding to a grammar.
</p>
<p>
<b>Semantic</b> function Function defined on the abstract syntax.
</p>
</div>
</div>
</div>
<div id="outline-container-orgc2ab7e4" class="outline-2">
<h2 id="orgc2ab7e4"><span class="section-number-2">3.</span> Parser Combinators</h2>
<div class="outline-text-2" id="text-3">
</div>
<div id="outline-container-orgebd3860" class="outline-3">
<h3 id="orgebd3860"><span class="section-number-3">3.1.</span> Parser data type</h3>
<div class="outline-text-3" id="text-3-1">
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #c678dd;">parseDate5</span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> <span style="color: #ECBE7B;">Date</span>
<span style="color: #c678dd;">parseMonth5</span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> <span style="color: #ECBE7B;">Month</span>
<span style="color: #c678dd;">parseDay5</span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> <span style="color: #ECBE7B;">Day</span>
<span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">Parser</span> a <span style="color: #dcaeea;">=</span> <span style="color: #ECBE7B;">String</span> <span style="color: #dcaeea;">-></span> [(a,<span style="color: #ECBE7B;">String</span>)]
</pre>
</div>
<p>
Defining a parser could look like this:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #c678dd;">parseDate5</span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> <span style="color: #ECBE7B;">Date</span>
<span style="color: #c678dd;">parseDate5</span> input <span style="color: #dcaeea;">=</span> [(<span style="color: #ECBE7B;">Date</span> d m,tail')
<span style="color: #dcaeea;">|</span> (d,tail ) <span style="color: #dcaeea;"><-</span> parseDay5 input
, (m,tail') <span style="color: #dcaeea;"><-</span> parseMonth5 tail]
</pre>
</div>
<p>
This is a repetitive pattern, and quite error prone.
</p>
<p>
We want it to look like this:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #c678dd;">parseDate6</span> <span style="color: #dcaeea;">=</span> <span style="color: #ECBE7B;">Date</span> <span style="color: #dcaeea;"><$></span> parseDay <span style="color: #dcaeea;"><*></span> parseMonth
</pre>
</div>
<p>
Notice this is similar to regular haskell function application, <$> -> $ and <*> -> .
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #dcaeea;"><$></span> <span style="color: #dcaeea;">::</span> (<span style="color: #ECBE7B;">Int</span> <span style="color: #dcaeea;">-></span> (<span style="color: #ECBE7B;">Month</span> <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Date</span>))
<span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> <span style="color: #ECBE7B;">Int</span>
<span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> (<span style="color: #ECBE7B;">Month</span> <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Date</span>)
<span style="color: #dcaeea;"><*></span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> (<span style="color: #ECBE7B;">Month</span> <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Date</span>)
<span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> <span style="color: #ECBE7B;">Month</span>
<span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> <span style="color: #ECBE7B;">Date</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org400fbf4" class="outline-3">
<h3 id="org400fbf4"><span class="section-number-3">3.2.</span> Actual parse data type is slightly different</h3>
<div class="outline-text-3" id="text-3-2">
<p>
The actual type also has what type of symbol we are trying to parse, usually char.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #51afef;">type</span> <span style="color: #ECBE7B;">Parser</span> a c <span style="color: #dcaeea;">=</span> [c] <span style="color: #dcaeea;">-></span> [(a,[c])]
(<span style="color: #c678dd;"><*></span>) <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> s (a <span style="color: #dcaeea;">-></span> b) <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> s a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> s b
(<span style="color: #c678dd;"><|></span>) <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> s a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> s a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> s a
(<span style="color: #c678dd;"><$></span>) <span style="color: #dcaeea;">::</span> (a <span style="color: #dcaeea;">-></span> b) <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> b
</pre>
</div>
<p>
Using the parser
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #c678dd;">parse</span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> s a <span style="color: #dcaeea;">→</span> [s] <span style="color: #dcaeea;">→</span> [(a, [s])]
<span style="color: #5B6268;">-- </span><span style="color: #5B6268;">Examples:</span>
<span style="color: #c678dd;">parse</span> ints <span style="color: #98be65;">"23,11"</span> <span style="color: #dcaeea;">==</span> [((<span style="color: #da8548; font-weight: bold;">23</span>, <span style="color: #da8548; font-weight: bold;">11</span>), <span style="color: #98be65;">""</span>)]
<span style="color: #c678dd;">parse</span> ints <span style="color: #98be65;">"23,11bla"</span> <span style="color: #dcaeea;">==</span> [((<span style="color: #da8548; font-weight: bold;">23</span>, <span style="color: #da8548; font-weight: bold;">11</span>), <span style="color: #98be65;">"bla"</span>)]
<span style="color: #c678dd;">parse</span> ints <span style="color: #98be65;">"whatever"</span> <span style="color: #dcaeea;">==</span> <span style="color: #ECBE7B;">[]</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org0a81d4f" class="outline-3">
<h3 id="org0a81d4f"><span class="section-number-3">3.3.</span> Implementing <*> and <$></h3>
<div class="outline-text-3" id="text-3-3">
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #dcaeea;"><$></span> <span style="color: #dcaeea;">::</span> (a <span style="color: #dcaeea;">-></span> b) <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> b
(f <span style="color: #dcaeea;"><$></span> parse) input <span style="color: #dcaeea;">=</span> [ (f x, tail)
<span style="color: #dcaeea;">|</span> (x, tail) <span style="color: #dcaeea;"><-</span> parse input]
</pre>
</div>
<p>
Examples
</p>
<div class="org-src-container">
<pre class="src src-haskell">((<span style="color: #da8548; font-weight: bold;">1</span><span style="color: #dcaeea;">+</span>) <span style="color: #dcaeea;"><$></span> parseNat) <span style="color: #98be65;">"100"</span> <span style="color: #dcaeea;">==</span> [(<span style="color: #da8548; font-weight: bold;">101</span>,<span style="color: #98be65;">""</span>)]
(map toUpper <span style="color: #dcaeea;"><$></span> parseString <span style="color: #98be65;">"hello"</span>) <span style="color: #98be65;">"hello world"</span> <span style="color: #dcaeea;">==</span> [(<span style="color: #98be65;">"HELLO"</span>,<span style="color: #98be65;">" world"</span>)]
</pre>
</div>
<p>
Ussually this isn’t used directly, more often then not combined with <*>
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #dcaeea;"><*></span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> (a <span style="color: #dcaeea;">-></span> b) <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> b
(pf <span style="color: #dcaeea;"><*></span> px) input <span style="color: #dcaeea;">=</span> [ (f x, tail1)
<span style="color: #dcaeea;">|</span> (f, tail1) <span style="color: #dcaeea;"><-</span> pf input
, (x, tail2) <span style="color: #dcaeea;"><-</span> px tail1]
</pre>
</div>
</div>
</div>
<div id="outline-container-orgd5b1806" class="outline-3">
<h3 id="orgd5b1806"><span class="section-number-3">3.4.</span> Examples <*> and <$></h3>
<div class="outline-text-3" id="text-3-4">
<p>
Examples:
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #ECBE7B;">(,)</span> <span style="color: #dcaeea;"><$></span> parseNat <span style="color: #dcaeea;"><*></span> parseString <span style="color: #98be65;">" green bottles"</span> <span style="color: #dcaeea;">$</span> <span style="color: #98be65;">"42 green bottles hanging on the wall"</span>
<span style="color: #dcaeea;">==</span> [((<span style="color: #da8548; font-weight: bold;">42</span>,<span style="color: #98be65;">" green bottles"</span>),<span style="color: #98be65;">" hanging on the wall"</span>)]
fst <span style="color: #c678dd;"><$></span> (<span style="color: #ECBE7B;">(,)</span> <span style="color: #dcaeea;"><$></span> parseNat <span style="color: #dcaeea;"><*></span> parseString <span style="color: #98be65;">" green bott "</span> <span style="color: #da8548; font-weight: bold;">42</span> green bottles hanging on the wall<span style="color: #ECBE7B;">"</span>
<span style="color: #dcaeea;">==</span> [(<span style="color: #da8548; font-weight: bold;">42</span>,<span style="color: #98be65;">" hanging on the wall"</span>)
</pre>
</div>
</div>
</div>
<div id="outline-container-orgc401f92" class="outline-3">
<h3 id="orgc401f92"><span class="section-number-3">3.5.</span> Guard</h3>
<div class="outline-text-3" id="text-3-5">
<p>
Only succeed if the result of a parser satisfys a given predicate
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #c678dd;">guard</span> <span style="color: #dcaeea;">::</span> (a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Bool</span>) <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a
<span style="color: #c678dd;">guard</span> cond parser input <span style="color: #dcaeea;">=</span> [ (result, tail)
<span style="color: #dcaeea;">|</span> (result, tail) <span style="color: #dcaeea;"><-</span> parser input
, cond result]
</pre>
</div>
<p>
Can also be defined using >>= (see further ahead for more details)
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #c678dd;">guard</span> <span style="color: #dcaeea;">::</span> (a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Bool</span>) <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a
<span style="color: #c678dd;">guard</span> cond parser <span style="color: #dcaeea;">=</span> parser <span style="color: #dcaeea;">>>=</span> <span style="color: #dcaeea;">\</span>a <span style="color: #dcaeea;">-></span>
<span style="color: #51afef;">if</span> cond a <span style="color: #51afef;">then</span> succeed a <span style="color: #51afef;">else</span> empty
</pre>
</div>
</div>
</div>
<div id="outline-container-orgbf4940b" class="outline-3">
<h3 id="orgbf4940b"><span class="section-number-3">3.6.</span> Choice: <|></h3>
<div class="outline-text-3" id="text-3-6">
<p>
Parses using either or both parsers
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #dcaeea;"><|></span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a
(p1 <span style="color: #dcaeea;"><|></span> p2) input <span style="color: #dcaeea;">=</span> p1 input <span style="color: #dcaeea;">++</span> p2 input
</pre>
</div>
<p>
choice takes a list of parsers and combines them in sequence, returning a list of results.
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #c678dd;">choice</span> <span style="color: #dcaeea;">::</span> [<span style="color: #ECBE7B;">Parser</span> s a] <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> s a
<span style="color: #c678dd;">choice</span> <span style="color: #dcaeea;">=</span> foldr (<span style="color: #dcaeea;"><|></span>) empty
</pre>
</div>
</div>
</div>
<div id="outline-container-orgad540b2" class="outline-3">
<h3 id="orgad540b2"><span class="section-number-3">3.7.</span> Longest</h3>
<div class="outline-text-3" id="text-3-7">
<p>
This function isn’t actually in library, but could still be a usefull example for a low level parser
</p>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #c678dd;">longest</span> <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a
<span style="color: #c678dd;">longest</span> parser input
<span style="color: #dcaeea;">=</span> concat
<span style="color: #dcaeea;">.</span> take <span style="color: #da8548; font-weight: bold;">1</span>
<span style="color: #dcaeea;">.</span> groupBy ((<span style="color: #dcaeea;">==</span>) <span style="color: #dcaeea;">`on`</span> length <span style="color: #dcaeea;">.</span> snd)
<span style="color: #dcaeea;">.</span> sortOn (length <span style="color: #dcaeea;">.</span> snd)
<span style="color: #dcaeea;">.</span> parser
<span style="color: #dcaeea;">$</span> input
</pre>
</div>
</div>
</div>
<div id="outline-container-org4390aa3" class="outline-3">
<h3 id="org4390aa3"><span class="section-number-3">3.8.</span> <$ <* and *></h3>
<div class="outline-text-3" id="text-3-8">
<p>
All of these are made for ignoring the result of a parser
</p>
<ul class="org-ul">
<li>Basically only use the argument if the parser succeeds</li>
</ul>
<div class="org-src-container">
<pre class="src src-haskell"><span style="color: #dcaeea;"><$</span> <span style="color: #dcaeea;">::</span> a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> b <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> a
(x <span style="color: #dcaeea;"><$</span> p) <span style="color: #dcaeea;">=</span> const x <span style="color: #dcaeea;"><$></span> p
</pre>
</div>
<div class="org-src-container">
<pre class="src src-haskell">(<span style="color: #c678dd;"><*</span>) <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> s a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> s b <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> s a
p <span style="color: #c678dd;"><*</span> q <span style="color: #dcaeea;">=</span> const <span style="color: #dcaeea;"><$></span> p <span style="color: #dcaeea;"><*></span> q
</pre>
</div>
<div class="org-src-container">
<pre class="src src-haskell">(<span style="color: #c678dd;">*></span>) <span style="color: #dcaeea;">::</span> <span style="color: #ECBE7B;">Parser</span> s a <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> s b <span style="color: #dcaeea;">-></span> <span style="color: #ECBE7B;">Parser</span> s b
p <span style="color: #c678dd;">*></span> q <span style="color: #dcaeea;">=</span> flip const <span style="color: #dcaeea;"><$></span> p <span style="color: #dcaeea;"><*></span> q
</pre>
</div>
</div>
</div>
<div id="outline-container-org40cf028" class="outline-3">
<h3 id="org40cf028"><span class="section-number-3">3.9.</span> succeed and epsilon</h3>
<div class="outline-text-3" id="text-3-9">
<p>
Creates a parser that always results in the same value, doesn’t consume anything from the input string
</p>
<div class="org-src-container">