Appendice A : Definizione Parser JavaCup
import java_cup.runtime.*; import Astratta.*; import Astratta.Types.*; import Astratta.Tables.*; action code {:Table venv = new Table(); Table tenv = new Table();
Atomics TyReal = new REALTypes(); Atomics TyInt = new INTypes(); Atomics TyChar = new CHARTypes(); Atomics TySt = new STRINGTypes(); Atomics TyBool = new BOOLTypes();
Atomics VOID = new Astratta.Types.Void(); Atomics TOP = new Top();
:};
parser code {:
public boolean flag = false;
public void report_error(String message, Object info) { StringBuffer m = new StringBuffer("Error");
Flag = true;
if(info instanceof java_cup.runtime.Symbol) m.append( " ("+info.toString()+")" );
m.append(" : "+message);
AppletBase.Error(); System.out.println(m);
public void report_fatal_error(String message, Object info) { report_error(message, info);
throw new RuntimeException("Errore sintattico");
} :};
init with {: setScanner(getScanner()); :}
scan with {: return getScanner().next_token(); :}
terminal Float TKREAL; terminal String TKSTRING; terminal Integer TKINT; terminal Character TKCHAR; terminal Boolean TKBOOL;
terminal BINDER; terminal UPDATE;
terminal DOT , COMMA , SEMI; terminal LBRACK , RBRACK; terminal LPAREN , RPAREN; terminal EQ , EXT;
terminal IN; terminal LET; terminal IDE;
/*Estensione sistema dei tipi*/
terminal Type; terminal epyT;
/*Operatori per tipi*/
terminal ARROW; terminal COLON;
/*Atomics Type*/ terminal INT; terminal REAL; terminal CHAR; terminal STRING; terminal BOOL;
non terminal Astratta.Termine root; //Goal non terminal Astratta.Termine term; non terminal Astratta.TreeLet let; non terminal Astratta.TreeVar ide; non terminal Astratta.Costante con; non terminal Astratta.TreeInv inv; non terminal Astratta.Obj obj; non terminal Astratta.TreeUp upd;
non terminal Astratta.ObjBody objComp; non terminal Astratta.ObjBody objCompList; non terminal Astratta.Meth ro;
non terminal Astratta.TreeExtr extr; non terminal Astratta.TreeAbst abst; non terminal Astratta.Formali parList ; non terminal Astratta.Attuali actList; non terminal Astratta.Attuali metActList;
/*Tipi*/
non terminal Astratta.Types.types types; non terminal Astratta.Types.MTypes MTypes; non terminal Astratta.Types.OTypes OTypes; non terminal Astratta.Types.OTypesList OTList; non terminal Astratta.Types.MTypesList MTypesC; non terminal Astratta.Types.Sel sel;
/*Estensione tipi*/
non terminal Astratta.typeDecl typeDecl; non terminal Astratta.Identificatore TypeIDE;
precedence left LET;
precedence left EXT , DOT , UPDATE; precedence left BINDER;
start with root;
/*La grammatica vera e propria*/
root ::= term:t
{: BoxT ast = new BoxT(t,tenv,venv); BRead.set(ast); :}
|typeDecl:tD term:t
{: BoxTyD ast = new BoxTyD(tD,new BoxT(t,tenv,venv)); BRead.set(ast); :}
;
typeDecl ::= Type TypeIDE:i EQ types:t epyT {: Sym s = Sym.sym(i.toString()); if (tenv.get(s) == null) {
tenv.put(s,t);
RESULT = new typeDecl(i,t);} else {
BRead.IsDouble(i.toString()); RESULT = new typeDecl(i,t);} :}
|Type TypeIDE:i EQ types:t SEMI typeDecl:tD {: Sym s = Sym.sym(i.toString());
if (tenv.get(s) == null) { tenv.put(s,t);
RESULT = new typeDecl(i,t,tD);} else {
BRead.IsDouble(i.toString());
RESULT = new typeDecl(i,t,tD);} :} ;
term ::= ide:id {: RESULT = (Termine)id; :}
|con:c {: RESULT = (Termine)(new TreeCos((Costante)c)); :} |inv:i {: RESULT = (Termine)i; :}
|upd:u {: RESULT = (Termine)u; :} |let:l {: RESULT = (Termine)l; :} |obj:o {: RESULT = (Termine)o; :} |extr:e {: RESULT = (Termine)e; :} ;
ide ::= IDE:i {: Sym s = Sym.sym(i.toString()); venv.put(s,new String(i.toString())); String sup = (String)venv.get(s);
RESULT = new TreeVar(new Identificatore(sup)); :} ;
con ::= TKINT:e1 {: RESULT = (Costante)(new IntCos(e1.intValue())); :} |TKREAL:e2 {: RESULT = (Costante)(new FloatCos(e2.floatValue())); :} |TKCHAR:e3 {: RESULT = (Costante)(new CharCos(e3.charValue())); :} |TKSTRING:e4 {: RESULT = (Costante)(new StringCos(e4.toString())); :} |TKBOOL:e5 {: RESULT = (Costante)(new BoolCos(e5.booleanValue())); :} ;
obj ::= LBRACK objComp:ob1 objCompList:oby
{: ObjBody y = new ObjBody(ob1.getI(),ob1.getM(),oby); RESULT = (Obj)(new TreeObj(y)); :}
|LBRACK RBRACK
{: ObjBody z = new ObjBody();
RESULT = (Obj)(new TreeObj(z)); :} ;
objCompList ::= COMMA objComp:ob2 objCompList:obnext
{: RESULT = new ObjBody(ob2.getI(),ob2.getM(),obnext); :} |RBRACK
{: RESULT = null; :}
|RBRACK COLON TypeIDE:i
{: if(!BoxTyD.IsInTable(tenv,i.toString())) BRead.NotDeclared(i.toString()); else BRead.Declared(i.toString()); RESULT = null; :} ;
objComp ::= IDE:i EQ abst:a {: venv.beginScope(); tenv.beginScope();
Sym s = Sym.sym(i.toString()); venv.put(s,new String(i.toString())); String sup = (String)venv.get(s);
ObjBody o = new ObjBody(new Identificatore(sup),(Meth)a,null); venv.endScope();
tenv.endScope(); RESULT = o; :} ;
inv ::= term:t DOT ro:r actList:a
{: RESULT = new TreeInv((Attuali)a,(Termine)t,(Meth)r); :} ;
ro ::= IDE:i {: Sym s = Sym.sym(i.toString()); venv.put(s,new String(i.toString())); tenv.put(s,new VarTypes());
String sup = (String)venv.get(s);
RESULT = (Meth)(new TreeSel(new Identificatore(sup))); :} |extr:e
|abst:a {: venv.beginScope(); tenv.beginScope(); RESULT = (Meth)(a); venv.endScope(); tenv.endScope(); :} ;
extr ::= obj:o EXT obj:o2 DOT IDE:i actList:a
{: Sym s = Sym.sym(((TreeObj)o).Wyn().getName()); Sym typ = Sym.sym(((TreeObj)o2).Wyn().getName()); tenv.put(s,((TreeObj)o).Wyn());
tenv.put(typ,((TreeObj)o2).Wyn()); Sym id = Sym.sym(i.toString()); venv.put(id,new String(i.toString())); String sup = (String)venv.get(id);
RESULT = new TreeExtr(o,o2,new TreeSel(new Identificatore(sup)),(Attuali)a); :} ;
abst ::= BINDER LPAREN RPAREN term:t
{: RESULT = new TreeAbst(new Formali(),(Termine)t); :} |BINDER LPAREN IDE:i parList:p term:t
{:Sym s = Sym.sym(i.toString()); venv.put(s,new String(i.toString())); String sup = (String)venv.get(s); tenv.put(s,VOID);
TreeAbst a = new TreeAbst(new Formali(new Identificatore(sup),p),(Termine)t); RESULT = a; :}
|BINDER LPAREN IDE:i COLON types:typ parList:p term:t {:Sym s = Sym.sym(i.toString());
venv.put(s,new String(i.toString())); String sup = (String)venv.get(s); tenv.put(s,typ);
parList ::= COMMA IDE:i parList:p
{: Sym s = Sym.sym(i.toString()); venv.put(s,new String(i.toString())); String sup = (String)venv.get(s); tenv.put(s,VOID);
RESULT = new Formali(new Identificatore(sup),(Formali)p); :} |RPAREN {: RESULT = null; :}
|COMMA IDE:i COLON types:typ parList:p {: Sym s = Sym.sym(i.toString());
venv.put(s,new String(i.toString())); String sup = (String)venv.get(s); tenv.put(s,typ);
RESULT = new Formali(new Identificatore(sup),(Formali)p, (types) typ); :} ;
actList ::= LPAREN ro:r metActList:m
{: RESULT = new Attuali((Meth)r,(Attuali)m); :} |LPAREN RPAREN
{: RESULT = new Attuali(); :} ;
metActList ::= COMMA ro:r metActList:m
{: RESULT = new Attuali((Meth)r,(Attuali)m); :} |RPAREN
{: RESULT = null; :} ;
upd ::= term:t DOT IDE:i UPDATE LBRACK ro:r RBRACK {: Sym s = Sym.sym(i.toString());
venv.put(s,new String(i.toString())); String sup = (String)venv.get(s); tenv.put(s,VOID);
|term:t DOT IDE:i UPDATE LBRACK ro:r COLON types:typ RBRACK {: Sym s = Sym.sym(i.toString());
venv.put(s,new String(i.toString())); String sup = (String)venv.get(s); tenv.put(s,typ);
RESULT = new TreeUp((Termine)t, new Identificatore(sup), (Meth)r, (types)typ); :} ;
let ::= LET IDE:i EQ term:t1 IN term:t2 {: tenv.beginScope();
Sym s = Sym.sym(i.toString()); venv.put(s,new String(i.toString())); String sup = (String)venv.get(s); tenv.put(s,VOID);
TreeLet l = new TreeLet(new Identificatore(sup),(Termine)t1,(Termine)t2); tenv.endScope();
RESULT = l; :}
|LET IDE:i COLON types:typ EQ term:t1 IN term:t2 {: venv.beginScope();
tenv.beginScope();
Sym s = Sym.sym(i.toString()); venv.put(s,new String(i.toString())); String sup = (String)venv.get(s); tenv.put(s,typ);
TreeLet l = new TreeLet(new Identificatore(sup),(Termine)t1,(Termine)t2,(types)typ); tenv.endScope();
venv.endScope(); RESULT = l; :} ;
/*Definizioni grammaticali per i Tipi*/ types ::= OTypes:o {: RESULT = (types)o; :} |MTypes:m {: RESULT = (types)m; :} ; OTypes ::= IDE:i
{: RESULT = (OTypes)(new Name(new Identificator(i.toString()))); :} |LBRACK sel:s COLON MTypes:m OTList:l
{: RESULT = (OTypes)(new OType((new OTypesList(s,m,l)))); :} |Atom:c
{: RESULT = (OTypes)c; :} ;
Atom ::= STRING
{: Sym key = Sym.sym("STRING"); tenv.put(key,TySt);
RESULT = TySt; :} |INT
{: Sym key = Sym.sym("INT"); tenv.put(key,TyInt);
RESULT = TyInt; :} |REAL
{: Sym key = Sym.sym("REAL"); tenv.put(key,TyReal);
RESULT = TyReal; :} |CHAR
{: Sym key = Sym.sym("CHAR"); tenv.put(key,TyChar);
|BOOL
{: Sym key = Sym.sym("BOOL"); tenv.put(key,TyBool);
RESULT = TyBool; :};
sel ::= IDE:s
{: RESULT = new Sel(new Identificator(s.toString())); :};
OTList ::= COMMA sel:s COLON MTypes:m OTList:l {: RESULT = new OTypesList(s,m,l); :} |RBRACK
{: RESULT = null; :} ;
MTypes ::= OTypes:o MTypesC:ml OTypes:r {: RESULT = new MTypes(o,ml,r); :} ;
MTypesC ::= MTypes:typ MTypesC:m
{: RESULT = new MTypesList(typ,m); :} |ARROW
{: RESULT = null; :} ;