-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_expr.py
902 lines (729 loc) · 28.4 KB
/
parse_expr.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
import lexer
from copy import deepcopy
UNOP_PRECEDENCE = 4
START_PRECEDENCE = 1
N_PRECEDENCE_LEVELS = 4
PRECENDENCE_MAP = {
lexer.PLUS: 1,
lexer.MINUS: 1,
lexer.TIMES: 2,
lexer.DIV: 2,
lexer.EXP: 3,
}
class MissingParens(Exception):
def __init__(self, str):
pass
class ParseError(Exception):
def __init__(self, str):
pass
class BopMissingArg(Exception):
def __init__(self, str):
pass
class UnopAdditionalArg(Exception):
def __init__(self, str):
pass
class UnmatchedParenError(Exception):
def __init__(self, str):
pass
class EndWithOperatorError(Exception):
def __init__(self, str):
pass
class AssignVariableException(Exception):
def __init__(self, str):
pass
class AST:
"""
AST is an abstract syntax tree
"""
def __init__(self):
self.sentences = []
def is_empty(self):
return self.sentences == []
class Expr(object):
"""
Expr respresents an expression
ABSTRACT CLASS for all instantiated expression classes
"""
def __init__(self):
pass
def __repr__(self):
return "This is a abstract expression"
class IntValue(Expr):
"""
IntValue represents an Int Value
"""
def __init__(self, value):
super().__init__()
self.value = value
def get_value(self):
return self.value
def __repr__(self):
return "(IntValue: " + str(self.value) + ")"
class VarValue(Expr):
"""
IntValue represents an Variable Value
"""
def __init__(self, value):
super().__init__()
self.value = value
def get_value(self):
return self.value
def __repr__(self):
return "(VarValue: " + str(self.value) + ")"
class Bop(Expr):
"""
Bop represents e1 bop e2
"""
def __init__(self, bop, left=None, right=None):
super().__init__()
self.bop = bop
self.left = left
self.right = right
def set_left(self, left):
self.left = left
def set_right(self, right):
self.right = right
def get_bop(self):
return self.bop
def get_left(self):
return self.left
def get_right(self):
return self.right
def __repr__(self):
return "(BOP: " + str(self.left) + str(self.bop) + str(self.right) + ")"
class Unop(Expr):
"""
Unop represents unop e
"""
def __init__(self, unop, expr=None):
super().__init__()
self.unop = unop
self.expr = expr
def set_expr(self, expr):
self.expr = expr
def get_unop(self):
return self.unop
def get_expr(self):
return self.expr
def __repr__(self):
return "(UNOP: " + str(self.unop) + str(self.expr) + ")"
class Assign(Expr):
"""
assign represents var assign expre
"""
def __init__(self, var, expr=None):
super().__init__()
self.var = var
self.expr = expr
def set_expr(self, expr):
self.expr = expr
def set_var(self, var):
self.var = var
def get_expr(self):
return self.expr
def get_var(self):
return self.var
def __repr__(self):
return "(Assign: " + str(self.var) + " := " + str(self.expr) + ")"
class While(Expr):
"""
While represents
while guard_expr dowhile
phrases
endwhile
"""
def __init__(self, guard=None, body_list=None):
super().__init__()
self.guard = guard
self.body = body_list
def set_guard(self, guard):
self.guard = guard
def set_body(self, body_list):
self.body = body_list
def get_guard(self):
return self.guard
def get_body(self):
return self.body
def __repr__(self):
return "(while " + str(self.guard) + " dowhile\n\t" + "\n\t".join(list(map(lambda phrase: str(phrase), self.body))) + "\nendwhile)"
class For(Expr):
"""
For represents
For var from int to int by int dofor
phrases
endfor
"""
def __init__(self, index, from_int, end_int, by, body_list):
super().__init__()
self.index = index
self.from_int = from_int
self.end_int = end_int
self.by = by
self.body = body_list
def __repr__(self):
return "(for " + str(self.index) + " from " + str(self.from_int) + " to " + str(self.end_int) + " by " + str(self.by) + " dofor\n\t" + "\n\t".join(list(map(lambda phrase: str(phrase), self.body))) + "\nendfor)"
class Function(Expr):
"""
Function represents
fun f a b c ->
body
endfun
"""
def __init__(self, name, args_list, body_list):
super().__init__()
self.name = name
self.args = args_list
self.body = body_list
def __repr__(self):
return "(fun " + str(self.name) + " " + " ".join(list(map(lambda arg: str(arg), self.args))) + " ->\n\t" + "\n\t".join(list(map(lambda phrase: str(phrase), self.body))) + "\nendfun)"
class IfThenElse(Expr):
"""
IFThenElse represents an if then else erpression
"""
def __init__(self, if_guard, if_body, elif_guards=[], elif_bodies=[], else_body=None):
super().__init__()
self.if_pair = (if_guard, if_body)
self.elif_list = (elif_guards, elif_bodies)
self.else_body = else_body
def __repr__(self):
(if_guard, if_body) = self.if_pair
elif_guards, elif_bodies = self.elif_list
else_body = self.else_body
return ("(if " + str(if_guard) + " then\n\t" + "\n\t".join(list(map(lambda phrase: str(phrase), if_body))) + "\nendif\n"
+ ("" if elif_guards == [] else "\n".join(list(map(lambda g, b: "elif " + str(g) + " then\n\t" +
"\n\t".join(list(map(lambda phrase: str(phrase), b))) + "\nendelif\n", elif_guards, elif_bodies))))
+ ("" if else_body == None else "else\n\t" +
"\n\t".join(list(map(lambda phrase: str(phrase), else_body))) + "\nendelse\n")
+ ")"
)
class Apply(Expr):
"""
Apply represents fun (arg1 arg2...) with possibly no args as in
fun () , with only open and close brackets.
"""
def __init__(self, fun, args_list=[]):
super().__init__()
self.fun = fun
self.args_list = args_list
def set_args(self, args_list):
self.args_list = args_list
def get_fun(self):
return self.fun
def get_args(self):
return self.args_list
def __repr__(self):
return "(Apply: " + str(self.fun) + "(" + (" ".join(list(map(lambda a: str(a), self.args_list)))) + ")" + ")"
class Return(Expr):
"""
return represents
return expr;
"""
def __init__(self, body):
super().__init__()
self.body = body
def __repr__(self):
return "(Return: " + str(self.body) + ";)"
class Program(Expr):
"""
Program represents a syntacucally valid program
"""
def __init__(self, phrase_list=[]):
super().__init__()
self.phrases = phrase_list
def __repr__(self):
return "(Program:\n" + "\n".join(list(map(lambda phrase: str(phrase), self.phrases))) + "\n)"
def reduce_stack(precedence, stack):
"""
reduce_stack(stack) reduces the stack from the top of the stack
to the end into a unified AST at precedence level precedence
REQUIRES: [precendence] cannot be 0
REQUIRES: [precendence] is psoitive
REQUIRES: STACK is NOT EMPTY
REQUIRES: STACK MUST BE ABLE TO TURNED INTO A VALID AST, E>G> A STACK WITH ONE ELEMENT
MUST BE A VALUE OR VARIABLE!
If the stack has one element, returns that element
"""
assert precedence > 0
if stack == []:
return stack
l = len(stack)
if l == 1:
return stack[0]
if precedence <= 3:
end_stack = stack[:3]
bop = end_stack[1][1]
start = end_stack[0]
end = end_stack[2]
new_stack = stack[3:]
new_stack.insert(0, Bop(bop, start, end))
return reduce_stack(precedence, new_stack)
# -------- WILL BE DEPRECATED -------------
# elif precedence == 4:
# unop = stack[0][1]
# val = stack[1]
# new_stack = stack[2:]
# new_stack.insert(0, Unop(unop, val))
# return reduce_stack(precedence, new_stack)
def get_precedence(symbol, precendence_map=PRECENDENCE_MAP):
"""
get_precedence(symbol, precendence_map=PRECENDENCE_MAP) is the
precendence of a symbol
REQUIRES: precendence_map is a correct PRECENDENCE_MAP
REQUIRES: SYMBOL is an OPERATOR in precendence_map
"""
return precendence_map[symbol]
def get_between_brackets(lexbuf, idx):
"""
get_between_brackets(lexbuf, idx) gets elements in lexbuf from idx
that starts with a LPARENt e.g, '('
We assume that idx is the poisition after the LPAREN in lexbuf
Returns the length of the exprtems including start and end parentheses and
list of the expr terms in lexbuf not including the start
and end parentheses as a 2-tuple
"""
stack = []
stack.append(lexer.LPAREN)
expr_terms = []
i = idx
while (i < len(lexbuf) and stack != []):
typ, val = lexbuf[i]
if val == lexer.LPAREN:
stack.append(val)
elif val == lexer.RPAREN:
if stack != [] and stack[-1] == lexer.LPAREN:
stack.pop()
expr_terms.append((typ, val))
i += 1
if i > len(lexbuf):
raise MissingParens("Missing or Misplaced Parentheses")
if stack != []:
raise MissingParens("Missing or Misplaced Parentheses")
l = len(expr_terms)
expr_terms.pop() # remove last parentheses
return (l, expr_terms)
def get_function_args(lexbuf, demarcation):
"""
get_function_args(lexbuf, demarcation) gets the function arguments
in lexbuf, as separated by demarcation characters
REQUIRES: args can have demarcation chracters inside of them,
but they must be separated by parentheses, e.g.
if comma "," is the demarcation between args,
then f(g, h(3, 4)) is legal since the demarcation in inner function call
h is separated by parens
Raises MissingParens("lexbuf missing right parens") if a parens
on the right is missing (not closed parens)
"""
def get_function_args_helper(lexbuf, demarcation, stack, arg, args_list):
if lexbuf == []:
if arg != []:
args_list.append(arg)
return args_list
pair = lexbuf[0]
_, val = pair
rem = lexbuf[1:]
if stack == [] and val == demarcation:
args_list.append(arg)
return get_function_args_helper(rem, demarcation, stack, [], args_list)
if val == lexer.LPAREN:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
elif val == lexer.RPAREN:
if len(stack) >= 1:
if stack[0] == lexer.LPAREN:
stack.pop()
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
stack.append(val)
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
else:
raise MissingParens("lexbuf missing right parens")
else:
arg.append(pair)
return get_function_args_helper(rem, demarcation, stack, arg, args_list)
return get_function_args_helper(lexbuf, demarcation, [], [], [])
# def lookahead(lexbuf, pos):
# """
# lookahead(lexbuf, pos) tells the precendence of the OPERATOR! at the lookeahad
# position [pos] in [lexbuf]
# Returns pair of op and precedence
# REQUIRES: lexbuf[pos] is an operator
# REQUIRES: pos in lexbuf!
# """
# op = lexbuf[pos]
# symbol = op[1]
# assert symbol in lexer.OPERATIONS
# return op, get_precedence(symbol, PRECENDENCE_MAP)
# def is_elt(pair):
# """
# is_elt(pair) is True iff pair is a elxer.Integer, Variabl;e, Keyword, Lparen
# rParen
# pair si 2 tuple from the lexer lexbuf list
# """
# elt_typ, elt_val = pair
# if elt_typ == lexer.INTEGER:
# return True
# elif elt_typ == lexer.VARIABLE:
# return True
# elif elt_typ == lexer.KEYWORD and elt_val in lexer.UNOPS:
# return True
# elif elt_val == lexer.LPAREN:
# return True
# elif elt_val == lexer.RPAREN:
# return True
# return False
# def is_op(pair):
# """
# is_op(pair) is True iff pair is a lexer Operation, incljuing binop or unops
# """
# _, elt_val = pair
# return elt_val in lexer.OPERATIONS
# def parse_expr(is_parse_op, count, precedence, stack, lexbuf):
# # ----------- 0 tokens remaining = nothing more to parse ----------
# # ----------- Used to escape if start call has 0 tokens ----------
# if lexbuf == []:
# return (count, reduce_stack(precedence, stack))
# if not is_parse_op:
# elt_length, elt_ast = match_elt(lexbuf)
# rem = lexbuf[elt_length:]
# if len(rem) <= 1:
# stack.append(elt_ast)
# return (count + elt_length), reduce_stack(precedence, stack)
# _, sym_precedence = lookahead(lexbuf, elt_length)
# rem = lexbuf[elt_length + 1:]
# if sym_precedence == precedence:
# stack.append(elt_ast)
# return parse_expr(not is_parse_op, count + elt_length, precedence, stack, rem)
# elif sym_precedence > precedence:
# new_stack = []
# new_stack.append(elt_ast)
# inner_parse_length, inner_ast = parse_expr(not is_parse_op,
# elt_length, sym_precedence, new_stack, rem)
# rem = rem[inner_parse_length:]
# # need to do a lookahead after parsing inner ast
# # depended! on next operator ! stack.append(inner_ast)
# return parse_expr(not is_parse_op, count + elt_length + inner_parse_length, precedence, stack, rem)
# else:
# stack.append(elt_ast)
# return (count + elt_length), reduce_stack(precedence, stack)
# else:
# stack.append(lexbuf[0])
# return parse_expr(not is_parse_op, count + 1, precedence, stack, lexbuf[1:])
# list of stacks
# [stack 1, stack 2, stack 3, stack 4....] with stack based on precedence level
# when a element is read, it is held until an operator is found
# if no associated operator [e.g. the buffer terminates],>
# add to the current precendence level parsing at
# reduce stack
# else wait until next operator:
# if operator at same precendence lvel
# add to current precendence level
# continue parsing
# elseif operator at lower precendence level:
# add to current precedence level
# reduce stack
# else:
# add to new higher precedence level stack
# parse at that new level
# def reduce_super_stack(stack):
# i = len(stack) - 1
# while i >= 0:
# substack = stack[i]
# if substack != []:
# reduced_substack = reduce_stack(i + 1, substack)
# if i != 0 and reduced_substack != []:
# stack[i - 1].append(reduced_substack)
# stack[i] = []
# i -= 1
# return reduce_stack(1, stack[0])
# def parse_op(count, held_ast, precedence, stack, lexbuf):
# assert lexbuf != []
# op, sym_precedence = lookahead(lexbuf, 0)
# rem = lexbuf[1:]
# if sym_precedence == precedence:
# stack[sym_precedence - 1].append(held_ast)
# stack[sym_precedence - 1].append(op)
# return parse_expr_helper(count + 1, None, sym_precedence, stack, rem)
# elif sym_precedence > precedence:
# stack[sym_precedence - 1].append(held_ast)
# stack[sym_precedence - 1].append(op)
# ast_length, higher_precedence_ast = parse_expr_helper(
# 0, None, sym_precedence, stack, rem)
# rem = rem[ast_length:]
# if rem != []:
# return parse_op(count + ast_length + 1, higher_precedence_ast, precedence, stack, rem)
# # stack[precedence - 1].append(higher_precedence_ast)
# # stack[sym_precedence - 1] = []
# return (count + 1), reduce_super_stack(stack)
# else:
# finished_stack = stack[precedence - 1]
# finished_stack.append(held_ast)
# reduced_ast = reduce_stack(precedence, finished_stack)
# stack[precedence - 1] = []
# stack[sym_precedence - 1].append(reduced_ast)
# stack[sym_precedence - 1].append(op)
# return parse_expr_helper(count + 1, None, sym_precedence, stack, rem)
# def parse_expr_helper_2(count, held_ast, precedence, stack, lexbuf):
# if lexbuf == []:
# return reduce_super_stack(stack)
# pair = lexbuf[0]
# if is_elt(pair):
# elt_length, elt_ast = match_elt(lexbuf)
# rem = lexbuf[elt_length:]
# if rem != []:
# return (count + elt_length), parse_op((count + elt_length), elt_ast, precedence, stack, rem)
# stack[precedence - 1].append(elt_ast)
# # substack = stack[precedence - 1]
# return (count + elt_length), reduce_super_stack(stack)
# # return (count + elt_length), reduce_stack(precedence, substack)
def match_elt(lexbuf):
"""
match_elt(lexbuf) is the length of the element and the element object
e.g. 1, Integer(1)
or 3, Bop(...)
if RPAREN is first character, will raise unmatched parentheses erro
REQUIRES: 0th element is in lexbuf: len(lexbuf )> 1
REQUIRES: elt is 0th element in lexbuf
"""
elt_typ, elt_val = lexbuf[0]
if elt_typ == lexer.INTEGER:
return 1, IntValue(elt_val)
elif elt_typ == lexer.VARIABLE:
if len(lexbuf) > 1:
_, la_val = lexbuf[1]
if la_val == lexer.LPAREN:
length, middle_terms = get_between_brackets(lexbuf, 2)
split_args = get_function_args(middle_terms, lexer.COMMA)
args_pairs = list(map(lambda args_buffer: parse_expr(
args_buffer), split_args))
args = list(map(lambda pair: pair, args_pairs))
return 2 + length, Apply(elt_val, args)
return 1, VarValue(elt_val)
elif elt_typ == lexer.KEYWORD and elt_val in lexer.UNOPS:
_, next_pair = lexbuf[1]
if next_pair == lexer.LPAREN:
length, middle_terms = get_between_brackets(lexbuf, 2)
parsed_middle_ast = parse_expr(middle_terms)
return 2 + length, Unop(elt_val, parsed_middle_ast)
elif next_pair == lexer.RPAREN:
raise UnmatchedParenError(
"Unmatched right parenthesis %s" % (next_pair))
length = 1
unop_typ, unop_val_ast = lexbuf[1]
if unop_typ == lexer.INTEGER:
unop_val_ast = IntValue(unop_val_ast)
elif unop_typ == lexer.VARIABLE:
unop_val_ast = VarValue(unop_val_ast)
return (1 + length), Unop(elt_val, unop_val_ast)
elif elt_val == lexer.LPAREN:
length, middle_terms = get_between_brackets(lexbuf, 1)
parens_ast = parse_expr(middle_terms)
return (1 + length), parens_ast
elif elt_val == lexer.RPAREN:
raise UnmatchedParenError(
"Unmatched right parenthesis %s" % (elt_val))
def fold_stack(stack):
def fold_stack_helper(stack, fold_item):
if len(stack) == 0:
return stack
if len(stack) == 1:
l = len(stack)
if fold_item != []:
stack[l - 1].append(fold_item)
reduced_ast = reduce_stack(l, stack[l - 1])
return reduced_ast
l = len(stack)
if fold_item != []:
stack[l - 1].append(fold_item)
reduced_ast = reduce_stack(l, stack[l - 1])
new_stack = deepcopy(stack[:(l-1)])
return fold_stack_helper(new_stack, reduced_ast)
return fold_stack_helper(stack, [])
def parse_expr_helper(lexbuf):
stack = [[] for _ in range(N_PRECEDENCE_LEVELS)]
precendence = 1
l = len(lexbuf)
carry_ast = None
i = 0
while (i < l):
elt = carry_ast
if carry_ast != None:
elt = carry_ast
rem = lexbuf[i:]
elt_length = 0
else:
elt_length, elt = match_elt(lexbuf[i:])
rem = lexbuf[i + elt_length:]
if rem == []:
stack[precendence - 1].append(elt)
reduced_ast = reduce_stack(precendence, stack[precendence - 1])
i += elt_length
stack[precendence - 1] = [reduced_ast]
stack = fold_stack(stack)
else:
op = rem[0] # if it exists
_, op_val = op
op_prec = get_precedence(op_val, PRECENDENCE_MAP)
if op_prec == precendence:
stack[op_prec - 1].append(elt)
stack[op_prec - 1].append(op)
i += elt_length + 1
elif op_prec > precendence:
stack[op_prec - 1].append(elt)
stack[op_prec - 1].append(op)
i += elt_length + 1
precendence = op_prec
else: # op_prec < precendence:
stack[precendence - 1].append(elt)
lower_ast = reduce_stack(precendence, stack[precendence - 1])
stack[precendence - 1] = []
stack[op_prec - 1].append(lower_ast)
stack[op_prec - 1].append(op)
i += elt_length + 1 # skip operator and redo on next run
precendence = op_prec
return stack
def parse_expr(lexbuf):
return parse_expr_helper(lexbuf)
if __name__ == "__main__":
print(parse_expr(lexer.lex("3")))
print(parse_expr(lexer.lex("3 + 4")))
print(parse_expr(lexer.lex("3 + 4 + 6")))
print(parse_expr(lexer.lex("3 + 4 + 5 + 6")))
print(parse_expr(lexer.lex("-3")))
print(parse_expr(lexer.lex("-3 + 4")))
print(parse_expr(lexer.lex("-3 + -4")))
print(parse_expr(lexer.lex("3 + -4")))
print(parse_expr(lexer.lex("-3 + 4 + 6")))
print(parse_expr(lexer.lex("3 + -4 + 6")))
print(parse_expr(lexer.lex("3 + 4 + -6")))
print(parse_expr(lexer.lex("3 + -4 + -6")))
print(parse_expr(lexer.lex("-3 + -4 + 6")))
print(parse_expr(lexer.lex("-3 + 4 + -6")))
print(parse_expr(lexer.lex("-3 + -4 + -6")))
print(parse_expr(lexer.lex("-3 + 4 + 5 + 6")))
print(parse_expr(lexer.lex("3 + -4 + 5 + 6")))
print(parse_expr(lexer.lex("3 + 4 + -5 + 6")))
print(parse_expr(lexer.lex("3 + 4 + 5 + -6")))
print(parse_expr(lexer.lex("-3 + -4 + 5 + 6")))
print(parse_expr(lexer.lex("3 + -4 + -5 + 6")))
print(parse_expr(lexer.lex("3 + 4 + -5 + -6")))
print(parse_expr(lexer.lex("-3 + 4 + 5 + -6")))
print(parse_expr(lexer.lex("3 + -4 + 5 + -6")))
print(parse_expr(lexer.lex("3 + -4 + -5 + 6")))
print(parse_expr(lexer.lex("(3)")))
print(parse_expr(lexer.lex("(((3)))")))
print(parse_expr(lexer.lex("(3 + 4)")))
print(parse_expr(lexer.lex("3 + (4 + 6)")))
print(parse_expr(lexer.lex("(3 + 4 + 6)")))
print(parse_expr(lexer.lex("(3 + 4) + 6")))
print(parse_expr(lexer.lex("(3) + 4 + 5 + 6")))
print(parse_expr(lexer.lex("(3) + (4 + 5) + 6")))
print(parse_expr(lexer.lex("((3) + (4 + 5)) + 6")))
print(parse_expr(lexer.lex("(3) + ((4 + 5) + 6)")))
print(parse_expr(lexer.lex("(-3)")))
print(parse_expr(lexer.lex("(((-3)))")))
print(parse_expr(lexer.lex("(3 + -4)")))
print(parse_expr(lexer.lex("(-3 + -4)")))
print(parse_expr(lexer.lex("-3 + (4 + 6)")))
print(parse_expr(lexer.lex("-3 + (-4 + 6)")))
print(parse_expr(lexer.lex("-3 + (-4 + -6)")))
print(parse_expr(lexer.lex("3 + (-4 + 6)")))
print(parse_expr(lexer.lex("3 + (4 + -6)")))
print(parse_expr(lexer.lex("3 + (-4 + -6)")))
print(parse_expr(lexer.lex("(3 + 4 + 6)")))
print(parse_expr(lexer.lex("(3 + 4) + 6")))
print(parse_expr(lexer.lex("(-3 + 4 + 6)")))
print(parse_expr(lexer.lex("(-3 + -4) + -6")))
print(parse_expr(lexer.lex("(3) + 4 + 5 + 6")))
print(parse_expr(lexer.lex("(3) + (4 + 5) + 6")))
print(parse_expr(lexer.lex("((3) + (4 + 5)) + 6")))
print(parse_expr(lexer.lex("(3) + ((-4 + 5) + 6)")))
print(parse_expr(lexer.lex("(3) + ((-4 + -5) + 6)")))
print(parse_expr(lexer.lex("(3) + ((4 + -5) + 6)")))
print(parse_expr(lexer.lex("(3) + ((-4 + -5) + -6)")))
print(parse_expr(lexer.lex("3 * 4")))
print(parse_expr(lexer.lex("3 * 4 + 6")))
print(parse_expr(lexer.lex("3 + 4 * 6")))
print(parse_expr(lexer.lex("3 * 4 * 6")))
print(parse_expr(lexer.lex("3 * 4 + 5 + 6")))
print(parse_expr(lexer.lex("3 + 4 * 5 + 6")))
print(parse_expr(lexer.lex("3 + 4 + 5 * 6")))
print(parse_expr(lexer.lex("3 + 4 * 5 * 6")))
print(parse_expr(lexer.lex("3 * 4 * 5 + 6")))
print(parse_expr(lexer.lex("3 * 4 + 5 * 6")))
print(parse_expr(lexer.lex("3 * 4 * 5 * 6")))
print(parse_expr(lexer.lex("-3 * 4")))
print(parse_expr(lexer.lex("-3 * -4")))
print(parse_expr(lexer.lex("3 * -4")))
print(parse_expr(lexer.lex("-3 * -4 + 6")))
print(parse_expr(lexer.lex("-3 + 4 * 6")))
print(parse_expr(lexer.lex("3 * -4 * 6")))
print(parse_expr(lexer.lex("3 * -4 + 5 + 6")))
print(parse_expr(lexer.lex("3 + -4 * -5 + 6")))
print(parse_expr(lexer.lex("3 + -4 + 5 * -6")))
print(parse_expr(lexer.lex("-3 + -4 * -5 * -6")))
print(parse_expr(lexer.lex("-3 * -4 * -5 + -6")))
print(parse_expr(lexer.lex("-3 * -4 + -5 * -6")))
print(parse_expr(lexer.lex("3 * -4 * -5 * -6")))
print(parse_expr(lexer.lex("-3 * 4")))
print(parse_expr(lexer.lex("-3 ** -4")))
print(parse_expr(lexer.lex("3 * -4")))
print(parse_expr(lexer.lex("-3 * -4 + 6")))
print(parse_expr(lexer.lex("-3 + 4 * 6")))
print(parse_expr(lexer.lex("3 * -4 * 6")))
print(parse_expr(lexer.lex("3 * -4 + 5 + 6")))
print(parse_expr(lexer.lex("3 + -4 * -5 + 6")))
print(parse_expr(lexer.lex("3 + -4 + 5 * -6")))
print(parse_expr(lexer.lex("-3 + -4 * -5 * -6")))
print(parse_expr(lexer.lex("-3 * -4 * -5 + -6")))
print(parse_expr(lexer.lex("-3 * -4 + -5 ** -6")))
print(parse_expr(lexer.lex("3 ** -4 ** -5 * -6")))
print(parse_expr(lexer.lex("unary()")))
print(parse_expr(lexer.lex("f(1, 2 + 3)")))
print(parse_expr(lexer.lex("f(g(3, 4), h(g(9), 8))")))
print(parse_expr(lexer.lex("-x * 4")))
print(parse_expr(lexer.lex("-3 ** -y")))
print(parse_expr(lexer.lex("3 * -4")))
print(parse_expr(lexer.lex("-3 * -4 + 6")))
print(parse_expr(lexer.lex("-3 + 4 * 6")))
print(parse_expr(lexer.lex("3 * -4 * 6")))
print(parse_expr(lexer.lex("3 * -4 + 5 + 6")))
print(parse_expr(lexer.lex("3 + -4 * -5 + 6")))
print(parse_expr(lexer.lex("x + -4 + 5 * -6")))
print(parse_expr(lexer.lex("-3 + -4 * -5 * -6")))
print(parse_expr(lexer.lex("-3 * -y * -z + -6")))
print(parse_expr(lexer.lex("-3 * -4 + -5 ** -6")))
print(parse_expr(lexer.lex("3 ** -4 ** -k * -6")))
print(parse_expr(lexer.lex("-(3 + 4) * 4")))
print(parse_expr(lexer.lex("-(-3 + 4) * 4")))
print(parse_expr(lexer.lex("-(3 + -4) * 4")))
print(parse_expr(lexer.lex("-(-3 + -4) * 4")))
print(parse_expr(lexer.lex("-3 ** -(2 * 3)")))
print(parse_expr(lexer.lex("-3 ** -(-2 * 3)")))
print(parse_expr(lexer.lex("-3 ** -(2 * -3)")))
print(parse_expr(lexer.lex("-3 ** -(-2 * -3)")))
print(parse_expr(lexer.lex("3 * -(1 + 2)")))
print(parse_expr(lexer.lex("3 * -(1 + -2)")))
print(parse_expr(lexer.lex("3 * -(-1 + 2)")))
print(parse_expr(lexer.lex("3 * -(-1 + -2)")))
print(parse_expr(lexer.lex("-(3 + 4) * -2 + 4")))
print(parse_expr(lexer.lex("-(-3 + 4) * -2 + 4")))
print(parse_expr(lexer.lex("-(3 + -4) * -2 + 4")))
print(parse_expr(lexer.lex("-(-3 + -4) * -2 + 4")))
print(parse_expr(lexer.lex("-3 ** -(2 * 3) * 2 + 1")))
print(parse_expr(lexer.lex("-3 ** -(-2 * 3) * -2 + 1")))
print(parse_expr(lexer.lex("-3 ** -(2 * -3) * -2 + 1")))
print(parse_expr(lexer.lex("-3 ** -(-2 * -3) * -2 + 1 ")))
print(parse_expr(lexer.lex("3 * -(1 + 2) * -4 + 1")))
print(parse_expr(lexer.lex("3 * -(1 + -2) * -4 + 1")))
print(parse_expr(lexer.lex("3 * -(-1 + 2) * -10 + 0")))
print(parse_expr(lexer.lex("3 * -(-1 + -2) * -10 + 0")))
# print(parse_expr(lexer.lex("3 * 4")))
# print(parse_expr(lexer.lex("-3")))
# print(parse_expr(lexer.lex("-3 * -4")))
# print(parse_expr(lexer.lex("3 + 4 * 2")))
# print(parse_expr(lexer.lex("3 * 4 + 2")))
# print(parse_expr(lexer.lex("3 * 4 * 2")))
# print(parse_expr(lexer.lex("3 * 4 * 2 + 2 * 3 + 1 * 2 * 3")))