692: #line 921 "lalr1_parser.ipk" 693: 694: def pr_tab(p): 695: at = p.action_table 696: gt = p.goto_table 697: print 'actions' 698: for i in range(len(at)): 699: print i,':',at[i] 700: print 'gotos' 701: for i in range(len(gt)): 702: print i,':',gt[i] 703: 704: def _test1(): 705: # the token interface will be defined in 706: # a wrapper module and in the lexer. 707: # this is the format the information will be stored in 708: # 709: # first, define the tokens 710: # 711: #(id, plus, times, lparen, rparen, eof) = range(6) 712: # 713: # then, define how you want them to appear 714: # in the documentation of the output file 715: # 716: toks = ["id", "+", "*", "(", ")","$"] 717: (id, plus, times, lparen, rparen, eof) = toks 718: 719: # 720: # define the productions (LHS=left hand side, RHS=right hand side) 721: # 722: prods = map(lambda x: Production(x[0], x[1]), [("E", ["E", plus, "T"]), 723: ("E", ["T"]), 724: ("T", ["T", times, "F"]), 725: ("T", ["F"]), 726: ("F", [lparen, "E", rparen]), 727: ("F", [id])]) 728: g = LALRGrammar(prods, "E") 729: # 730: 731: # 732: # define functions for the parser to use 733: # 734: def fadd(prod,*args): 735: print "adding %d with %d" % (args[0][1], args[2][1]) 736: return args[0][1] + args[2][1] 737: 738: def fdummy(prod,*args): 739: print "calling fdummy with args %s" % `args` 740: return args[0][1] 741: 742: def ftimes(prod,*args): 743: print "multiplying %d with %d" % (args[0][1], args[2][1]) 744: return args[0][1] * args[2][1] 745: 746: def fparens(prod,*args): 747: print "handling parens, returning whats in between" 748: return args[1][1] 749: 750: 751: # 752: # register the functions 753: # 754: for i in range(len(g.productions)): 755: if len(g.productions[i].RHS) == 1: 756: g.productions[i].func=fdummy 757: g.productions[0].func=fadd 758: g.productions[2].func=ftimes 759: g.productions[4].func=fparens 760: # 761: # produce the parser 762: # 763: pr_tab(g) 764: 765: # 766: # this is the input as would be returned by the Lexer 767: # (3+(4*2))*2*5 =110 768: input = [(lparen, "("), (id, 3), (plus, "+"), 769: (lparen, "("), (id, 4), (times, "*"), (id, 2), (rparen, ")"), 770: (rparen, ")"), (times, "*"), 771: (id, 2), (times, "*"), (id, 5) ] 772: 773: res = g.parse(input) 774: print 'RESULT=',res 775: