Advertisement
Guest User

Untitled

a guest
May 14th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BNF 9.49 KB | None | 0 0
  1. /*­­­­Sección de declaraciones package e imports­­­­­­­­­*/
  2. import java_cup.runtime.*;
  3. /*­­­­ Sección componentes de código de usuario ­­­­*/
  4. parser code {:
  5.     public void report_error(String message, Object info) {
  6.         StringBuffer m = new StringBuffer("Error");
  7.         System.out.println("Mensaje: "+message);
  8.         System.out.println("info: "+info.toString());
  9.         if(info instanceof java_cup.runtime.Symbol) {
  10.             java_cup.runtime.Symbol s=((java_cup.runtime.Symbol)info);
  11.             /* Comprueba si el numero de línea es mayor o igual que cero */
  12.             if(s.left >= 0) {
  13.                 m.append(" en linea "+(s.left+1));
  14.                 /*Comprueba si el numero de columna es mayoro igual que cero */
  15.                 if (s.right >= 0)
  16.                     m.append(", y columna "+(s.right+1));
  17.             }
  18.         }
  19.         m.append(" : "+message);
  20.         System.err.println(m);
  21.     }
  22.     public void report_fatal_error(String message, Object info) {
  23.         report_error(message, info);
  24.         System.exit(1);
  25.     }
  26.    
  27.     int ban=1;
  28. :};
  29.  
  30. /*Terminales*/
  31.  
  32.  
  33. terminal TVAR, TCONST, TALGORITMO, TINICIO, TFINALGORITMO, TLEE, TMUESTRE, TVERDADERO, TFALSO, TABANDONA, TDE, TREG;
  34.  
  35. terminal TSINO, TSI, TFINSI, TENTONCES, TMIENTRAS, TFINMIENTRAS, THAZ, TPARA, TDESDE, THASTA, TFINPARA, TREPITA, TQUE;
  36.  
  37. terminal TSUMA, TRESTA, TMULTIPLICACION, TDIVISION,
  38. TMENOR, TMENIGUAL, TMAYOR, TMAYIGUAL, TASIGNACION, TDIFERENTE, TIGUAL, TPUNTOYCOMA, TDOSPUNTOS, TBARRA, TCIRCUN, TMODULO, TY, TO, TNO, TCOMA, TLLAVEOP, TLLAVECLOS, TCOROP, TCORCLOS, TPARENOP, TPARENCLOS, TCOMENT;
  39.  
  40. terminal TENTERO, TSIMPLE, TDOBLE, TCADENA, TCARACTER, TLOGICO;
  41. terminal TVECTOR, TMATRIZ, TTIPO, TREGISTRO, TFINREGISTRO;
  42.  
  43. terminal Integer TNUM;
  44. terminal String TID;
  45. terminal String TCAD;
  46.  
  47. /*no terminales*/
  48.  
  49. 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;
  50. non terminal AST expresion_simple, expresion_aditiva, op_relacional, opsuma, termino, opmult, factor, sent_lista, opera, exp, rep;
  51. non terminal String tipo_dato;
  52.  
  53. precedence left TSUMA,TRESTA;
  54. precedence left TDIVISION, TMULTIPLICACION;
  55. precedence left TSINO, TID;
  56.  
  57.  
  58.  
  59. /* Produciones */
  60.  
  61. programa ::= cuer_prog:e1 {:RESULT = e1.getBody(); Recorrer r=new Recorrer(); r.recor(RESULT);
  62.     if(ban)System.out.println("programa");
  63.    
  64. :};
  65.  
  66. cuer_prog ::= cuer_prog:e1 TALGORITMO TID secu_sent:e2 TFINALGORITMO{: e1.nexta(e1,e2); RESULT = e1.getBody();
  67. if(ban)System.out.println("cuer_prog1"); :}
  68. | TALGORITMO TID secu_sent:e1 TFINALGORITMO {:RESULT = e1.getBody();
  69.     if(ban)System.out.println("cuer_prog2");
  70. :};
  71.  
  72.  
  73. //////secu_sent ::= TVAR sent_declaracion_var:e1 TINICIO sec_prog {:RESULT = e1.getBody();
  74.  
  75.  
  76.  
  77. secu_sent ::= TVAR sent_declaracion:e1 {:RESULT = e1.getBody();
  78. if(ban)System.out.println("secu_sent");
  79. :};
  80.  
  81. //ojo
  82. sent_declaracion ::= sent_dvar:e1 TINICIO sent_inicio:e2 {: e1.nexta(e1,e2); RESULT = e1.getBody();
  83. if(ban)System.out.println("sent_declaracion");
  84. :};
  85.  
  86.  
  87.  
  88. /*OJO*/
  89.  
  90. sent_dvar ::= sent_dvar:e1 decl:e2 {: e1.nexta(e1,e2); RESULT = e1.getBody(); if(ban)System.out.println("sent_dvar1");:} |
  91.   decl:e1 {:RESULT = e1.getBody();
  92.    if(ban)System.out.println("sent_dvar2");
  93.  :};
  94.  
  95. decl ::= TID:e1 TDOSPUNTOS tipo_dato:tip {: RESULT = new BaseNode(String.valueOf(e1),tip);
  96. if(ban)System.out.println("decl");
  97. :}; /*| rep:e1 TDOSPUNTOS tipo_dato:tip {: RESULT = new BaseNode(String.valueOf(e1),tip); :};*/
  98.  
  99.  
  100. /*rep ::= var:e1 TCOMA var:e2 {: e1.nexta(e1,e2); RESULT = e1.getBody(); :}
  101.  | rep:e1 TCOMA var:e2 {: e1.nexta(e1,e2); RESULT = e1.getBody(); :}*/
  102.  
  103.  
  104. tipo_dato ::= TENTERO {: RESULT="ENTERO";
  105. if(ban)System.out.println("tipo_dato");
  106.  :};
  107.  
  108. sent_inicio ::= sent_lista:e1 {:RESULT = e1.getBody();
  109. if(ban)System.out.println("sent_inicio");
  110.  :};
  111.  
  112. /////sent_inicio ::= sent_inicio:e1 sent_lista:e2       {:RESULT = e1.getBody(); :};
  113. /////sent_lista ::= sent_prog:e2 {: if(e1!=null){ e1.nexta(e1,e2); RESULT = e1.getBody();}else RESULT = e2.getBody(); :}
  114. /////| {};
  115.  
  116. sent_lista ::= sent_lista:e1 sent_prog:e2 {: if(e1!=null){ e1.nexta(e1,e2); RESULT = e1.getBody();}else RESULT = e2.getBody();
  117. if(ban)System.out.println("sent_lista");
  118.  
  119. :}
  120. | {};
  121.  
  122. sent_prog ::= sent_escr:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_prog");:} |
  123. sent_lee:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_lee"); :} |
  124. sent_cond:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_cond"); :} |
  125. sent_cicli: e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_asig"); :} |
  126. sent_asig:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_asig"); :};
  127.  
  128.  
  129. /*OJO*/
  130.  
  131. sent_escr ::= TMUESTRE cadena:e1 {:RESULT= new ExpUnaria("muestre",e1);
  132.     if(ban)System.out.println("sent_escr"); :};
  133.  
  134. sent_lee ::= TLEE var:e1 {:RESULT= new ExpUnaria("lee",e1); if(ban)System.out.println("sent_lee");  :} |
  135. TLEE var:e1 TCOMA var:e2 {:RESULT= new ExpBinaria("lee",e1,e2); if(ban)System.out.println("sent_lee2"); :} ;
  136.  
  137. sent_cond ::= TSI cond:e1 TENTONCES sent_prog:e2 TFINSI {:RESULT= new ExpBinaria("if",e1,e2); if(ban)System.out.println("sent_cond");  :} |
  138. 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");  :};
  139.  
  140. sent_cicli ::= mientras:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_mientras");  :} |
  141. para:e1 {:RESULT = e1.getBody();  if(ban)System.out.println("sent_para"); :}|
  142. repita:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("sent_repita"); :};
  143.  
  144. mientras ::= TMIENTRAS cond:e1 THAZ sent_prog:e2 TFINMIENTRAS {:RESULT= new ExpBinaria("mientras",e1,e2);
  145.   if(ban)System.out.println("sent_repita");
  146.  :} ;
  147.  
  148. para ::= TPARA var:e1 TDESDE var:e2 THASTA var:e3 sent_prog TFINPARA {:RESULT= new ExpTriple("para",e1,e2,e3); :} |
  149. TPARA var:e1 TDESDE numero:e2 THASTA numero:e3 sent_prog TFINPARA {:RESULT= new ExpTriple("para",e1,e2,e3); :}  ;
  150.  
  151. repita ::= TREPITA sent_prog:e1 THASTA TQUE cond:e2 {:RESULT= new ExpBinaria("repita",e1,e2); :} ;
  152.  
  153. //ojo
  154. sent_asig ::= var:e1 TASIGNACION var:e2 {:RESULT= new ExpBinaria("asignacion",e1,e2);
  155. if(ban)System.out.println("sent_asig1"); :} |
  156. var:e1 TASIGNACION numero:e2 {:RESULT= new ExpBinaria("asignacion",e1,e2); if(ban)System.out.println("sent_asig2");  :} |
  157. var:e1 TASIGNACION exp:e2 {:RESULT= new ExpBinaria("asignacion",e1,e2); if(ban)System.out.println("sent_asig3");  :};
  158.  
  159.  
  160. exp ::= var:e1 opera:e3 var:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("exp");  :} |
  161. numero:e1 opera:e3 numero:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("exp2"); :}   |
  162. var:e1 opera:e3 numero:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("exp3"); :} |
  163. numero:e1 opera:e3 var:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("exp4"); :};
  164.  
  165. opera ::= TSUMA {: RESULT = new BaseNode("+"); if(ban)System.out.println("opera+"); :}|
  166.   TRESTA {: RESULT = new BaseNode("-");if(ban)System.out.println("opera-");  :} |  
  167. TMULTIPLICACION {: RESULT = new BaseNode("*");if(ban)System.out.println("pera*");  :}|
  168.  TDIVISION {: RESULT = new BaseNode("/");if(ban)System.out.println("opera/");  :};
  169.  
  170. cond ::= var:e1 TIGUAL cond:e2 {:RESULT= new ExpBinaria("=",e1,e2); if(ban)System.out.println("cond1");  :} |
  171.  expresion_simple:e1 {:RESULT= e1.getBody(); if(ban)System.out.println("cond2"); :};
  172.  
  173. var ::= TID:e11 {: RESULT = new BaseNode(String.valueOf(e11)); if(ban)System.out.println("var"); :};
  174.  
  175. expresion_simple  ::= expresion_aditiva:e1  op_relacional:e3  expresion_aditiva:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); :} | expresion_aditiva:e1 {:RESULT = e1.getBody(); :};
  176.  
  177. op_relacional ::= TMENIGUAL {: RESULT = new BaseNode("<="); if(ban)System.out.println("op_relacional<="); :} |
  178.   TMENOR {: RESULT = new BaseNode("<"); if(ban)System.out.println("op_relacional<"); :} |
  179.     TMAYOR {: RESULT = new BaseNode(">"); if(ban)System.out.println("op_relacional>"); :} |  
  180.      TMAYIGUAL {: RESULT = new BaseNode(">="); if(ban)System.out.println("op_relacional>="); :} |  
  181.        TDIFERENTE{: RESULT = new BaseNode("<>"); if(ban)System.out.println("op_relacional<>");:};
  182.  
  183. expresion_aditiva ::= expresion_aditiva:e1  opsuma:e2 termino:e3 {: RESULT = new ExpBinaria((BaseNode) e2,e1,e3); if(ban)System.out.println("exp_aditiva"); :} |
  184.  termino:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("exp_aditiva2");:};
  185.  
  186. opsuma ::= TSUMA {: RESULT = new BaseNode("+"); if(ban)System.out.println("opsuma");:} |
  187.   TRESTA {: RESULT = new BaseNode("-"); if(ban)System.out.println("opsuma2");:};
  188.  
  189. termino ::= termino:e1   opmult:e3   factor:e2 {: RESULT = new ExpBinaria((BaseNode) e3,e1,e2); if(ban)System.out.println("termino"); :} |
  190.    factor:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("factor");:};
  191.  
  192. opmult ::= TMULTIPLICACION {: RESULT = new BaseNode("*"); if(ban)System.out.println("opmult*"); :} |
  193.   TDIVISION {: RESULT = new BaseNode("/"); if(ban)System.out.println("opmult/"); :};
  194.  
  195. factor ::= TPARENOP cond:e1 TPARENCLOS {:RESULT = e1.getBody(); if(ban)System.out.println("factor");:}  |  
  196.   var:e1 {:RESULT = e1.getBody(); if(ban)System.out.println("factor2"); :} |
  197.   TNUM:num {: RESULT = new BaseNode(String.valueOf(num)); if(ban)System.out.println("factor3");:} ;
  198.  
  199. numero ::= TNUM:num {:
  200.  RESULT = new BaseNode(String.valueOf(num));
  201.  if(ban)System.out.println("numero"); :} ;
  202.  
  203. cadena ::= TCAD:e12 {:
  204.  RESULT = new BaseNode(String.valueOf(e12));
  205.  if(ban)System.out.println("numero"); :};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement