Re: [Spirit-general] Calculator example.

 Re: [Spirit-general] Calculator example. From: Joel de Guzman - 2003-02-28 00:26:59 ```Navi Singh wrote: > x = 5 + 6 > parsing succeeded > x = (5 + 6) > result = 11 > y = 8 + x=4 > ^--parsing failed here > y = 8 + (x = 4) > parsing succeeded > y = (8 + (x = 4)) > result = 12 > > > My question is: why is y = 8 + x=4 not ok, but y = 8 + (x = 4) ok? You guessed it. Because of precedence. > How do I modify the rules to make both work? I want = to have a > lower precedence than +- But that's the way it should be in most grammars that I know. I'm confused. = already has a lower precedence in your grammar. Thus: y = 8 + x=4 is actually parsed as: y = (8 + x)=4 Which does not conform to your grammar where the LHS is expected to be a variable. You'll need the explicit parens around x=4. The results are correct AFAICT. Cheers, -- Joel de Guzman joel at boost-consulting.com http://www.boost-consulting.com http://spirit.sf.net ```

 [Spirit-general] Calculator example. From: Navi Singh - 2003-02-27 18:44:36 ```I am studying the calculator examples that come with Spirit 1.3.1. I tried the following: rule<> assignment, expression, expr, term, factor, variable; variable = lexeme[(alpha >> *(alnum | '_'))]; factor = real_p[my_nodes::do_num(my_stack)] | variable[my_nodes::do_var(my_stack, my_syms)] | '(' >> expression >> ')' | ('-' >> factor)[my_nodes::do_neg(my_stack)]; term = factor >> *( ('*' >> factor)[my_nodes::do_mul(my_stack)] | ('/' >> factor)[my_nodes::do_div(my_stack)] ); expr = term >> *( ('+' >> term)[my_nodes::do_add(my_stack)] | ('-' >> term)[my_nodes::do_sub(my_stack)] ); assignment = variable[my_nodes::do_var(my_stack, my_syms)] >> '=' >> expr[my_nodes::do_assign(my_stack)]; expression = assignment | expr >> *assignment; On testing it, the following output is produced: ///////////////////////////////////////////////////////// The simplest working calculator... ///////////////////////////////////////////////////////// Type an expression...or [q or Q] to quit x = 5 + 6 parsing succeeded x = (5 + 6) result = 11 y = 8 + x=4 ^--parsing failed here y = 8 + (x = 4) parsing succeeded y = (8 + (x = 4)) result = 12 My question is: why is y = 8 + x=4 not ok, but y = 8 + (x = 4) ok? How do I modify the rules to make both work? I want = to have a lower precedence than +- Thanks. Navi __________________________________________________ Do you Yahoo!? Yahoo! Tax Center - forms, calculators, tips, more http://taxes.yahoo.com/ ```
 Re: [Spirit-general] Calculator example. From: Joel de Guzman - 2003-02-28 00:26:59 ```Navi Singh wrote: > x = 5 + 6 > parsing succeeded > x = (5 + 6) > result = 11 > y = 8 + x=4 > ^--parsing failed here > y = 8 + (x = 4) > parsing succeeded > y = (8 + (x = 4)) > result = 12 > > > My question is: why is y = 8 + x=4 not ok, but y = 8 + (x = 4) ok? You guessed it. Because of precedence. > How do I modify the rules to make both work? I want = to have a > lower precedence than +- But that's the way it should be in most grammars that I know. I'm confused. = already has a lower precedence in your grammar. Thus: y = 8 + x=4 is actually parsed as: y = (8 + x)=4 Which does not conform to your grammar where the LHS is expected to be a variable. You'll need the explicit parens around x=4. The results are correct AFAICT. Cheers, -- Joel de Guzman joel at boost-consulting.com http://www.boost-consulting.com http://spirit.sf.net ```
 Re: [Spirit-general] Calculator example. From: Navi Singh - 2003-02-28 18:09:23 ```--- Joel de Guzman wrote: > But that's the way it should be in most grammars that I know. I'm > confused. = already has a lower precedence in your grammar. > > Thus: > > y = 8 + x=4 > > is actually parsed as: > > y = (8 + x)=4 > > Which does not conform to your grammar where the LHS is expected to > be a variable. You'll need the explicit parens around x=4. The results > are correct AFAICT. Thanks, Joel. You cleared up a bug in my thought process :) Thank you for the fantastic work with the Spirit parser generator. My interest in Spirit is motivated by the need for a parser for a script such as the following (I am writing a parallel processing version of Non-Linear Regression): I hope that this will be a fairly easy exercise. Is there anything that I should watch out for while working on such a script? Thanks again, Navi sample Non-linear regression script. ==================================== parallel() { x[1]@=load("NLIN2_a"); x[2]@=load("NLIN2_b"); x[3]@=load("NLIN2_c"); nlin(x[1:3]@) { args() { maxiter = 50; tau = 0.99; converge = 0.001; output="nlin2_abc.txt"; printmatrix=yes; # intercept = yes; } parms() { c_age00=0.5; c_age0 =0; c_age1 =0; c_age2 =0; c_age3 =0; c_age4 =0; c_age5 =0; c_mam =0; c_jja =0; c_djf =0; c_mar =0; c_may =0; c_jun =0; c_aug =0; c_sep =0; c_nov =0; c_dec =0; c_feb =0; c_lock0=0; c_lock1=0; c_lock2=0; c_lock3=0; } init() { z@ = load("Int_Rates"); array mrt(z@, int_rate); rate_start_date = cell(z@, startdate, 0); drop z@; } #define lockin function j=min(12*(int(fdate/100)-int(rate_start_date/100)) + mod(fdate,100)-mod(rate_start_date,100) + 3, 3); jp=max(0,j-1) - 1; jpp=max(0,j-2) - 1; eit=100*(wac - 0.5*(mrt[jp]+mrt[jpp])); #constrain condition on eit applying on weight variable; pbal0=pbal*(eit<12.5); _weight_=pbal0; lock0=0.01*(min(eit,0) - min(eit-(-300),0)); lock1=0.01*(min(eit-(-50),0) - min(eit-(-300),0)); lock2=0.01*(min(eit-(-100),0)- min(eit-(-300),0)); lock3=0.01*(min(eit-(-200),0)- min(eit-(-300),0)); lockin=exp(c_lock0*lock0 + c_lock1*lock1 + c_lock2*lock2 + c_lock3*lock3); #define aging function. aging= c_age00 + c_age0*max(age,0) + c_age1*max(age-14,0) + c_age2*max(age-26,0) + c_age3*max(age-270,0) + c_age4*max(age-312,0) + c_age5*max(age-336,0); #define seasoning function asofmo=mod(fdate,100); jan=0; feb=0; mar=0; apr=0; may=0; jun=0; jul=0; aug=0; sep=0; oct=0; nov=0; dec=0; mam=0; jja=0; son=0; djf=0; if(asofmo==1) { djf=1; jan=1; } else if(asofmo==2) { djf=1; feb=1; } else if(asofmo==3) { mam=1; mar=1; } else if(asofmo==4) { mam=1; apr=1; } else if(asofmo==5) { mam=1; may=1; } else if(asofmo==6) { jja=1; jun=1; } else if(asofmo==7) { jja=1; jul=1; } else if(asofmo==8) { jja=1; aug=1; } else if(asofmo==9) { son=1; sep=1; } else if(asofmo==10){ son=1; oct=1; } else if(asofmo==11){ son=1; nov=1; } else if(asofmo==12){ djf=1; dec=1; } seacov=exp( c_mam*(mam-son)+c_jja*(jja-son)+c_djf*(djf-son) +c_mar*(mar-apr)+c_may*(may-apr) +c_jun*(jun-jul)+c_aug*(aug-jul) +c_sep*(sep-oct)+c_nov*(nov-oct) +c_dec*(dec-jan)+c_feb*(feb-jan) ); # model function; hover0=aging*seacov*lockin; hover=max(0,min(100,hover0)); model smm=hover; derivatives() { der.c_age00=seacov*lockin; der.c_age0=max(age,0)*hover0; der.c_age1=max(age-14,0)*hover0; der.c_age2=max(age-26,0)*hover0; der.c_age3=max(age-270,0)*hover0; der.c_age4=max(age-312,0)*hover0; der.c_age5=max(age-336,0)*hover0; der.c_mam=(mam-son)*hover0; der.c_jja=(jja-son)*hover0; der.c_djf=(djf-son)*hover0; der.c_mar=(mar-apr)*hover0; der.c_may=(may-apr)*hover0; der.c_jun=(jun-jul)*hover0; der.c_aug=(aug-jul)*hover0; der.c_sep=(sep-oct)*hover0; der.c_nov=(nov-oct)*hover0; der.c_dec=(dec-jan)*hover0; der.c_feb=(feb-jan)*hover0; der.c_lock0=lock0*hover0; der.c_lock1=lock1*hover0; der.c_lock2=lock2*hover0; der.c_lock3=lock3*hover0; } } } __________________________________________________ Do you Yahoo!? Yahoo! Tax Center - forms, calculators, tips, more http://taxes.yahoo.com/ ```
 Re: [Spirit-general] Calculator example. From: Joel de Guzman - 2003-03-03 15:35:12 ```Navi Singh wrote: > Thanks, Joel. You cleared up a bug in my thought process :) Thank > you for the fantastic work with the Spirit parser generator. My > interest in Spirit is motivated by the need for a parser for a script > such as the following (I am writing a parallel processing version of > Non-Linear Regression): [snip] Hi Navi, Most welcome. Pardon the delay in replying. Some email gremlins ate my emails for lunch again! Actually, I had a net problem the past 2 days. > I hope that this will be a fairly easy exercise. Is there anything > that I should watch out for while working on such a script? Hmmm. I'm not sure I can give a fairly good tip. I haven't done such things. Anyway, you know where to ask when you get into some technical obstacles. We're here to help. -- Joel de Guzman joel at boost-consulting.com http://www.boost-consulting.com http://spirit.sf.net ```