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:
706:
707:
708:
709:
710:
711:
712:
713:
714:
715:
716: toks = ["id", "+", "*", "(", ")","$"]
717: (id, plus, times, lparen, rparen, eof) = toks
718:
719:
720:
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:
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:
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:
762:
763: pr_tab(g)
764:
765:
766:
767:
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: