-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.py
1269 lines (1149 loc) · 39.1 KB
/
Parser.py
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
# -*- coding: utf-8 -*-
######################################################################
# Parser
# Qianji Zheng, Texas Tech University
# July 2014
######################################################################
from Tokenizer import *
from Expression import *
from fractions import Fraction
''':
This simple parser program parses the following grammar:
######################################################################
Grammar of LED terms
T0 → variable | numeral | definedConstant | ( term ) | pipe term pipe |
{ term .. term } | prefun ( terms ) | { } | { terms } |
{ term pipe Stmt }
T1 → T0 | T1 [ term ]
T2 → T1 | T1 ^ T2
Bigop → Sum | Prod | Union | Nrsec
Prefix → - | + | Bigop [ Stmt ] | definedFunctionSymbol | floor | ceil
T3 → T2 | Prefix T3
Infix4 → * | / | mod | nrsec
T4 → T3 | T4 Infix4 T3
Infix5 → + | - | U | \
T5 → T4 | T5 Infix5 T4
term → T5 | lambda vars . term | type var
terms → term | term , terms
infpred → = | < | > | <= | >= | in | subeq
infpredString → term infpred term infpred term | term infpred infpredString
S0 → definedRelationSym term | term infpred term | term : type
S1 → S0 | some var in term . S1 | all var in term . S1
S2 → S1 | ~ S2
S3 → S2 | S3 & S2
S4 → S3 | S4 or S3
S5 → S4 | S4 => S5
S6 → S5 | S5 <=> S6
Stmt → S6
vars → var | var vars
guard → If Stmt then
constDef → constSym := body | guard constSym := body
ifBranch → term if Stmt
ifBranches → ifBranch | ifBranch ifBranches
branches → ifBranches | ifBranches ; term otherwise
whereClause → where Stmt
funDefBod → term | branches
funRHS → funDefBod | funDefBod whereClause
funcDef → ?guard funcSym params := funDefBod ?whereClause
relDef → ?guard relSym params iff Stmt
varList → var | var , varlist
params → ( varList )
varDecl → var var : type | vars decls
decls → varList : type | varList : type , decls
type → typeSymbol | type Product | Set ( type ) | List ( type )
|type -> type | type ~> type |
typeProduct → type * type | type * typeProduct
indefiniteTerm → term | < type > | var where Stmt | ( varlist ) where Stmt
typeDef → typeSym ::= typeDefBody
typeDefBody → indefiniteTerm | indefiniteTerm pipe typeDefBody
programElement var → varDecl | constDef funcDef | relDef | typeDef
program → programElement | programElement program
######################################################################
'''
'''
A *var* or an *identifier* is a nonempty string of letters and digits beginning with a letter.
A *numeral* is a nonempty string of digits
Please refer Evaluater.py for the definition of AST
'''
'''
# All the functions in this file that starts with the word "parse" have the same signature as follows if their function signatures are not provided:
# list(string) -> AST * bool
# If S is a list of string then parse**(S) = (tree,True), where tree is the abstract syntax tree of S, if S complies to the corresponding rule of the grammar
# otherwise parse**(S) = (None,False).
# Each rule is given right before the function definition
'''
'''
######################################################################
'''
'''
list<str> -> (str * list<str>) * bool
If S is a list of string that comply to the format of the left hand side of the function definition,
then parseLHS(S) = ((fName,fParams),True), where fname is a string and fParams is a list of strings
otherwise, parseLHS(S) = (None,False)
'''
def parseLHS(S):
if len(S)==1 and isIdentifier(S[0]):
return ((S[0],[]),True)
if isIdentifier(S[0]) and S[1]=='(' and S[len(S)-1]==')':
fName = S[0] # the name of the function
# f()
if len(S)==3:
return ((fName,[]),True)
t,f = parseTerms(S[2:-1])
if f:
S = S[2:len(S)-1]
fParams = params(S)
return ((fName,fParams),True)
return (None,False)
# rule: funcBody -> Expression | conditional
def parseFuncBody(S):
tree1,flag1 = parseConditional(S)
if flag1:
return (tree1,True)
tree2,flag2 = parseExpression(S)
if flag2:
return (tree2,True)
return (None,False)
# rule: conditional -> ifClauses | ifClauses ; term otherwise
def parseConditional(S):
if S[-1]=='otherwise':
S = S[:-1]
# find the last ';' of S
i = lastIndex(';',S)
(t2,f2)=parseIfClauses(S[0:i])
(t1,f1)= parseTerm(S[i+1:])
if f1 and f2:
if(t2.op()=='cond'):
# add otherwise to the list of AST
return (AST('cond',t2.args()+[AST('ow',[t1])]),True)
else:
return (AST('cond',[t2,AST('ow',[t1])]),True)
tree1,flag1 = parseIfClauses(S)
if flag1:
return (tree1,True)
return (None,False)
# rule: ifClasuses -> ifClause | ifClause ; ifClauses
def parseIfClauses(S):
for i in range(len(S)):
if S[i]==';':
(t1,f1)=parseIfClause(S[0:i])
(t2,f2)= parseIfClauses(S[i+1:])
if f1 and f2:
if(isinstance(t2.tree,list) and t2.op()=='cond'):
return (AST('cond',[t1]+t2.args()),True)
else:
return (AST('cond',[t1,t2]),True)
(tree,flag) = parseIfClause(S)
if flag:
return (tree,True)
return (None,False)
# rule: ifClause -> term if statement
def parseIfClause(S):
for i in range(len(S)):
if S[i]=='if':
(t1,f1)=parseTerm(S[0:i])
# statement and sentence are used interchangely
(t2,f2)= parseSentence(S[i+1:])
if f1 and f2:
return (AST('if',[t2,t1]),True)
return (None,False)
def parseWhereClause(S):
'''rule: whereClause -> where Stmt
'''
if len(S)>1 and S[0]=='where':
(t,f)=parseSentence(S[1:])
if f:
return (t,True)
return(None,False)
'''
# helper function
# str * list<str> -> int
# If C is a string and S is a list of string, lastIndex(C, S) is the index of the last C in S if there is at least C,
# otherwise lastIndex(C, S) = 0
'''
def lastIndex(C, S):
index = 0
for i in range(len(S)):
if(S[i]==C):
index = i
return index
# Expression -> Sentence | Term
def parseExpression(S):
if S==None or len(S)==0:
return(None,False)
tree2,flag2 = parseSentence(S)
if flag2:
return (tree2,True)
tree1,flag1 = parseTerm(S)
if flag1:
return (tree1,True)
return (None,False)
# rule: Sentence -> S6 | ( S6 )
def parseSentence(S):
if S==None or len(S)==0:
return(None,False)
tree2,flag2 = parseS6(S)
if flag2:
return (tree2,True)
if S[0]=='(' and S[-1]==')':
tree1,flag1 = parseS6(S[1:-1])
if flag1:
return (tree1,True)
return (None,False)
# rule: S6 -> S5 | S5 <=> S6
def parseS6(S):
for i in range(len(S)):
if S[i]=='<=>':
(t1,f1)=parseS5(S[0:i])
(t2,f2)= parseS6(S[i+1:])
if f1 and f2:
return (AST('<=>',[t1,t2]),True)
(tree,flag) = parseS5(S)
if flag:
return (tree,True)
return (None,False)
# rule: S5 -> S4 | S4 => S5
def parseS5(S):
for i in range(len(S)):
if S[i]=='=>':
(t1,f1)=parseS4(S[0:i])
(t2,f2)= parseS5(S[i+1:])
if f1 and f2:
return (AST('=>',[t1,t2]),True)
(tree,flag) = parseS4(S)
if flag:
return (tree,True)
return (None,False)
# rule: S4 > S3 | S4 or S3
def parseS4(S):
for i in range(len(S)):
if S[i]=='or':
(t1,f1)=parseS4(S[0:i])
(t2,f2)= parseS3(S[i+1:])
if f1 and f2:
return (AST('or',[t1,t2]),True)
(tree,flag) = parseS3(S)
if flag:
return (tree,True)
return (None,False)
# rule: S3 -> S2 | S3 & S2
def parseS3(S):
for i in range(len(S)):
# if S[i]=='&' or S[i]==',':
if S[i]=='&':
(t1,f1)=parseS3(S[0:i])
(t2,f2)= parseS2(S[i+1:])
if f1 and f2:
return (AST('and',[t1,t2]),True)
(tree,flag) = parseS2(S)
if flag:
return (tree,True)
return (None,False)
#rule: S2 -> S1 | ~ S2
def parseS2(S):
if len(S)==0:
return (None,False)
if S[0]=='~':
(t1,f1)= parseS2(S[1:])
if f1:
return (AST('~',[t1]),True)
(tree,flag) = parseS1(S)
if flag:
return (tree,True)
return (None,False)
#rule S1 -> S0 | some var in term : S2 | all var in term : S2
def parseS1(S):
# some var in term : S1 | all var in term : S1
(tree,flag) = parseSomeAll(S)
if flag:
return (tree,True)
# S0
(tree,flag) = parseS0(S)
if flag:
return (tree,True)
return (None,False)
# rule: some var in term . S2 | all var in term . S2
def parseSomeAll(S):
separators = ['some','all']
if len(S)<6:
return(None,False)
if S[0] in separators:
try:
i = S.index('in')
j = S.index('.')
t1,f1 = parseVar(S[1])
t2,f2 = parseTerm(S[i+1:j])
t3,f3 = parseS2(S[j+1:])
if f1 and f2 and f3:
return (AST(S[0],[t1,t2,t3]),True)
except ValueError:
return (None,False)
return (None,False)
#rule S0 -> term infpred term | identifier | consecutives | expression typeExpression
InfpredS0 = ['=','<','>','<=','>=','in','subeq']
def parseS0(S):
if len(S)==0: return(None,False)
# parse identifier
if len(S)==1:
if isIdentifier(S[0]):
return (AST(S[0]),True)
for i in range(len(S)):
for infpred in InfpredS0:
if S[i]==infpred:
(t1,f1)=parseTerm(S[0:i])
(t2,f2)= parseTerm(S[i+1:])
if f1 and f2:
return (AST(infpred,[t1,t2]),True)
# (Sentence)
if S[0]=='(' and S[len(S)-1]==')':
(tree,flag)=parseSentence(S[1:len(S)-1])
if flag: return (tree,True)
# prerel (terms)
if S[len(S)-1] ==')':
(tree,flag) = parseUserDefinedFun(S)
if flag: return (tree,True)
if len(S)>=5:
(tree,flag) = parseConsecutives(['<','<='],S)
if flag: return (tree,True)
(tree,flag) = parseConsecutives(['>','>='],S)
if flag: return (tree,True)
(tree,flag) = parseConsecutives(['='],S)
if flag: return (tree,True)
# Expression : typeExpression
for i in range(len(S)):
if S[i]==':':
(t1,f1)=parseExpression(S[0:i])
(t2,f2)= parseTypeExpression(S[i+1:])
if f1 and f2:
return (AST(':',[t1,t2]),True)
return (None,False)
def universalParse(S,F):
'''helper function to remove duplicate functions
list<list<str>> * list<str> -> tuple
If S is a list of parsing list of string, and F is a list of parsing functions
then universalParse(S,F) = (trees,True), where trees is a list of the abstract syntax trees,
if each memeber of S complies to its corresponding rule in F
otherwise universalParse(S,F) = (None,False).
For example, if S = [['Int'],['Int']], F=['TExp0','TExp0'] then universalParse(S,F)= (trees,True),
where trees=[t1,t2] and t1 is AST of parseTExp0(S[0]) and t2 is the AST of parseTExp0(S[1])
'''
trees = []
flags = []
if len(F)>0 and len(F) ==len(S):
for i in range(len(F)):
func = functionNames(F[i])
t,f = func(S[i])
trees.append(t)
flags.append(f)
if all(flags):
return (trees,True)
return (None,False)
# typeProduct -> tExp0 * tExp0 | tExp0 * typeProduct
def parseTypeProduct(S):
for i in range(len(S)):
if S[i]=='*':
(t1,f1)=parseTExp0(S[0:i])
(t2,f2)= parseTypeProduct(S[i+1:])
(t3,f3) = parseTExp0(S[i+1:])
# tExp0 * typeProduct
if f1 and f2:
if(isinstance(t2.tree,list) and t2.op()=='star'):
return (AST('star',[t1]+t2.args()),True)
else:
return (AST('star',[t1,t2]),True)
# tExp0 * tExp0
if f1 and f3:
return (AST('star',[t1,t3]),True)
return (None,False)
def parseTypeExpression(S):
'''rule: typeExpression -> tExp0 |typeProduct | typeExpression U tExp0 | typeExpression U typeProduct
'''
# tExp0
t,f = parseTExp0(S)
if f:
return (t,f)
# typeExpression U tExp0 | typeExpression U typeProduct
for i in range(len(S)):
if S[i]=='U':
t2,f2 = parseTypeExpression(S[0:i])
t1,f1 = parseTExp0(S[i+1:])
t3,f3 = parseTypeProduct(S[i+1:])
if f1 and f2:
return (AST('U',[t2,t1]),True)
if f3 and f2:
return (AST('U',[t2,t3]),True)
# typeProduct
if S.count('U')==0 and S.count('*')>0:
t,f = parseTypeProduct(S)
if f:
return(t,f)
return(None,False)
# TExp0 -> Bool | Nat | Int | Rat | (typeExpression)|Seq(tExp)|fSet(tExp)|typeSymbol
def parseTExp0(S):
# built-in types: Bool, Nat, Int, Rat, fSet, Seq, Lambda
if len(S)==1 and isIdentifier(S[0]):
return (AST(S[0]),True)
# (typeExpression)
if len(S)>2 and S[0]=='(' and S[-1]==')':
return parseTypeExpression(S[1:-1])
# Seq(tExp) or fSet(tExp)
if len(S)>3 and S[1]=='(' and S[-1]==')':
t,f = parseTypeExpression(S[2:-1])
if S[0]=='Seq' and f:
return(AST('Seq',[t]),True)
if S[0]=='fSet' and f:
return (AST('fSet',[t]),True)
# a set
if len(S)>2 and S[0]=='{' and S[-1]=='}':
return parseTerm(S)
return (None,False)
# operators = ['<','<=']
# rule: consecutives -> consecutive | term op consecutive op is one of the memeber in operators
def parseConsecutives(operators,S):
#operators = ['<','<=']
connector = 'and'
if operators[0]=='U':
connector ='U'
t1,f1 = parseConsecutive(operators,S)
if f1:
return (t1,f1)
try:
#i = S.index('<')
i = firstIndex(operators,S)
if i==None:
return (None,False)
(t1,f1)=parseTerm(S[0:i])
(t2,f2)= parseConsecutives(operators,S[i+1:])
if f1 and f2:
#return ( AST(connector, [ (S[i],[t1,t2[1][0][1][0]]), t2]) , True )
return ( AST(connector, [ AST (S[i],[t1,(t2.args()[0]).args()[0]]) , t2]) , True )
except ValueError:
return(None,False)
return (None,False)
# rule: consecutive -> term < term < term
def parseConsecutive(operators,S):
#operators = ['<','=','<=']
connector = 'and'
if operators[0]=='U':
connector ='U'
if len(S)<5:
return (None,False)
try:
# get the first '<'
#i = S.index('<')
i = firstIndex(operators,S)
if i==None:
return (None,False)
# get the second '<'
# j = S[i+1:].index('<') +i+1
j = firstIndex(operators,S[i+1:])
if j==None:
return (None,False)
j= j+i+1
(t1,f1)=parseTerm(S[0:i])
(t2,f2)= parseTerm(S[i+1:j])
(t3,f3) = parseTerm(S[j+1:])
if f1 and f2 and f3:
#return (('<',[t1,t2,t3]),True)
return ( AST(connector, [ AST(S[i],[t1,t2]) , AST( S[j],[t2,t3] ) ]) , True )
except ValueError:
return (None,False)
return (None,False)
# rule: term -> T4 | lambda vars . term | typeExpression
def parseTerm(S):
t,f = parseT4(S)
if f:
return parseT4(S)
t1,f1 = parseLambda(S)
if f1:
return parseLambda(S)
# t,f = parseTypeExpression(S)
# if f:
# return(t,f)
return (None,False)
# rule: term -> lam vars . term
def parseLambda(S):
if len(S)<4:
return (None,False)
if S[0]=='lam':
try:
# find '.' in S
i = S.index('.')
except ValueError:
return (None,False)
(t1,f1)=parseVars(S[1:i])
(t2,f2)= parseTerm(S[i+1:])
if f1 and f2:
if isinstance(t1.tree,list) and t1.op()=='cstack':
return (AST('lambda',[('vars',t1.args()),t2]),True)
else:
#return (AST('lambda',[[t1],t2]),True)
return (AST('lambda',[('vars',[t1]),t2]),True)
return (None,False)
# rule: vars -> var | var vars
def parseVars(S):
if len(S)==1:
(tree,flag) = parseVar(S)
if flag:
return (tree,True)
(t1,f1)=parseVar(S[0])
(t2,f2)= parseVars(S[1:])
if f1 and f2:
if(isinstance(t2.tree,list) and t2.op()=='cstack'):
return (AST('cstack',[t1]+t2.args()),True)
else:
return (AST('cstack',[t1,t2]),True)
return (None,False)
# rule: var
def parseVar(S):
if len(S)==1:
if isIdentifier(S):
return (AST(S[0]),True)
return (None,False)
# rule: T4 -> T3 | T4 Infix4 T3
InfixT4 = ['+','-','U','\\']
def parseT4(S):
for i in range(len(S)):
for infix in InfixT4:
if S[i]==infix:
(t1,f1)=parseT4(S[0:i])
(t2,f2)= parseT3(S[i+1:])
if f1 and f2:
return (AST(infix,[t1,t2]),True)
(tree,flag) = parseT3(S)
if flag:
return (tree,True)
return (None,False)
# rule: T3 -> T2 | T3 Infix3 T2
InfixT3 = ['*','/','mod','nrsec',]
def parseT3(S):
for i in range(len(S)):
for infix in InfixT3:
if S[i]==infix:
(t1,f1)=parseT3(S[0:i])
(t2,f2)= parseT2(S[i+1:])
if f1 and f2:
return (AST(infix,[t1,t2]),True)
(tree,flag) = parseT2(S)
if flag:
return (tree,True)
return (None,False)
# rule: T2 -> T1 | Prefix2 T2 | Bigop [ Stmt ] T2 | Bigop [ Stmt ] ^ [ Stmt ] T2
PrefixT2 = ['+','-']
def parseT2(S):
if len(S)==0:
return (None,False)
for prefix in PrefixT2:
if S[0]==prefix:
(t,f)= parseT2(S[1:])
if f:
return (AST(prefix+'1',[t]),True)
(tree,flag) = parseT1(S)
if flag:
return (tree,True)
(tree,flag) = parseBigop(S)
if flag:
return (tree,True)
return (None,False)
Bigop = ['Sum','Prod','Union','Nrsec']
# rule: Bigop [ Stmt ] T2 | Bigop [ Stmt ] ^ [ Stmt ] T2 | Bigop[var = term] ^ [term] T2
def parseBigop(S):
(tree,flag) = parseBigop1(S)
if flag:
return (tree,True)
(tree,flag) = parseBigop2(S)
if flag:
return (tree,True)
(tree,flag) = parseBigop3(S)
if flag:
return (tree,True)
return (None,False)
# rule: Bigop [ Stmt ] T2
def parseBigop1(S):
if len(S)<5:
return(None,False)
if S[0] in Bigop:
try:
# starting from the next char of the first '[', find the first ']' that does not match '['
i = closeParentesis('[',S[2:])
if i==None:
return (None,False)
i = i+2
t1,f1 = parseSentence(S[2:i])
t2,f2 = parseT2(S[i+1:])
if f1 and f2:
return (AST(S[0],[t1,t2]),True)
except ValueError:
return (None,False)
return (None,False)
# rule: Bigop [ Stmt ] ^ [ Stmt ] T2
def parseBigop2(S):
if len(S)<9:
return(None,False)
if S[0] in Bigop:
try:
# starting from the next char of the first '[', find the first ']' that does not match '['
i = closeParentesis('[',S[2:])
if i==None:
return (None,False)
# add 2 to get the correct index
i=i+2
j = i+1
k = closeParentesis('[',S[j+2:])
if k == None:
return (None,False)
k=k+2+j
t1,f1 = parseSentence(S[2:i])
t2,f2 = parseSentence(S[j+2:k])
t3,f3 = parseT2(S[k+1:])
if f1 and f2 and f3:
return (AST(S[0],[t1,t2,t3]),True)
except ValueError:
return (None,False)
return (None,False)
# rule: Bigop[var = term] ^ [term] T2
# Bigop[var = term1]^[term2] term3 should parse with the same AST as Bigop[var in {term1...term2}] term3
def parseBigop3(S):
if len(S)<11:
return(None,False)
if S[0] in Bigop:
try:
# find the first '=', except will catch it if not found
e = S.index('=')
f = S.index('^')
# starting from the next char of the first '[', find the first ']' that does not match '['
i = closeParentesis('[',S[2:])
if i==None:
return (None,False)
# add 2 to get the correct index
i=i+2
j = i+1
k = closeParentesis('[',S[j+2:])
if k == None:
return (None,False)
k=k+2+j
t1a,f1a = parseVar(S[2:e])
t1b,f1b = parseTerm(S[e+1:i])
t2,f2 = parseTerm(S[j+2:k])
t3,f3 = parseT2(S[k+1:])
if f1a and f1b and f2 and f3 :
# Bigop[var = term1]^[term2] term3 should parse with the same AST as Bigop[var in {term1...term2}] term3
ts = AST('in',[t1a,AST('intRange',[t1b,t2]) ])
return (AST(S[0],[ts,t3]),True)
except ValueError:
return (None,False)
return (None,False)
# rule: T1 -> T0 | T1 [term] | T0 ^ T2
InfixT1 = ['^']
def parseT1(S):
if len(S)==0:
return (None,False)
# T1[term]
if S[-1] == ']':
# search back forward from the end of S to find the first [
lb = firstIndexBack('[',len(S)-1,S)
if lb ==None:
return (None,False)
else:
t1,f1 = parseT1(S[:lb])
t2,f2 = parseTerm(S[lb+1:len(S)-1])
if f1 and f2:
return (AST('sub',[t1,t2]),True)
# T0 ^ T2
for i in range(len(S)):
for infix in InfixT1:
if S[i]==infix:
(t1,f1)=parseT0(S[0:i])
(t2,f2)= parseT2(S[i+1:])
if f1 and f2:
return (AST(infix,[t1,t2]),True)
# T0
(tree,flag) = parseT0(S)
if flag:
return (tree,True)
return (None,False)
#rule 0: T0 -> atom | numeral | identifier | (term) | pipe term pipe | floor(term) | ceil(term) | prefun(terms) |
# vector | set | tuple | Pow (vector) | choose(vector)| { term .. term } | { term pipe Stmt }
def parseT0(S):
if len(S)==0:
return (None,False)
try:
# (term)
if S[0]=='(' and S[len(S)-1]==')':
(tree,flag)=parseTerm(S[1:len(S)-1])
if flag: return (tree,True)
# |term|
if S[0]=='|' and S[len(S)-1]=='|':
(tree,flag)=parseTerm(S[1:len(S)-1])
if flag: return (AST('pipes',[tree]),True)
# floor ( term )
if S[0]=='floor' and S[1]=='(' and S[len(S)-1]==')':
(tree,flag)=parseTerm(S[2:len(S)-1])
if flag: return (AST('floor',[tree]),True)
# ceil ( term )
if S[0]=='ceil' and S[1]=='(' and S[len(S)-1]==')':
(tree,flag)=parseTerm(S[2:len(S)-1])
if flag: return (AST('ceil',[tree]),True)
# Pow(vector)
if S[0]=='Pow' and S[1]=='(' and S[len(S)-1]==')':
(tree,flag)=parseVector(S[2:len(S)-1])
if flag: return (AST('Pow',[tree]),True)
# choose(vector)
if S[0]=='choose' and S[1]=='(' and S[len(S)-1]==')':
(tree,flag)=parseVector(S[2:len(S)-1])
if flag: return (AST('choose',[tree]),True)
# prefun (terms)
if S[len(S)-1] ==')':
(tree,flag) = parseUserDefinedFun(S)
if flag: return (tree,True)
if len(S)==1:
# numeral
if isNumeral(S[0]):
if isIntegerNum(S[0]):return (AST(int(Fraction(S[0]))),True)
if isDecimalNonRepeating(S[0]):return (AST(Fraction(S[0])),True)
return (AST(numeralRepeatingValue(S[0])),True)
# if S[0] is a numeral that has a decimal fraction with a repeating block
# identifier
if isIdentifier(S[0]): return (AST(S[0]),True)
# quoted string is represented as sequence with a list of asscii code
if isQuotedString(S[0]):
L=[ord(i) for i in S[0][1:-1]]
return (AST('string',L),True)
(tree,flag) = parseContainer(S)
if flag: return (tree,True)
# parse range { term .. term }
(tree,flag) = parseRange(S)
if flag: return (tree,True)
# parse { term pipe Stmt }
(tree,flag) = parseTermPipeStmt(S)
if flag: return (tree,True)
# parse atom
(tree,flag) = parseAtom(S)
if flag: return (tree,True)
# parse consecutive unions
(tree,flag) = parseConsecutives(['U'],S)
if flag: return (tree,True)
except IndexError:
return(None,False)
return (None,False)
# rule: T0 -> atom
def parseAtom(S):
if len(S)==1:
if len(S[0])>1:
if S[0][0]=='`' and isIdentifier(S[0][1:]):
return(AST(S[0]),True)
return (None,False)
# rule: T0 -> prefun ( terms )
def parseUserDefinedFun(S):
try:
if S[len(S)-1] ==')':
# find the first index of '(' in S. It throws ValueError if '(' is not in S
i = S.index('(')
(t1,f1)=parsePrefun(S[0:i])
#(t2,f2)= parseTerms(S[i+1:len(S)-1])
(t2,f2)= parseContainerTerms(S[i+1:len(S)-1])
if f1 and f2:
if(isinstance(t2.tree,list) and t2.op()=='cstack'):
return (AST(t1,t2.args()),True)
else:
return (AST(t1,[t2]),True)
except ValueError:
return(None,False)
return (None,False)
# rule: prefun -> identifier
def parsePrefun(S):
if(len(S)==1):
if isIdentifier(S[0]):
return (AST(S[0]),True)
return (None,False)
# rule: terms -> term | term , terms
def parseTerms(S):
for i in range(len(S)):
if S[i]==',':
(t1,f1)=parseTerm(S[0:i])
(t2,f2)= parseTerms(S[i+1:])
if f1 and f2:
if(isinstance(t2.tree,list) and t2.op()=='cstack'):
return (AST('cstack',[t1]+t2.args()),True)
else:
return (AST('cstack',[t1,t2]),True)
(tree,flag) = parseTerm(S)
if flag:
return (tree,True)
return (None,False)
def parseGuard(S):
'''rule: gurad -> If Stmt then
'''
if hasKeywords(S,['If','then']) and S[0]=='If' and S[-1]=='then':
return parseSentence(S[1:-1])
return (None,False)
def hasKeywords(L,Ws):
'''list<str> * list<str> -> bool
hasKeywords(L,Ws) iff all words in Ws is in L
'''
return all(word in L for word in Ws)
'''
# helper function
# str -> bool
# isIdentifier(S) iff S is an *identifier*
'''
def isIdentifier(S):
if len(S)==0: return False
if not alpha(S[0]): return False
for c in S:
if not (alphaNum(c)): return False
return True
# def isQuotedString(S):
# '''isQuotedString(S) iff S is a *quoted string*
# string ->bool
# A quoted string consists of zero or more string characters enclosed in double quotes ("). For example, the following are quoted strings:
# "hi mom"
# "Go tell the Spartans\rThou who passest by"
# "John said \"hello\""
# '''
def isNumeral(S):
'''isNumeral(S) iff S is a *numeral*
string -> bool
A *numeral* is either an *integer numeral*, a *decimal fraction*, or an *integer numeral* followed by a decimal fraction. For example, the following are LED numerals:
714 01 21.7 .3(145..) 0.(3..) 000 3.96(721..)
'''
return isIntegerNum(S) or isDecimalFraction(S) or isIntegerNumDotDecimalFraction(S)
def isIntegerNum(S):
'''An *integer numeral* is a string of 1 or more decimal digits (0-9)
string -> bool
'''
if len(S)<1:
return False
return all(x.isdigit() for x in S)
def isDecimalFraction(S):
'''A *decimal fraction* is either
1) a decimal point followed by one or more digits, or
2) a decimal point, followed by zero or more digits, followed by a repeating block
string ->bool
'''
if len(S)<2: return False
# get the index where repeating block starts
index =S.find('(')
if S[0]=='.':
if index==-1:
return all(x.isdigit() for x in S[1:])
else:
return all(x.isdigit() for x in S[1:index]) and isRepeatingBlock(S[index:])
def isRepeatingBlock(S):
''' A *repeating block* consists of a left parenthesis, followed by one or more decimal digits, followed by two periods, followed by a right parenthesis.
string -> bool
'''
if len(S)<5: return False
if S[0]=='(' and S[-1]==')' and isIntegerNum(S[1:-3]): return True
return False
def isIntegerNumDotDecimalFraction(S):
'''
isIntegerNumDotDecimalFraction(S) iff S is an *integer numeral* followed by a decimal fraction.
sting -> bool
'''
index = S.find('.')
if index ==-1: return False
return isIntegerNum(S[0:index]) and isDecimalFraction(S[index:])
def isDecimalNonRepeating(S):
'''isDecimalNonRepeating(S) iff S is a numeral with a decimal franction but does not contain a repeating block
sting->bool
'''
return isNumeral(S) and S.find('.')!=-1 and S.find('(')==-1
def repeatingBlockParameters(S):
''' If S A decimal fraction consisting of a decimal point, followed by digit string S1
of length n, followed by a repeating block whose body S2 is of length p, then repeatingBlockParameters(S) is a list [S1,S2,p,n]
otherwise []
'''
indexParen = S.find('(')
indexPeriod = S[indexParen:].find('.') +indexParen
if len(S[1:indexParen])==0:
S1=0
n=0
else:
S1=int(S[1:indexParen])
n =len(str(S1))
S2=int(S[indexParen+1:indexPeriod])
p = len(str(S2))
return [S1,S2,p,n]
def decimalFractionValue(S):
'''S is A decimal fraction consisting of a decimal point, followed by digit string S1
of length n, followed by a repeating block whose body S2 is of length p, then S is the decimal expansion of the following rational number:
S1/10**n + S2/(10**p-1)*10**n
'''
[S1,S2,p,n] = repeatingBlockParameters(S)
return Fraction(S1,10**n) + Fraction(S2,((10**p-1)*10**n))
def numeralRepeatingValue(S):
'''If S is a numeral with a decimal fraction that contains a repeating block, then numeralRepeatingValue(S) is the rational number of S
'''
# if isNumeral(S):
# if not (isIntegerNum(S) or isDecimalNonRepeating(S)):
if isDecimalFraction(S):
return decimalFractionValue(S)
else:
index = S.find('.')
return Fraction(int(S[0:index]))+decimalFractionValue(S[index:])
'''
# helper function
# AST -> bool
# If T is an AST, then hasIdentifier(T) iff there is at least one indentifier in T
'''
def hasIdentifier(T):
if isScalar(T): return False
if isIdentifier(T): return True
(Op,X) = T
for i in X:
if hasIdentifier(i):
return True
return False
'''
# helper function
list<str> -> list<str>
If S is a list of string that comply to the format of the parameters of the function definition,
then params(S) is a list of parameters of the function definition
otherwise, params(S) = []
'''