Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Sección de declaraciones package e imports*/
- import java_cup.runtime.*;
- /* Sección componentes de código de usuario */
- parser code {:
- public void report_error(String message, Object info) {
- StringBuffer m = new StringBuffer("Error");
- System.out.println("Mensaje: "+message);
- System.out.println("info: "+info.toString());
- if(info instanceof java_cup.runtime.Symbol) {
- java_cup.runtime.Symbol s=((java_cup.runtime.Symbol)info);
- /* Comprueba si el numero de línea es mayor o igual que cero */
- if(s.left >= 0) {
- m.append(" en linea "+(s.left+1));
- /*Comprueba si el numero de columna es mayoro igual que cero */
- if (s.right >= 0)
- m.append(", y columna "+(s.right+1));
- }
- }
- m.append(" : "+message);
- System.err.println(m);
- }
- public void report_fatal_error(String message, Object info) {
- report_error(message, info);
- System.exit(1);
- }
- int ban=1;
- :};
- /*Terminales*/
- terminal TVAR, TCONST, TALGORITMO, TINICIO, TFINALGORITMO, TLEE, TMUESTRE, TVERDADERO, TFALSO, TABANDONA, TDE, TREG;
- terminal TSINO, TSI, TFINSI, TENTONCES, TMIENTRAS, TFINMIENTRAS, THAZ, TPARA, TDESDE, THASTA, TFINPARA, TREPITA, TQUE;
- terminal TSUMA, TRESTA, TMULTIPLICACION, TDIVISION,
- TMENOR, TMENIGUAL, TMAYOR, TMAYIGUAL, TASIGNACION, TDIFERENTE, TIGUAL, TPUNTOYCOMA, TDOSPUNTOS, TBARRA, TCIRCUN, TMODULO, TY, TO, TNO, TCOMA, TLLAVEOP, TLLAVECLOS, TCOROP, TCORCLOS, TPARENOP, TPARENCLOS, TCOMENT;
- terminal TENTERO, TSIMPLE, TDOBLE, TCADENA, TCARACTER, TLOGICO;
- terminal TVECTOR, TMATRIZ, TTIPO, TREGISTRO, TFINREGISTRO;
- terminal Integer TNUM;
- terminal String TID;
- terminal String TCAD;
- /*no terminales*/
- non terminal AST programa, cuer_prog, secu_sent, sent_declaracion, sent_dvar, decl, sent_inicio, sent_prog, sent_escr, sent_lee, sent_cond, sent_cicli, sent_asig, mientras, para, repita, cadena, var, cond, numero;
- non terminal AST expresion_simple, expresion_aditiva, op_relacional, opsuma, termino, opmult, factor, sent_lista, opera, exp, rep;
- non terminal String tipo_dato;
- precedence left TSUMA,TRESTA;
- precedence left TDIVISION, TMULTIPLICACION;
- precedence left TSINO, TID;
- /* Produciones */
- programa ::= cuer_prog:e1 {:RESULT = e1.getBody(); Recorrer r=new Recorrer(); r.recor(RESULT);
- if(ban)System.out.println("programa");
- :};
- cuer_prog ::= cuer_prog:e1 TALGORITMO TID secu_sent:e2 TFINALGORITMO{: e1.nexta(e1,e2); RESULT = e1.getBody();
- if(ban)System.out.println("cuer_prog1"); :}
- | TALGORITMO TID secu_sent:e1 TFINALGORITMO {:RESULT = e1.getBody();
- if(ban)System.out.println("cuer_prog2");
- :};
- //////secu_sent ::= TVAR sent_declaracion_var:e1 TINICIO sec_prog {:RESULT = e1.getBody();
- secu_sent ::= TVAR sent_declaracion:e1 {:RESULT = e1.getBody();
- if(ban)System.out.println("secu_sent");
- :};
- //ojo
- sent_declaracion ::= sent_dvar:e1 TINICIO sent_inicio:e2 {: e1.nexta(e1,e2); RESULT = e1.getBody();
- if(ban)System.out.println("sent_declaracion");
- :};
- /*OJO*/
- sent_dvar ::= sent_dvar:e1 decl:e2 {: e1.nexta(e1,e2); RESULT = e1.getBody(); if(ban)System.out.println("sent_dvar1");:} |
- decl:e1 {:RESULT = e1.getBody();
- if(ban)System.out.println("sent_dvar2");
- :};
- decl ::= TID:e1 TDOSPUNTOS tipo_dato:tip {: RESULT = new BaseNode(String.valueOf(e1),tip);
- if(ban)System.out.println("decl");
- :}; /*| rep:e1 TDOSPUNTOS tipo_dato:tip {: RESULT = new BaseNode(String.valueOf(e1),tip); :};*/
- /*rep ::= var:e1 TCOMA var:e2 {: e1.nexta(e1,e2); RESULT = e1.getBody(); :}
- | rep:e1 TCOMA var:e2 {: e1.nexta(e1,e2); RESULT = e1.getBody(); :}*/
- tipo_dato ::= TENTERO {: RESULT="ENTERO";
- if(ban)System.out.println("tipo_dato");
- :};
- sent_inicio ::= sent_lista:e1 {:RESULT = e1.getBody();
- if(ban)System.out.println("sent_inicio");
- :};
- /////sent_inicio ::= sent_inicio:e1 sent_lista:e2 {:RESULT = e1.getBody(); :};
- /////sent_lista ::= sent_prog:e2 {: if(e1!=null){ e1.nexta(e1,e2); RESULT = e1.getBody();}else RESULT = e2.getBody(); :}
- /////| {};
- sent_lista ::= sent_lista:e1 sent_prog:e2 {: if(e1!=null){ e1.nexta(e1,e2); RESULT = e1.getBody();}else RESULT = e2.getBody();
- if(ban)System.out.println("sent_lista");
- :}
- | {};
- sent_prog ::= sent_escr:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_prog");:} |
- sent_lee:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_lee"); :} |
- sent_cond:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_cond"); :} |
- sent_cicli: e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_asig"); :} |
- sent_asig:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_asig"); :};
- /*OJO*/
- sent_escr ::= TMUESTRE cadena:e1 {:RESULT= new ExpUnaria("muestre",e1);
- if(ban)System.out.println("sent_escr"); :};
- sent_lee ::= TLEE var:e1 {:RESULT= new ExpUnaria("lee",e1); if(ban)System.out.println("sent_lee"); :} |
- TLEE var:e1 TCOMA var:e2 {:RESULT= new ExpBinaria("lee",e1,e2); if(ban)System.out.println("sent_lee2"); :} ;
- sent_cond ::= TSI cond:e1 TENTONCES sent_prog:e2 TFINSI {:RESULT= new ExpBinaria("if",e1,e2); if(ban)System.out.println("sent_cond"); :} |
- TSI cond:e1 TENTONCES sent_prog:e2 TSINO sent_prog:e3 {:RESULT= new ExpTriple("if",e1,e2,e3); if(ban)System.out.println("sent_cond2"); :};
- sent_cicli ::= mientras:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_mientras"); :} |
- para:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_para"); :}|
- repita:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_repita"); :};
- mientras ::= TMIENTRAS cond:e1 THAZ sent_prog:e2 TFINMIENTRAS {:RESULT= new ExpBinaria("mientras",e1,e2);
- if(ban)System.out.println("sent_repita");
- :} ;
- para ::= TPARA var:e1 TDESDE var:e2 THASTA var:e3 sent_prog TFINPARA {:RESULT= new ExpTriple("para",e1,e2,e3); :} |
- TPARA var:e1 TDESDE numero:e2 THASTA numero:e3 sent_prog TFINPARA {:RESULT= new ExpTriple("para",e1,e2,e3); :} ;
- repita ::= TREPITA sent_prog:e1 THASTA TQUE cond:e2 {:RESULT= new ExpBinaria("repita",e1,e2); :} ;
- //ojo
- sent_asig ::= var:e1 TASIGNACION var:e2 {:RESULT= new ExpBinaria("asignacion",e1,e2);
- if(ban)System.out.println("sent_asig1"); :} |
- var:e1 TASIGNACION numero:e2 {:RESULT= new ExpBinaria("asignacion",e1,e2); if(ban)System.out.println("sent_asig2"); :} |
- var:e1 TASIGNACION exp:e2 {:RESULT= new ExpBinaria("asignacion",e1,e2); if(ban)System.out.println("sent_asig3"); :};
- exp ::= var:e1 opera:e3 var:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("exp"); :} |
- numero:e1 opera:e3 numero:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("exp2"); :} |
- var:e1 opera:e3 numero:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("exp3"); :} |
- numero:e1 opera:e3 var:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("exp4"); :};
- opera ::= TSUMA {: RESULT = new BaseNode("+"); if(ban)System.out.println("opera+"); :}|
- TRESTA {: RESULT = new BaseNode("-");if(ban)System.out.println("opera-"); :} |
- TMULTIPLICACION {: RESULT = new BaseNode("*");if(ban)System.out.println("pera*"); :}|
- TDIVISION {: RESULT = new BaseNode("/");if(ban)System.out.println("opera/"); :};
- cond ::= var:e1 TIGUAL cond:e2 {:RESULT= new ExpBinaria("=",e1,e2); if(ban)System.out.println("cond1"); :} |
- expresion_simple:e1 {:RESULT= e1.getBody(); if(ban)System.out.println("cond2"); :};
- var ::= TID:e11 {: RESULT = new BaseNode(String.valueOf(e11)); if(ban)System.out.println("var"); :};
- expresion_simple ::= expresion_aditiva:e1 op_relacional:e3 expresion_aditiva:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); :} | expresion_aditiva:e1 {:RESULT = e1.getBody(); :};
- op_relacional ::= TMENIGUAL {: RESULT = new BaseNode("<="); if(ban)System.out.println("op_relacional<="); :} |
- TMENOR {: RESULT = new BaseNode("<"); if(ban)System.out.println("op_relacional<"); :} |
- TMAYOR {: RESULT = new BaseNode(">"); if(ban)System.out.println("op_relacional>"); :} |
- TMAYIGUAL {: RESULT = new BaseNode(">="); if(ban)System.out.println("op_relacional>="); :} |
- TDIFERENTE{: RESULT = new BaseNode("<>"); if(ban)System.out.println("op_relacional<>");:};
- expresion_aditiva ::= expresion_aditiva:e1 opsuma:e2 termino:e3 {: RESULT = new ExpBinaria((BaseNode) e2,e1,e3); if(ban)System.out.println("exp_aditiva"); :} |
- termino:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("exp_aditiva2");:};
- opsuma ::= TSUMA {: RESULT = new BaseNode("+"); if(ban)System.out.println("opsuma");:} |
- TRESTA {: RESULT = new BaseNode("-"); if(ban)System.out.println("opsuma2");:};
- termino ::= termino:e1 opmult:e3 factor:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("termino"); :} |
- factor:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("factor");:};
- opmult ::= TMULTIPLICACION {: RESULT = new BaseNode("*"); if(ban)System.out.println("opmult*"); :} |
- TDIVISION {: RESULT = new BaseNode("/"); if(ban)System.out.println("opmult/"); :};
- factor ::= TPARENOP cond:e1 TPARENCLOS {:RESULT = e1.getBody(); if(ban)System.out.println("factor");:} |
- var:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("factor2"); :} |
- TNUM:num {: RESULT = new BaseNode(String.valueOf(num)); if(ban)System.out.println("factor3");:} ;
- numero ::= TNUM:num {:
- RESULT = new BaseNode(String.valueOf(num));
- if(ban)System.out.println("numero"); :} ;
- cadena ::= TCAD:e12 {:
- RESULT = new BaseNode(String.valueOf(e12));
- if(ban)System.out.println("numero"); :};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement