-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser-generator.el
2287 lines (2071 loc) · 78.1 KB
/
parser-generator.el
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
;;; parser-generator.el --- Parser Generator library -*- lexical-binding: t -*-
;; Copyright (C) 2020-2024 Free Software Foundation, Inc.
;; Author: Christian Johansson <[email protected]>
;; Maintainer: Christian Johansson <[email protected]>
;; Created: 10 Oct 2020
;; Modified: 20 Dec 2024
;; Version: 0.2.4
;; Keywords: tools, convenience
;; URL: https://github.com/cjohansson/emacs-parser-generator
;; Package-Requires: ((emacs "26"))
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; The idea of this plugin is to provide functions for various kinds of context-free grammar parser generations with support for syntax-directed-translations (SDT) and semantic actions (SA) and the possibility of exporting parsers and translators (as generated stand-alone elisp code) to enable Emacs plugin-agnostic usage.
;;; Code:
(require 'subr-x)
;;; Variables:
(defvar
parser-generator--context-sensitive-attributes
nil
"List of valid context-sensitive attributes.")
(defvar
parser-generator--debug
nil
"Whether to print debug messages or not.")
(defvar
parser-generator--e-identifier
'e
"The identifier used for ε-symbol. Default value \='e.")
(defvar
parser-generator--eof-identifier
'$
"The identifier used for end of file identifier. Default value is \='$.")
(defvar
parser-generator--global-attributes
nil
"List of valid global attributes.")
(defvar
parser-generator--global-declaration
nil
"Global declaration for grammar.")
(defvar
parser-generator--grammar
nil
"Current grammar used in parser.")
(defvar
parser-generator--f-sets
nil
"Generated F-sets for grammar.")
(defvar
parser-generator--look-ahead-number
nil
"Current look-ahead number used.")
(defvar
parser-generator--table-context-sensitive-attributes-p
nil
"Hash-table of context-sensitive-attributes.")
(defvar
parser-generator--table-global-attributes-p
nil
"Hash-table of global-attributes.")
(defvar
parser-generator--table-look-aheads-p
nil
"Hash-table of look-aheads for quick checking.")
(defvar
parser-generator--table-non-terminal-p
nil
"Hash-table of terminals for quick checking.")
(defvar
parser-generator--table-productions-rhs
nil
"Hash-table of productions RHS indexed by LHS for quick retrieving.")
(defvar
parser-generator--table-productions-number
nil
"Hash-table indexed by production and value is production-number.")
(defvar
parser-generator--table-productions-number-reverse
nil
"Hash-table indexed by production-number and value is production.")
(defvar
parser-generator--table-terminal-p
nil
"Hash-table of non-terminals for quick checking.")
(defvar
parser-generator--table-translations
nil
"Hash-table indexed by production-number and value is translation function.")
(defvar
parser-generator--table-productions-attributes
nil
"Hash-table of attributes related to productions.")
(defvar
parser-generator--last-grammar-error
nil
"Error message of last grammar.")
;; Macros
(defmacro parser-generator--debug (&rest message)
"Output MESSAGE but only if debug is enabled."
`(when parser-generator--debug
,@message))
;; Helper Functions
(defun parser-generator--clear-cache ()
"Clear cache."
(setq
parser-generator--f-sets
nil))
(defun parser-generator--distinct (elements)
"Return distinct of ELEMENTS."
(let ((processed (make-hash-table :test 'equal))
(new-elements))
(dolist (element elements)
(unless (gethash element processed)
(puthash element t processed)
(push element new-elements)))
(nreverse
new-elements)))
(defun parser-generator--generate-list-of-symbol (k symbol)
"Generate list of K number of SYMBOL."
(let ((list-index 0)
(list))
(while (< list-index k)
(push symbol list)
(setq list-index (1+ list-index)))
list))
(defun parser-generator--get-grammar-look-aheads ()
"Return all possible look-ahead set."
(unless parser-generator--look-ahead-number
(error "No look-ahead number defined!"))
(let ((terminals
(parser-generator--get-grammar-terminals))
(look-aheads)
(k (max
1
parser-generator--look-ahead-number))
(stack '((0 0 nil)))
(marked-paths (make-hash-table :test 'equal))
(added-look-aheads (make-hash-table :test 'equal)))
(let ((terminals-max-index
(1- (length terminals)))
(terminal-index)
(look-ahead-length)
(look-ahead)
(eof-list
(parser-generator--generate-list-of-symbol
k
parser-generator--eof-identifier)))
(while stack
(let ((item (pop stack)))
(setq terminal-index (nth 0 item))
(setq look-ahead-length (nth 1 item))
(setq look-ahead (nth 2 item))
(while (and
(< look-ahead-length k)
(<= terminal-index terminals-max-index))
(let ((potential-look-ahead look-ahead)
(next-terminal (nth terminal-index terminals)))
(push next-terminal potential-look-ahead)
(if (gethash potential-look-ahead marked-paths)
(setq terminal-index (1+ terminal-index))
(puthash
potential-look-ahead
t
marked-paths)
(push
`(,terminal-index ,look-ahead-length,look-ahead)
stack)
(setq look-ahead-length (1+ look-ahead-length))
(setq look-ahead potential-look-ahead)
(setq terminal-index 0))))
(let ((look-ahead-to-add))
(if look-ahead
(progn
;; If length of look-ahead is below k, append EOF identifiers
(while (< look-ahead-length k)
(push
parser-generator--eof-identifier
look-ahead)
(setq
look-ahead-length
(1+ look-ahead-length)))
(setq
look-ahead-to-add
(reverse look-ahead)))
(setq
look-ahead-to-add
eof-list))
(when
(and look-ahead-to-add
(not
(gethash
look-ahead-to-add
added-look-aheads)))
(puthash
look-ahead-to-add
t
added-look-aheads)
(push
look-ahead-to-add
look-aheads))))))
(sort
look-aheads
'parser-generator--sort-list)))
(defun parser-generator--get-grammar-non-terminals (&optional G)
"Return non-terminals of grammar G."
(unless G
(if parser-generator--grammar
(setq G parser-generator--grammar)
(error "No grammar G defined!")))
(nth 0 G))
(defun parser-generator--get-grammar-production-number (production)
"If PRODUCTION exist, return it's number."
(unless parser-generator--table-productions-number
(error "Table for production-numbers is undefined!"))
(gethash
production
parser-generator--table-productions-number))
(defun parser-generator--get-grammar-production-by-number (production-number)
"If PRODUCTION-NUMBER exist, return it's production."
(unless parser-generator--table-productions-number-reverse
(error "Table for reverse production-numbers is undefined!"))
(gethash
production-number
parser-generator--table-productions-number-reverse))
(defun parser-generator--get-grammar-context-sensitive-attributes-by-production-number (production-number)
"Get context-sensitive attributes by PRODUCTION-NUMBER, if any."
(gethash
production-number
parser-generator--table-productions-attributes))
(defun parser-generator--get-grammar-productions (&optional G)
"Return productions of grammar G."
(unless G
(if parser-generator--grammar
(setq G parser-generator--grammar)
(error "No grammar G defined!")))
(nth 2 G))
(defun parser-generator--get-grammar-rhs (lhs)
"Return right hand sides of LHS if there is any."
(unless parser-generator--table-productions-rhs
(error "Table for productions RHS indexed by LHS is undefined!"))
(unless (listp lhs)
(setq lhs (list lhs)))
(gethash
lhs
parser-generator--table-productions-rhs))
(defun parser-generator--get-grammar-start (&optional G)
"Return start of grammar G."
(unless G
(if parser-generator--grammar
(setq G parser-generator--grammar)
(error "No grammar G defined!")))
(nth 3 G))
(defun parser-generator--get-grammar-terminals (&optional G)
"Return terminals of grammar G."
(unless G
(if parser-generator--grammar
(setq G parser-generator--grammar)
(error "No grammar G defined!")))
(nth 1 G))
(defun parser-generator--get-grammar-translation-by-number (production-number)
"If translation for PRODUCTION-NUMBER exist, return it."
(unless parser-generator--table-translations
(error "Table for translations by production-number is undefined!"))
(gethash production-number parser-generator--table-translations))
(defun parser-generator--get-list-permutations (list k)
"Return all possible LIST permutations length K."
(let ((permutations)
(permutations-length 1))
(let ((list-length (length list))
(i 0))
(while (< i k)
(let ((times (expt list-length (- k (1+ i))))
(global-i 0))
(while (< global-i permutations-length)
;; For each list..
(let ((list-i 0))
(while (< list-i list-length)
;; Add it |list| ^ (k - i) times to list
(let ((times-i 0))
(while (< times-i times)
(if (= i 0)
(push (list (nth list-i list)) permutations)
(push (nth list-i list) (nth global-i permutations)))
(setq global-i (1+ global-i))
(setq times-i (1+ times-i))))
(setq list-i (1+ list-i))))
(when (= i 0)
(setq permutations-length (length permutations)))))
(setq i (1+ i))))
(sort permutations 'parser-generator--sort-list)))
(defun parser-generator--hash-to-list (hash-table &optional un-sorted)
"Return a list that represent the HASH-TABLE.
Each element is a list: (list key value), optionally UN-SORTED."
(let (result)
(if (hash-table-p hash-table)
(progn
(maphash
(lambda (k v)
(if (hash-table-p v)
(push (list k (parser-generator--hash-to-list v un-sorted)) result)
(push (list k v) result)))
hash-table)
(if un-sorted
(nreverse result)
(sort (nreverse result) (lambda (a b) (< (car a) (car b))))))
nil)))
(defun parser-generator--hash-values-to-list (hash-table &optional un-sorted)
"Return a list that represent the HASH-TABLE.
Each element is a list: (list key value), optionally UN-SORTED."
(let (result)
(if (hash-table-p hash-table)
(progn
(maphash
(lambda (_k v) (push v result))
hash-table)
(if un-sorted
(nreverse result)
(sort (nreverse result) (lambda (a b) (< (car a) (car b))))))
nil)))
(defun parser-generator--load-symbols ()
"Load all symbols of grammar."
;; Build hash-table of all terminals of grammar
(let ((terminals
(parser-generator--get-grammar-terminals)))
(setq
parser-generator--table-terminal-p
(make-hash-table :test 'equal))
(dolist (terminal terminals)
(puthash
terminal
t
parser-generator--table-terminal-p)))
;; Build hash-table of all non-terminals
(let ((non-terminals
(parser-generator--get-grammar-non-terminals)))
(setq
parser-generator--table-non-terminal-p
(make-hash-table :test 'equal))
(dolist (non-terminal non-terminals)
(puthash
non-terminal
t
parser-generator--table-non-terminal-p)))
;; Build hash-tables of context-sensitive attributes
(setq
parser-generator--table-context-sensitive-attributes-p
(make-hash-table :test 'equal))
(dolist
(attribute
parser-generator--context-sensitive-attributes)
(puthash
attribute
t
parser-generator--table-context-sensitive-attributes-p))
;; Build hash-table of global attributes
(setq
parser-generator--table-global-attributes-p
(make-hash-table :test 'equal))
(dolist
(attribute
parser-generator--global-attributes)
(puthash
attribute
t
parser-generator--table-global-attributes-p))
;; Validate global declaration
(when
parser-generator--global-declaration
(dolist
(item
parser-generator--global-declaration)
(unless
(parser-generator--valid-global-attribute-p
(car item))
(error
"Invalid global declaration '%S' in grammar!"
(car item)))))
(let ((productions
(parser-generator--get-grammar-productions)))
;; Build hash-table of all right-hand-sides of
;; a given left-hand-side of a production
;; exclude all functions that are used for translations
(setq
parser-generator--table-productions-rhs
(make-hash-table :test 'equal))
;; Build hash-table of production -> production number
;; and production-number -> production
;; and a new set of productions that excludes translations
;; and always has the left-hand-side as a list
;; and verify each element in RHS belonging to terminals
;; or non-terminals
(setq
parser-generator--table-productions-number
(make-hash-table :test 'equal))
(setq
parser-generator--table-productions-number-reverse
(make-hash-table :test 'equal))
(setq
parser-generator--table-translations
(make-hash-table :test 'equal))
(setq
parser-generator--table-productions-attributes
(make-hash-table :test 'equal))
(let ((production-index 0)
(new-productions))
;; Iterate each production
(dolist (p productions)
(let ((lhs (car p))
(rhs (cdr p))
(production))
(unless (listp lhs)
(setq lhs (list lhs)))
(let ((new-value
(gethash
lhs
parser-generator--table-productions-rhs))
(rhs-element-index 0)
(rhs-length (length rhs))
(rhs-element))
;; Iterate each symbol in RHS
(while
(<
rhs-element-index
rhs-length)
(let ((translation)
(production-attributes))
(setq
rhs-element
(nth
rhs-element-index
rhs))
;; Potentially each symbol in RHS could be a separate RHS
(unless (listp rhs-element)
(setq rhs-element (list rhs-element)))
(let ((sub-rhs-element-index 0)
(sub-rhs-element-length (length rhs-element))
(sub-rhs-element)
(new-rhs))
;; Iterate each symbol in SUB-RHS
(while
(<
sub-rhs-element-index
sub-rhs-element-length)
(setq
sub-rhs-element
(nth
sub-rhs-element-index
rhs-element))
(if (and
(listp sub-rhs-element)
(functionp sub-rhs-element))
(setq
translation
sub-rhs-element)
(unless
(or
(parser-generator--valid-terminal-p sub-rhs-element)
(parser-generator--valid-non-terminal-p sub-rhs-element)
(parser-generator--valid-e-p sub-rhs-element)
(parser-generator--valid-eof-p sub-rhs-element)
(parser-generator--valid-context-sensitive-attribute-p sub-rhs-element))
(error
"Symbol %s in RHS %s of production %s is not a valid terminal, non-terminal, context-sensitive attribute, e-identifier or EOF-identifier!"
sub-rhs-element
rhs-element
lhs))
(if (parser-generator--valid-context-sensitive-attribute-p
sub-rhs-element)
(progn
(when (=
sub-rhs-element-index
(1- sub-rhs-element-length))
(error "Expecting value for context-sensitive attribute %S!" sub-rhs-element))
(let ((attribute-value
(nth
(1+ sub-rhs-element-index)
rhs-element)))
(if production-attributes
(setq
production-attributes
(append
production-attributes
sub-rhs-element
attribute-value))
(setq
production-attributes
(list
sub-rhs-element
attribute-value)))
(setq
sub-rhs-element-index
(1+ sub-rhs-element-index))))
(push
sub-rhs-element
new-rhs)))
(setq
sub-rhs-element-index
(1+ sub-rhs-element-index)))
(setq
production
(list
lhs
(reverse new-rhs)))
(message
";; Production %s: %S"
production-index
production)
(push
(reverse new-rhs)
new-value)
(puthash
lhs
(reverse new-value)
parser-generator--table-productions-rhs))
(setq
rhs-element-index
(1+ rhs-element-index))
(puthash
production
production-index
parser-generator--table-productions-number)
(puthash
production-index
production
parser-generator--table-productions-number-reverse)
(puthash
production-index
production-attributes
parser-generator--table-productions-attributes)
(push
production
new-productions)
(when translation
(parser-generator--debug
(message
"Translation %S: %S"
production-index
translation))
(puthash
production-index
translation
parser-generator--table-translations))
(setq
production-index
(1+ production-index)))))))
(setq
new-productions
(nreverse new-productions))
(setcar
(nthcdr
2
parser-generator--grammar)
new-productions)))
(let ((look-aheads
(parser-generator--get-grammar-look-aheads)))
(setq
parser-generator--table-look-aheads-p
(make-hash-table :test 'equal))
(dolist (look-ahead look-aheads)
(puthash
look-ahead
t
parser-generator--table-look-aheads-p))))
(defun parser-generator-set-e-identifier (e-identifier)
"Set E-IDENTIFIER."
(unless (or
(stringp e-identifier)
(symbolp e-identifier))
(error "E-identifier must be a symbol or string!"))
(setq parser-generator--e-identifier e-identifier))
(defun parser-generator-set-eof-identifier (eof-identifier)
"Set EOF-IDENTIFIER."
(unless (or
(stringp eof-identifier)
(symbolp eof-identifier))
(error "EOF-identifier must be a symbol or string!"))
(setq parser-generator--eof-identifier eof-identifier))
(defun parser-generator-set-look-ahead-number (k)
"Set look-ahead number K."
(unless (parser-generator--valid-look-ahead-number-p k)
(error "Invalid look-ahead number k!"))
(setq parser-generator--look-ahead-number k))
(defun parser-generator-set-grammar (G)
"Set grammar G.."
(unless (parser-generator--valid-grammar-p G)
(error "Invalid grammar G! Error %s" (parser-generator--get-last-grammar-error)))
(setq parser-generator--grammar G))
(defun parser-generator-process-grammar ()
"Process grammar."
(message "\n;; Starting process of grammar..\n")
(parser-generator--clear-cache)
(unless parser-generator--look-ahead-number
(error "No look-ahead-number defined!"))
(unless
(parser-generator--valid-look-ahead-number-p
parser-generator--look-ahead-number)
(error "Invalid look-ahead number k!"))
(message ";; k = %d" parser-generator--look-ahead-number)
(unless parser-generator--grammar
(error "No grammar defined!"))
(unless
(parser-generator--valid-grammar-p
parser-generator--grammar)
(error
"Invalid grammar! Error: %s"
(parser-generator--get-last-grammar-error)))
(parser-generator--load-symbols)
(message "\n;; Completed process of grammar\n"))
(defun parser-generator--sort-list (a b)
"Return non-nil if a element in A is greater than a
element in B in lexicographic order."
(let ((length (min (length a) (length b)))
(index 0)
(continue t)
(response nil))
(while (and
continue
(< index length))
(let ((a-element (nth index a))
(b-element (nth index b)))
(while (and
a-element
(listp a-element))
(setq a-element (car a-element)))
(while (and
b-element
(listp b-element))
(setq b-element (car b-element)))
(when (and
(or
(stringp a-element)
(symbolp a-element))
(or
(stringp b-element)
(symbolp b-element)))
(if (string-greaterp a-element b-element)
(setq continue nil)
(when (string-greaterp b-element a-element)
(setq response t)
(setq continue nil))))
(when (and
(numberp a-element)
(numberp b-element))
(if (> a-element b-element)
(setq continue nil)
(when (> b-element a-element)
(setq response t)
(setq continue nil)))))
(setq index (1+ index)))
response))
(defun parser-generator--valid-context-sensitive-attribute-p (attribute)
"Check if ATTRIBUTE is a valid context-sensitive attribute."
(gethash
attribute
parser-generator--table-context-sensitive-attributes-p))
(defun parser-generator--valid-context-sensitive-attributes-p (attributes)
"Check if all ATTRIBUTES are valid context-sensitive attributes."
(let ((is-valid t)
(length (length attributes))
(index 0))
(unless (listp attributes)
(setq is-valid nil))
(while (and
is-valid
(< index length))
(let ((element
(nth index attributes)))
(unless
(parser-generator--valid-context-sensitive-attribute-p
element)
(setq
is-valid
nil)))
(setq index (+ index 2)))
is-valid))
(defun parser-generator--valid-global-attribute-p (attribute)
"Check if ATTRIBUTE is a valid global attribute."
(gethash
attribute
parser-generator--table-global-attributes-p))
(defun parser-generator--valid-global-attributes-p (attributes)
"Check if all ATTRIBUTES are valid global attributes."
(let ((is-valid t)
(length (length attributes))
(index 0))
(unless (listp attributes)
(setq is-valid nil))
(while (and
is-valid
(< index length))
(let ((element
(nth index attributes)))
(unless
(parser-generator--valid-global-attribute-p
element)
(setq
is-valid
nil)))
(setq index (+ index 2)))
is-valid))
(defun parser-generator--valid-e-p (symbol)
"Return whether SYMBOL is the e identifier or not."
(eq symbol parser-generator--e-identifier))
(defun parser-generator--valid-eof-p (symbol)
"Return whether SYMBOL is the EOF identifier or not."
(eq symbol parser-generator--eof-identifier))
(defun parser-generator--get-last-grammar-error ()
"Get last grammar validation error, if any otherwise nil."
parser-generator--last-grammar-error)
(defun parser-generator--valid-grammar-p (G)
"Return if grammar G is valid or not.
Grammar should contain list with 4 elements: non-terminals (N), terminals (T),
productions (P), start (S) where N, T and P
are lists containing symbols and/or strings and S is a symbol or string."
(setq parser-generator--last-grammar-error nil)
(let ((valid-p t))
(unless (listp G)
(setq
parser-generator--last-grammar-error
"Grammar is not a list!")
(setq valid-p nil))
(when (and
valid-p
(not (= (length G) 4)))
(setq
parser-generator--last-grammar-error
"Grammar is not a list of length 4! (non-terminals, terminals, productions, start)")
(setq valid-p nil))
(when (and
valid-p
(or
(not (listp (nth 0 G)))
(not (listp (nth 1 G)))
(not (listp (nth 2 G)))
(not (or
(stringp (nth 3 G))
(symbolp (nth 3 G))))))
(cond
((not (listp (nth 0 G)))
(setq
parser-generator--last-grammar-error
"The grammars first element, the list of non-terminals, is not a list!"))
((not (listp (nth 1 G)))
(setq
parser-generator--last-grammar-error
"The grammars second element, the list of terminals, is not a list!"))
((not (listp (nth 2 G)))
(setq
parser-generator--last-grammar-error
"The grammars third element, the list of productions, is not a list!"))
((not
(or
(stringp (nth 3 G))
(symbolp (nth 3 G))))
(setq
parser-generator--last-grammar-error
"The grammars fourth element, the grammar start, is neither a string or symbol!")))
(setq valid-p nil))
(when valid-p
;; Check every non-terminal
(let ((non-terminals (nth 0 G)))
(let ((non-terminal-count (length non-terminals))
(non-terminal-index 0))
(while (and
valid-p
(< non-terminal-index non-terminal-count))
(let ((non-terminal
(nth non-terminal-index non-terminals)))
(unless
(or
(symbolp non-terminal)
(stringp non-terminal))
(setq
parser-generator--last-grammar-error
(format
"Non-terminal %S is neither a string or a symbol!"
non-terminal))
(setq valid-p nil)))
(setq
non-terminal-index
(1+ non-terminal-index)))))
;; Check every terminal
(let ((terminals (nth 1 G)))
(let ((terminal-count (length terminals))
(terminal-index 0))
(while
(and
valid-p
(< terminal-index terminal-count))
(let ((terminal (nth terminal-index terminals)))
(unless
(or
(symbolp terminal)
(stringp terminal))
(setq
parser-generator--last-grammar-error
(format
"Terminal %S is neither a string or a symbol!"
terminal))
(setq valid-p nil)))
(setq
terminal-index
(1+ terminal-index)))))
;; Check every production
(let ((productions (nth 2 G)))
(let ((production-count (length productions))
(production-index 0))
(while (and
valid-p
(<
production-index
production-count))
(let ((production
(nth
production-index
productions)))
(unless
(parser-generator--valid-production-p
production)
(setq
parser-generator--last-grammar-error
(format
"Production %S is not a valid production!"
production))
(setq valid-p nil)))
(setq
production-index
(1+ production-index)))))
;; Check start
(let ((start (nth 3 G)))
(when (and
valid-p
(not (or (stringp start) (symbolp start))))
(setq
parser-generator--last-grammar-error
(format
"Start %S is neither a string or a symbol!"
start))
(setq valid-p nil))))
valid-p))
(defun parser-generator--valid-look-ahead-p (symbol)
"Return whether SYMBOL is a look-ahead in grammar or not."
(unless parser-generator--table-look-aheads-p
(error "Table for look-aheads is undefined!"))
(unless (listp symbol)
(setq symbol (list symbol)))
(gethash
symbol
parser-generator--table-look-aheads-p))
(defun parser-generator--valid-look-ahead-number-p (k)
"Return if look-ahead number K is valid or not."
(and
(integerp k)
(>= k 0)))
(defun parser-generator--valid-non-terminal-p (symbol)
"Return whether SYMBOL is a non-terminal in grammar or not."
(unless parser-generator--table-non-terminal-p
(error "Table for non-terminals is undefined!"))
(gethash
symbol
parser-generator--table-non-terminal-p))
(defun parser-generator--valid-production-p (production)
"Return whether PRODUCTION is valid or not."
(let ((is-valid t))
(unless (listp production)
(setq is-valid nil))
(when (and is-valid
(not (> (length production) 1)))
(setq is-valid nil))
(when (and
is-valid
(not (or
(stringp (car production))
(symbolp (car production))
(listp (car production)))))
(setq is-valid nil))
;; Validate left-hand-side (LHS) of production
(when (and is-valid