* Example program-7 (SNOBOL) 10/31/2002 * 1. Recursive function * 2. Use of BAL and ARBNO(P) * 3. Conversion of an infix expression of the form: * subexp1 op subexp2 * into the following form: * op subexp1(yet to be converted) subexp2(yet to be converted), * provides the basis for recursive definition of a function. &Anchor = 1 &Trim = 1 &FTrace = 20 Plus = "+" Term = BAL Plus PlusPat = ARBNO(Term) $ Exp Fail | + *Differ(Exp) Tab(*(Size(Exp) - 1)) . First Len(1) . OP Rem . Second * Note that *Tab(Size(Exp1)) and Tab(*Size(Exp1)) are the same. Times = "*" Fact = BAL Times TimesPat = ARBNO(Fact) $ Exp Fail | + *Differ(Exp) Tab(*(Size(Exp) - 1)) . First Len(1) . OP Rem . Second define('Prefix(EX)First,Second,OP') ;* Locals are critical. * Main Program starts here ********************* Repeat Infix = Input :F(NoMore) Count = Count + 1 Output = "INPUT COUNT:" Count Output = "INPUT FORM:: " Infix Output = "PREFIX FORM:: " Prefix(Infix) :(Repeat) NoMore Output = Output = "TOTAL COUNT OF INPUTS:::" Count :(End) * Function Definition of "Prefix(EX)" Prefix Strip = "(" BAL . EXX ")" RPOS(0) Again EX Strip = EXX :S(Again) EX PlusPat = OP Prefix(First) Prefix(Second) :F(NEXT) Prefix = EX :(Return) NEXT EX TimesPat = OP Prefix(First) Prefix(Second) Prefix = EX :(Return) End ((A+B*(C+D+E*F))) (A+B+C)+D*E+F (A+B+C)*((D+F+T))