📄 grm.cup
字号:
start with program;program ::= exp:e {: /*CUP$Grm$actions.stampaTot();*/ parser.parseResult = e; // RIGA FONDAMENTALE! :};explist ::= exp:e {: RESULT = new Absyn.ExpList(e,null); :} | exp:e1 SEMICOLON explist:e2 {: RESULT = new Absyn.ExpList(e1,e2); :} // per una singola exp errata | error:e SEMICOLON {: System.out.println(" Lista espressioni corretta: exp ; exp ; ... ; exp "); stampaDebug("EXPLIST -> ERROR\n"); :} ;explist ::= ;exp ::= MINUS exp:e {: RESULT = new Absyn.OpExp(eleft, new Absyn.IntExp(eleft,0), Absyn.OpExp.MINUS, e); stampaDebug("Exp--> Uminus"); :} %prec UMINUS | letexp:e {: RESULT = e; stampaDebug("Exp--> Letexp"); :} | assignexp:e {: RESULT = e; stampaDebug("Exp--> assegnamento "); :} | opexp:e {: RESULT = e; stampaDebug("Exp--> operazione"); :} | forexp:e {: RESULT = e; stampaDebug("Exp--> for"); :} | ifexp:e {: RESULT = e; stampaDebug("Exp--> if exp"); :} | ifelseexp:e {: RESULT = e; stampaDebug("Exp--> if ..else ..exp"); :} | whileexp:e {: RESULT = e; stampaDebug("Exp--> while"); :} | NIL:n {: RESULT = new Absyn.NilExp(nleft); stampaDebug("Exp--> Nil");:} | intexp:e {: RESULT = new Absyn.IntExp(eleft,e.intValue()); stampaDebug("Exp--> int"); :} | STRING:s {: RESULT = new Absyn.StringExp(sleft,s); stampaDebug("Exp--> String : " + s); :} | BREAK:b {: RESULT = new Absyn.BreakExp(bleft); stampaDebug("Exp--> Break");:} | callexp:c {: RESULT = c; stampaDebug("Exp--> call "); :} | recordcreation:c {: RESULT = c; stampaDebug("Exp--> recordcreation"); :} | opexplog:l {: RESULT = l; stampaDebug("Exp--> oplog");:} | LPAREN expseq:e RPAREN {: RESULT =e; stampaDebug("Exp--> ( Exp )"); :} | lvalue:l {: RESULT = new Absyn.VarExp(lleft,l); stampaDebug("Exp--> lvalue"); :} ;expseq ::= explist:e1 {: RESULT = new Absyn.SeqExp(e1left,e1); :};intexp ::= INT:a {: RESULT = a; :};letexp ::= LET:l decs:d IN expseq:es END {: RESULT = new Absyn.LetExp(lleft,d,es); :};opexp ::= exp:e1 MINUS exp:e2 {: Integer i= new Integer(1); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :} | exp:e1 PLUS exp:e2 {: Integer i= new Integer(0); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :} | exp:e1 DIVIDE exp:e2 {: Integer i= new Integer(3); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :} | exp:e1 TIMES exp:e2 {: Integer i= new Integer(2); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :} | exp:e1 EQ exp:e2 {: Integer i= new Integer(4); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :} | exp:e1 LT exp:e2 {: Integer i= new Integer(6); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :} | exp:e1 LE exp:e2 {: Integer i= new Integer(7); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :} | exp:e1 GE exp:e2 {: Integer i= new Integer(9); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :} | exp:e1 GT exp:e2 {: Integer i= new Integer(8); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :} | exp:e1 NEQ exp:e2 {: Integer i= new Integer(5); RESULT = new Absyn.OpExp(e1left,e1,i.intValue(),e2); :};// Qualsiasi valore diverso da zero e' considerato TRUE ,mentre lo zero e' considerato FALSEopexplog ::= exp:e1 AND exp:e2 {: Absyn.IntExp a =new Absyn.IntExp(e1left,0); RESULT = new Absyn.IfExp(e1left,e1,e2,a); :} | exp:e1 OR exp:e2 {: Absyn.IntExp a =new Absyn.IntExp(e1left,1); RESULT = new Absyn.IfExp(e1left,e1, a ,e2); :};forexp ::= FOR:f ID:v1 ASSIGN exp:v2 TO exp:e1 DO exp:e2 {: RESULT = new Absyn.ForExp(fleft,new Absyn.VarDec(v1left,sym(v1),v2),e1,e2); :} | FOR:f error {: RESULT = new Absyn.ForExp(fleft,null,null,null); System.out.println(" Sintassi FOR corretta: FOR id := exp TO exp DO exp"); stampaDebug("FOR -> ERROR\n"); :} | FOR:f ID error {: RESULT = new Absyn.ForExp(fleft,null,null,null); System.out.println(" Sintassi FOR corretta: FOR id := exp TO exp DO exp"); stampaDebug("FOR ID -> ERROR\n"); :} | FOR:f ID ASSIGN error {: RESULT = new Absyn.ForExp(fleft,null,null,null); System.out.println(" Sintassi FOR corretta: FOR id := exp TO exp DO exp"); stampaDebug("FOR ID ASSIGN -> ERROR\n"); :} | FOR:f ID ASSIGN exp TO error {: RESULT = new Absyn.ForExp(fleft,null,null,null); System.out.println(" Sintassi FOR corretta: FOR id := exp TO exp DO exp"); stampaDebug("FOR ID ASSIGN EXP TO -> ERROR\n"); :} | FOR:f ID ASSIGN exp TO exp error {: RESULT = new Absyn.ForExp(fleft,null,null,null); System.out.println(" Sintassi FOR corretta: FOR id := exp TO exp DO exp"); stampaDebug("FOR ID ASSIGN EXP TO EXP -> ERROR\n"); :} | FOR:f ID ASSIGN exp TO exp DO error {: RESULT = new Absyn.ForExp(fleft,null,null,null); System.out.println(" Sintassi FOR corretta: FOR id := exp TO exp DO exp"); stampaDebug("FOR ID ASSIGN EXP TO EXP DO -> ERROR\n"); :};whileexp ::= WHILE:w exp:e1 DO exp:e2 {: RESULT = new Absyn.WhileExp(wleft,e1,e2); :} | WHILE:w error {: RESULT = new Absyn.WhileExp(wleft,null,null); System.out.println(" Sintassi WHILE corretta: WHILE exp DO exp"); stampaDebug("WHILE -> ERROR\n"); :} | WHILE:w exp error {: RESULT = new Absyn.WhileExp(wleft,null,null); System.out.println(" Sintassi WHILE corretta: WHILE exp DO exp"); stampaDebug("WHILE EXP -> ERROR\n"); :} | WHILE:w exp:e1 DO error {: RESULT = new Absyn.WhileExp(wleft,e1,null); System.out.println(" Sintassi WHILE corretta: WHILE exp DO exp"); stampaDebug("WHILE EXP DO -> ERROR\n"); :};ifexp ::= IF:i exp:e1 THEN exp:e2 {: RESULT = new Absyn.IfExp(ileft,e1,e2); :} | IF:i error {: RESULT = new Absyn.IfExp(ileft,null,null); System.out.println(" Sintassi IF-THEN corretta: IF exp THEN exp"); stampaDebug("IF -> ERROR\n"); :} | IF:i exp:e1 error {: RESULT = new Absyn.IfExp(ileft,e1,null); System.out.println(" Sintassi IF-THEN corretta: IF exp THEN exp"); stampaDebug("IF EXP -> ERROR\n"); :} | IF:i exp:e1 THEN error {: RESULT = new Absyn.IfExp(ileft,e1,null); System.out.println(" Sintassi IF-THEN corretta: IF exp THEN exp"); stampaDebug("IF EXP THEN -> ERROR\n"); :};ifelseexp::= IF:i exp:e1 THEN exp:e2 ELSE exp:e3 {: RESULT = new Absyn.IfExp(ileft,e1,e2,e3);:} | IF:i exp:e1 THEN exp:e2 ELSE error {: RESULT = new Absyn.IfExp(ileft,e1,e2,null); System.out.println(" Sintassi IF-THEN-ELSE corretta: IF exp THEN exp ELSE exp"); stampaDebug("IF EXP THEN EXP ELSE -> ERROR\n"); :};namety ::= ID:i {: RESULT = new Absyn.NameTy(ileft,sym(i)); stampaDebug("Type-id --> Id"); :};arrayty ::= ARRAY:a OF ID:i {: RESULT = new Absyn.ArrayTy(aleft,sym(i)); stampaDebug("Type-id --> Array"); :};recordty ::= LBRACE:b tyfields:f RBRACE {: RESULT = new Absyn.RecordTy(bleft,f); stampaDebug("Type-id --> Rec"); :};typeid ::= namety:n {: RESULT =n;:} | recordty:r {: RESULT =r;:} | arrayty:a {: RESULT =a;:};tyrest ::= COMMA ID:i COLON namety:a tyrest:f {: RESULT = new Absyn.FieldList(ileft,sym(i),a.name,f); :} | COMMA ID:i error {: RESULT = new Absyn.FieldList(ileft,sym(i),null,null); System.out.println(" Sintassi parametri dichiarazione funzione: (id:TypeId, id:TypeId , .... ,id:TypeId) "); stampaDebug("TYREST: ID -> ERROR\n"); :} | COMMA ID:i COLON error {: RESULT = new Absyn.FieldList(ileft,sym(i),null,null); System.out.println(" Sintassi parametri dichiarazione funzione: (id:TypeId, id:TypeId , .... ,id:TypeId) "); stampaDebug("TYREST: ID : -> ERROR\n"); :};tyrest ::= ;tyfields ::= ID:i COLON namety:a tyrest:f {: RESULT = new Absyn.FieldList(ileft,sym(i),a.name,f); :} | ID:i error {: RESULT = new Absyn.FieldList(ileft,sym(i),null,null); System.out.println(" Sintassi parametri dichiarazione funzione: (id:TypeId, id:TypeId , .... ,id:TypeId)"); stampaDebug("TYFIELDS: ID -> ERROR\n"); :} | ID:i COLON error {: RESULT = new Absyn.FieldList(ileft,sym(i),null,null); System.out.println(" Sintassi parametri dichiarazione funzione: (id:TypeId, id:TypeId , .... ,id:TypeId)"); stampaDebug("TYFIELDS: ID : -> ERROR\n"); :};tyfields ::= ;assignexp ::= lvalue:l ASSIGN exp:e {: RESULT = new Absyn.AssignExp(lleft,l,e); :};decs ::= dec:h decs:t {: RESULT = new Absyn.DecList(h,t); :};decs ::= ;dec ::= vardec:v {: RESULT = v; stampaDebug("Dec--> VarDec"); :} | tydec:t {: RESULT = t; stampaDebug("Dec--> TypeDec"); :} | fundec:f {: RESULT = f; stampaDebug("Dec--> FunctionDec"); :} | error {: System.out.println(" Puoi dichiarare solo variabili, funzioni o tipi"); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); System.out.println(" Sintassi dichiarazione variabile: VAR id [:TypeId] := exp"); System.out.println(" Sintassi dichiarazione tipo: TYPE id = TypeId\n"); stampaDebug("DEC: ERROR\n"); :};tydec ::= TYPE:t ID:i EQ typeid:a tydec:f {: RESULT = new Absyn.TypeDec(tleft,sym(i),a,f); stampaDebug("Dec --> TypeDec mutua"); :};tydec ::= TYPE:t ID:i EQ typeid:a {: RESULT = new Absyn.TypeDec(tleft,sym(i),a,null); :} | TYPE:t error {: RESULT = new Absyn.TypeDec(tleft,null,null,null); System.out.println(" Sintassi dichiarazione tipo: TYPE id = TypeId\n"); stampaDebug("TYPE -> ERROR\n"); :} | TYPE:t ID:i error {: RESULT = new Absyn.TypeDec(tleft,sym(i),null,null); System.out.println(" Sintassi dichiarazione tipo: TYPE id = TypeId\n"); stampaDebug("TYPE ID -> ERROR\n"); :} | TYPE:t ID:i EQ error {: RESULT = new Absyn.TypeDec(tleft,sym(i),null,null); System.out.println(" Sintassi dichiarazione tipo: TYPE id = TypeId\n"); stampaDebug("TYPE ID EQ -> ERROR\n"); :};vardec ::= VAR:p ID:i ASSIGN exp:e {: RESULT = new Absyn.VarDec(pleft,sym(i),e); :} | VAR:p ID:i COLON namety:t ASSIGN exp:e {: RESULT = new Absyn.VarDec(pleft,sym(i),t,e);:} | VAR:p ID:i ASSIGN arrayexp:e {: RESULT = new Absyn.VarDec(pleft,sym(i),e); :} | VAR:p ID:i COLON namety:t ASSIGN arrayexp:e {: RESULT = new Absyn.VarDec(pleft,sym(i),t,e);:};arrayexp ::= ID:t LBRACK:l exp:e1 RBRACK OF exp:e2 {: RESULT =new Absyn.ArrayExp(tleft,sym(t),e1,e2); :};lvalue ::= ID:i {: RESULT = new Absyn.SimpleVar(ileft,sym(i)); stampaDebug("Var--> SimpleVar"); :} | lvalue:i LBRACK error {: RESULT = new Absyn.SubscriptVar(ileft,i,null); System.out.println(" Sintassi corretta: id[exp]"); stampaDebug("LVALUE: LVALUE [ -> ERROR\n"); :} | lvalue:l DOT error {: RESULT = new Absyn.FieldVar(lleft,l,null); System.out.println(" Sintassi corretta: id.id "); stampaDebug("LVALUE: LVALUE . -> ERROR\n"); :} | lvalue:l DOT ID:i {: RESULT = new Absyn.FieldVar(lleft,l,sym(i)); stampaDebug("Var--> FiedlVar"); :} | lvalue:l LBRACK exp:e RBRACK {: RESULT = new Absyn.SubscriptVar(lleft,l,e); stampaDebug("Var--> SubscriptVar"); :};recordcreation ::= ID:i LBRACE recexp:l RBRACE {: RESULT = new Absyn.RecordExp(ileft,sym(i),l); :};recexp ::= ID:i EQ exp:e {: RESULT = new Absyn.FieldExpList(ileft,sym(i),e,null);:} | ID:i EQ arrayexp:e {: RESULT = new Absyn.FieldExpList(ileft,sym(i),e,null); :} | error:e {: RESULT = new Absyn.FieldExpList(eleft,null,null,null); System.out.println(" Sintassi creazione record: TypeId{ id=exp, id=exp, ... , id=exp } "); stampaDebug("RECEXP: ERROR\n");:} | ID:i error {: RESULT = new Absyn.FieldExpList(ileft,sym(i),null,null); System.out.println(" Sintassi creazione record: TypeId{ id=exp, id=exp, ... , id=exp } "); stampaDebug("RECEXP: ID -> ERROR\n"); :} | ID:i EQ error {: RESULT = new Absyn.FieldExpList(ileft,sym(i),null,null); System.out.println(" Sintassi creazione record: TypeId{ id=exp, id=exp, ... , id=exp } "); stampaDebug("RECEXP: ID EQ -> ERROR\n"); :} | ID:i EQ exp:e COMMA recexp:r {: RESULT = new Absyn.FieldExpList(ileft,sym(i),e,r); :} | ID:i EQ arrayexp:e COMMA recexp:r {: RESULT = new Absyn.FieldExpList(ileft,sym(i),e,r); :};recexp ::= ;fundec ::= FUNCTION:f ID:i LPAREN tyfields:t RPAREN EQ exp:e {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,null,e,null); stampaDebug("Fundec normale"); :} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN COLON namety:nt EQ exp:e {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,nt,e,null); stampaDebug("Fundec normale con tipo"); :} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN EQ exp:e fundec:m{: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,null,e,m); stampaDebug("Fundec Mutua");:} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN COLON namety:nt EQ exp:e fundec:m {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,nt,e,m); stampaDebug("Fundec Mutua con tipo"); :} | FUNCTION:f error {: RESULT = new Absyn.FunctionDec(fleft,null,null,null,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION -> ERROR\n"); :} | FUNCTION:f ID:i error {: RESULT = new Absyn.FunctionDec(fleft,sym(i),null,null,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION ID -> ERROR\n"); :} | FUNCTION:f ID:i LPAREN tyfields:t error {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,null,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION ID ( TYFIELDS | null -> ERROR\n"); :} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN error {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,null,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION ID ( TYFIELDS ) -> ERROR\n"); :} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN EQ error {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,null,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION ID ( TYFIELDS ) EQ -> ERROR\n"); :} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN COLON error {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,null,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION ID ( TYFIELDS ) : -> ERROR\n"); :} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN COLON namety:nt error {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,nt,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION ID ( TYFIELDS ) : NAMETY -> ERROR\n"); :} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN COLON namety:nt EQ error {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,nt,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION ID ( TYFIELDS ) : NAMETY EQ -> ERROR\n"); :} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN COLON namety:nt EQ LPAREN error {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,nt,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION ID ( TYFIELDS ) : NAMETY EQ ( -> ERROR\n"); :} | FUNCTION:f ID:i LPAREN tyfields:t RPAREN EQ LPAREN error {: RESULT = new Absyn.FunctionDec(fleft,sym(i),t,null,null,null); System.out.println(" Sintassi dichiarazione funzione: FUNCTION id ( [id:TypeId, id:TypeId, ..., id:TypeId] ) [:TypeId] = exp "); stampaDebug("FUNCTION ID ( TYFIELDS ) EQ ( -> ERROR\n"); :};callexp ::= ID:i LPAREN argseq:l RPAREN {: RESULT = new Absyn.CallExp(ileft,sym(i),l); :};argseq ::= exp:e {: RESULT = new Absyn.ExpList(e,null); :} | error {: RESULT = new Absyn.ExpList(null,null); System.out.println(" Sintassi chiamata funzione: id(exp, ..., exp)"); stampaDebug("ARGSEG: -> ERROR\n");:} | exp:e error {: RESULT = new Absyn.ExpList(e,null); System.out.println(" Sintassi chiamata funzione: id(exp, ..., exp)"); stampaDebug("ARGSEG: EXP -> ERROR\n");:} | exp:e1 COMMA argseq:e2 {: RESULT = new Absyn.ExpList(e1,e2); :};argseq ::=;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -