// $Id: PepaParser.cup,v 1.10 1999/07/27 16:35:24 gcla Exp $ // file : PepaParser.cup // description : Parser for Pepa syntax // project : PEPAroni (mjta) // author : Graham Clark // Time-stamp : <19 Jul 99 17:00:11 gcla> package pepa.process; import java_cup.runtime.*; import pepa.process.PepaToken.*; import pizza.util.Set; import pizza.lang.*; import pizza.util.*; import pizza.util.Option.*; import pepa.process.Rate.*; import pepa.process.Debug; import eduni.simjava.*; /* Preliminaries to set up and use the scanner. */ init with {: PepaLexer.init(); :}; scan with {: return PepaLexer.next_token(); :}; /* Terminals (tokens returned by the scanner). */ terminal COMMA, DOT, TOP, LPAREN, RPAREN, SEMI, EQUALS, PAR; terminal LCOOP, RCOOP, MINUS, MULT, SUM, HIDE, HASH, PERC; terminal EXP, NORM, UNIF, LSET, RSET, PEOF; terminal Yytoken STRN, NUMB; /* Non terminals */ /* non terminal procdefn_list; non terminal ProcessDefn procdefn_part, procdefn; non terminal Process proc; */ /*non terminal procdefns, procdefn_part;*/ non terminal defns, defn; non terminal Process proc, process; non terminal ProcessDefn procdefn; non terminal VarDefn vardefn; non terminal Activity activity; non terminal Action anaction; /* can this possibly be Set here? */ /*non terminal SetLEFTANGLEActionRIGHTANGLE coopset, actionlist;*/ non terminal ActionSet actions, actionset, actionlist, coopset; non terminal Dist dist; non terminal Double real; non terminal Rate rate; /* non terminal Double exponential; non terminal MeanVar normal; */ /*non terminal Integer expr;*/ /* Precedences */ /* precedence left PLUS, MINUS; precedence left TIMES, DIVIDE, MOD; precedence left UMINUS; */ /* gcla later - BUG - fix nonassoc of *, / */ /* Hack - lcoop is not a parse object! */ precedence left LCOOP, PAR; precedence nonassoc HIDE, MULT; precedence left SUM; precedence nonassoc MINUS; precedence nonassoc DOT; start with process; /* The grammar */ process ::= defns proc:p2 {: //Symbol spd = (Symbol)pd; //ProcessDefn pr = (ProcessDefn)spd.value; //Debug.println(5, "first production"); //ProcessDefn pr = (ProcessDefn)pd; //Debug.println(5, "first production2"); //String name = (String)pr.name; //Process p = (Process)pr.proc; //Debug.println(5, "Adding process defn"); //PepaProcess.addDef(name, p); //Debug.println(5, "proc is " + p.toString()); //RESULT = PepaProcess.Var("darn"); Debug.println(5, "proc first rule is " + p2.toString()); RESULT = p2; :} ; defns ::= defn defns | ; defn ::= HASH procdefn:pd SEMI {: Debug.println(5, "[procdef] first production"); ProcessDefn pr = (ProcessDefn)pd; Debug.println(5, "first production2"); String name = (String)pr.name; Process p = (Process)pr.proc; Debug.println(5, "Adding process defn"); PepaProcess.addDef(name, p); Debug.println(5, "proc is " + p.toString()); :} | PERC vardefn:vd SEMI {: Debug.println(5, "[vardef] first production"); VarDefn vdef = (VarDefn)vd; String name = (String)vdef.name; Double r = (Double)vdef.v; Debug.println(5, "Adding var defn"); Peparoni.addDef(name, r); Debug.println(5, "value of "+name+" is " + r.toString()); :} ; /* procdefns ::= procdefn_part procdefns | ; procdefn_part ::= HASH procdefn:pd SEMI {: Debug.println(5, "first production"); ProcessDefn pr = (ProcessDefn)pd; Debug.println(5, "first production2"); String name = (String)pr.name; Process p = (Process)pr.proc; Debug.println(5, "Adding process defn"); PepaProcess.addDef(name, p); Debug.println(5, "proc is " + p.toString()); :} ; */ vardefn ::= STRN:s EQUALS real:r {: Yytoken toks = (Yytoken)s; String name = s.m_text; Double valu = (Double)r; VarDefn vdefn = new VarDefn(name, valu); RESULT = vdefn; :} /* gcla finish this later */ ; real ::= NUMB:n {: Debug.println(5, "Found real number"); Yytoken rtok = (Yytoken)n; Double rval = Double.valueOf(rtok.m_text); RESULT = rval; :} ; procdefn ::= STRN:s EQUALS proc:p {: //Symbol ss = (Symbol)s; //Symbol pp = (Symbol)p; //String name = (String)ss.value; //Process pr = (Process)pp.value; Yytoken toks = (Yytoken)s; String name = s.m_text; //String name = (String)s; Process pr = (Process)p; ProcessDefn pdefn = new ProcessDefn(name, pr); //RESULT = new Symbol(0, pdefn); RESULT = pdefn; :} /* gcla finish this later */ ; /* activity should use action directly, not STRN */ anaction ::= STRN:a {: /* Support for tau actions! */ Debug.println(5, "Found action"); Yytoken atok = (Yytoken)a; String aname = atok.m_text; Action act = new Action(Some(aname)); RESULT = act; :} ; activity ::= LPAREN STRN:a COMMA rate:r RPAREN {: Debug.println(5, "Found activity"); Yytoken atok = (Yytoken)a; String act = atok.m_text; //Yytoken rtok = (Yytoken)r; //Double rate = Double.valueOf(rtok.m_text); //Activity actv = new Activity(Some(act), Spec(rate)); Debug.println(5, "woohoo"); Activity actv = new Activity(Some(act), r); Debug.println(5, "constructed activity"); RESULT = actv; :} | LPAREN STRN:a COMMA dist:d RPAREN {: Debug.println(5, "Found activity with dist"); Yytoken atok = (Yytoken)a; String act = atok.m_text; Activity actv = new Activity(Some(act), d); Debug.println(5, "constructed activity with dist"); RESULT = actv; :} ; /* activity ::= LPAREN STRN:a COMMA rate:r RPAREN {: Debug.println(5, "Found activity"); Yytoken atok = (Yytoken)a; String act = atok.m_text; //Yytoken rtok = (Yytoken)r; //Double rate = Double.valueOf(rtok.m_text); //Activity actv = new Activity(Some(act), Spec(rate)); Activity actv = new Activity(Some(act), r); Debug.println(5, "constructed activity"); RESULT = actv; :} ; */ dist ::= EXP LPAREN rate:m RPAREN {: Debug.println(5, "Found exp"); // should check that rate is a num (specvalue) and // give appropriate error if not Sim_negexp_obj ne = new Sim_negexp_obj("RV("+m.specValue().toString()+")",1/(double)(m.specValue()),Dist.uniq); // hack! gcla later Dist.reseed(); //Dist.uniq = Dist.uniq + 1; //Rate r = new Spec(m); Dist d = new Dist(ne, Dist.EXPONENTIAL, m); RESULT = d; :} | NORM LPAREN rate:m SEMI rate:v RPAREN {: Debug.println(5, "Found normal"); Sim_normal_obj no = new Sim_normal_obj("RV("+m.specValue().toString()+","+v.specValue().toString()+")",(double)(m.specValue()),(double)(v.specValue()),Dist.uniq); // hack! gcla later Dist.reseed(); //Dist.uniq = Dist.uniq + 1; //Rate r = new Spec(m); Dist d = new Dist(no, Dist.NORMAL, m); RESULT = d; :} | UNIF LPAREN rate:m SEMI rate:v RPAREN {: Debug.println(5, "Found uniform"); Double min = m.specValue(); Double max = v.specValue(); double avg = ((double)max - (double)min)/2; Sim_uniform_obj un = new Sim_uniform_obj("RV("+min.toString()+","+max.toString()+")",(double)min,(double)max,Dist.uniq); // hack! gcla later Dist.reseed(); //Dist.uniq = Dist.uniq + 1; //Rate r = new Spec(m); Dist d = new Dist(un, Dist.UNIFORM, Spec((Double)avg)); RESULT = d; :} ; rate ::= rate:r1 SUM rate:r2 {: Debug.println(5, "Found sum of rates"); Rate rres = r1.plus(r2); RESULT = rres; :} | rate:r1 MINUS rate:r2 {: Debug.println(5, "Found minus of rates"); Rate rres = r1.minus(r2); RESULT = rres; :} | rate:r1 MULT rate:r2 {: Debug.println(5, "Found mult of rates"); Rate rres = r1.times(r2); RESULT = rres; :} | rate:r1 HIDE rate:r2 {: Debug.println(5, "Found div of rates"); Rate rres = r1.div(r2); RESULT = rres; :} | LPAREN rate:r RPAREN {: Debug.println(5, "Found bracket rates"); RESULT = r; :} /* | NUMB:n {: Debug.println(5, "Found rate number"); Yytoken rtok = (Yytoken)n; Double rateval = Double.valueOf(rtok.m_text); Rate r = new Spec(rateval); RESULT = r; :} */ | real:rv {: Debug.println(5, "Found rate number"); Rate r = new Spec(rv); RESULT = r; :} | STRN:s {: Yytoken toks = (Yytoken)s; String name = s.m_text; Debug.println(5, "Found rate string"+name); Rate r = new Spec(Peparoni.def(name)); RESULT = r; :} | TOP {: Debug.println(5, "Found top rate"); Rate r = new Unspec((Double)1.0); RESULT = r; :} ; coopset ::= LCOOP actions:l RCOOP {: Debug.println(5, "Found coopset"); RESULT = l; :} | LCOOP RCOOP {: Debug.println(5, "Found empty coopset in <>"); ActionSet l = new ActionSet(); RESULT = l; :} | PAR {: Debug.println(5, "Found empty coopset"); ActionSet l = new ActionSet(); RESULT = l; :} ; actions ::= actionset:as {: Debug.println(5, "found action set in setorlist"); RESULT = as; :} | actionlist:al {: Debug.println(5, "found action list in setorlist"); RESULT = al; :} ; actionset ::= LSET actionlist:al RSET {: Debug.println(5, "found action set"); RESULT = al; :} ; actionlist ::= actionlist:as COMMA anaction:a {: Debug.println(5, "Found action list"); //Yytoken atok = (Yytoken)a; //String aname = atok.m_text; //Action act = new Action(aname); //as.put(act); as.put(a); RESULT = as; :} | anaction:a {: Debug.println(5, "Found first?/last action"); //Yytoken atok = (Yytoken)a; //String aname = atok.m_text; //Action act = new Action(aname); //Debug.println(5, "go to creating new set"); ActionSet as = new ActionSet(); // should call Set //as.put(act); as.put(a); Debug.println(5, "put element"); RESULT = as; :} ; proc ::= activity:a DOT proc:p {: Debug.println(5, "Found prefix"); //Symbol aa = (Symbol)a; //Symbol rr = (Symbol)r; //Symbol pp = (Symbol)p; //String act = (String)aa.value; //String rate = (String)rr.value; //Process pr = (Process)pp.value; //Yytoken atok = (Yytoken)a; //String act = atok.m_text; //Yytoken rtok = (Yytoken)r; //Double rate = Double.valueOf(rtok.m_text); //Double rate = Double.valueOf("1.4"); //Double rate = (Double)1.4; //String act = (String)a; //Double rate = Double.valueOf(r); //Double rate = (Double)3.4; Process pr = (Process)p; Activity actv = (Activity)a; PepaProcess ppr = PepaProcess.Prefix(actv, (PepaProcess)pr); //RESULT = new Symbol(0, ppr); RESULT = ppr; :} | proc:p1 SUM proc:p2 {: Debug.println(5, "Found sum"); PepaProcess pr1 = (PepaProcess)p1; PepaProcess pr2 = (PepaProcess)p2; PepaProcess prsum = PepaProcess.Sum(pr1, pr2); RESULT = prsum; :} | proc:p1 coopset:l proc:p2 {: Debug.println(5, "Found coop"); PepaProcess pr1 = (PepaProcess)p1; PepaProcess pr2 = (PepaProcess)p2; CSet cs = l; PepaProcess prcoop = PepaProcess.Coop(pr1, cs, pr2); RESULT = prcoop; :} | proc:p HIDE actionset:l {: Debug.println(5, "Found hide"); PepaProcess pr = (PepaProcess)p; CSet cs = l; PepaProcess prhide = PepaProcess.Hide(pr, cs); RESULT = prhide; :} | LPAREN proc:p RPAREN {: Debug.println(5, "Found brackets"); RESULT = p; :} | STRN:p {: Debug.println(5, "Found process name"); //Symbol sname = (Symbol)p; //String name = (String)sname.value; Debug.println(5, "got to here 1"); Yytoken tokp = (Yytoken)p; String name = p.m_text; //String name = (String)p; Debug.println(5, "got to here 2"); PepaProcess ppr = PepaProcess.Var(name); Debug.println(5, "got to here 3"); //RESULT = new Symbol(0, ppr); RESULT = ppr; :} ;