Advertisement
LinkStormer

Parser.cup

May 25th, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | Source Code | 0 0
  1. // Archivo: parser.cup
  2. parser code {:
  3. import java_cup.runtime.*;
  4. :}
  5.  
  6. /* Terminales */
  7. terminal
  8. // Añadir operadores
  9. SUMA, RESTA, MULTIPLICACION, DIVISION, MODULO, INCREMENTO, DECREMENTO, OPERADOR_COMPARACION
  10. // Python
  11. IF, SWITCH, ELSE, FOR, WHILE, DO, FOREACH, DEF, LAMBDA, IN, DOS_PUNTOS, IDENTIFICADOR,
  12. PARENTESIS_IZQ, PARENTESIS_DER, COMA, ASIGNACION, VALOR, CLASS, EXTENDS, IMPLEMENTS,
  13. TRY, CATCH, NEW, THROW, LLAVE_IZQ, LLAVE_DER, PUNTO_COMA, CORCHETE_IZQ, CORCHETE_DER,
  14. // SQL
  15. SELECT, FROM, WHERE, INSERT, INTO, CREATE, DATABASE, TABLE, SET, ADD, VALORES, VALUES,
  16. INDEX, DELETE, UPDATE, DROP, ALTER, TRIGGER, PRIMARY_KEY, TIPO_DATO_SQL;
  17.  
  18.  
  19. /* No terminales */
  20. non terminal
  21. programa_python, instruccion, instruccion_for, instruccion_while, instruccion_do, instruccion_foreach, arreglo, lista_valores, // <-- Añadido
  22. clase, funcion, bloque, consulta_sql, condicion, identificadores, objeto, miembros, opt_herencia,
  23. columnas, tipos, accion, parametros, expresion, manejo_excepciones;
  24.  
  25. /* Precedencias */
  26. precedence left PARENTESIS_IZQ, PARENTESIS_DER;
  27. precedence left MULTIPLICACION, DIVISION, MODULO;
  28. precedence left SUMA, RESTA;
  29.  
  30. /* Reglas */
  31. start with programa_python;
  32. programa_python ::= instruccion+;
  33.  
  34. instruccion ::=
  35. instruccion_for | instruccion_while | instruccion_do | instruccion_foreach
  36. | funcion | consulta_sql | clase | manejo_excepciones | arreglo | objeto;
  37.  
  38. // === Bucles ===
  39. instruccion_do ::=
  40. DO LLAVE_IZQ bloque LLAVE_DER WHILE PARENTESIS_IZQ condicion PARENTESIS_DER PUNTO_COMA
  41. {: System.out.println("[Sintaxis] Bucle DO-WHILE válido"); :};
  42.  
  43. instruccion_foreach ::=
  44. FOREACH PARENTESIS_IZQ IDENTIFICADOR IN IDENTIFICADOR PARENTESIS_DER LLAVE_IZQ bloque LLAVE_DER
  45. {: System.out.println("[Sintaxis] Bucle FOREACH válido"); :};
  46.  
  47. // === Clases y Excepciones ===
  48. clase ::=
  49. CLASS IDENTIFICADOR (EXTENDS|IMPLEMENTS IDENTIFICADOR)? LLAVE_IZQ miembros LLAVE_DER
  50. {: System.out.println("[Sintaxis] Clase definida"); :};
  51.  
  52. manejo_excepciones ::= TRY LLAVE_IZQ bloque LLAVE_DER CATCH PARENTESIS_IZQ IDENTIFICADOR PARENTESIS_DER LLAVE_IZQ bloque LLAVE_DER
  53. {: System.out.println("[Sintaxis] Manejo de excepciones válido"); :};
  54.  
  55. opt_herencia ::= EXTENDS IDENTIFICADOR | IMPLEMENTS IDENTIFICADOR | /* vacío */;
  56.  
  57. // === Estructuras Python ===
  58. arreglo ::=
  59. IDENTIFICADOR ASIGNACION CORCHETE_IZQ lista_valores CORCHETE_DER PUNTO_COMA
  60. {: System.out.println("[Sintaxis] Arreglo definido"); :};
  61.  
  62. lista_valores ::=
  63. VALOR
  64. | lista_valores COMA VALOR;
  65.  
  66. objeto ::=
  67. IDENTIFICADOR ASIGNACION IDENTIFICADOR PARENTESIS_IZQ lista_valores PARENTESIS_DER PUNTO_COMA
  68. {: System.out.println("[Sintaxis] Objeto instanciado"); :};
  69.  
  70. // === SQL Ampliado ===
  71. consulta_sql ::=
  72. SELECT identificadores FROM IDENTIFICADOR opt_where PUNTO_COMA
  73. {: System.out.println("[Sintaxis] SELECT válido"); :}
  74. | INSERT INTO IDENTIFICADOR PARENTESIS_IZQ identificadores PARENTESIS_DER VALORES PARENTESIS_IZQ lista_valores PARENTESIS_DER PUNTO_COMA
  75. {: System.out.println("[Sintaxis] INSERT reconocido"); :}
  76. | UPDATE IDENTIFICADOR SET identificadores WHERE condicion PUNTO_COMA
  77. {: System.out.println("[Sintaxis] UPDATE válido"); :}
  78. | CREATE DATABASE IDENTIFICADOR PUNTO_COMA
  79. {: System.out.println("[Sintaxis] CREATE DATABASE válido"); :}
  80. | CREATE TABLE IDENTIFICADOR PARENTESIS_IZQ columnas PARENTESIS_DER PUNTO_COMA
  81. {: System.out.println("[Sintaxis] CREATE TABLE válido"); :}
  82. | ALTER TABLE IDENTIFICADOR accion IDENTIFICADOR TIPO_DATO_SQL PUNTO_COMA
  83. {: System.out.println("[Sintaxis] ALTER TABLE válido"); :}
  84. | DROP TABLE IDENTIFICADOR PUNTO_COMA
  85. {: System.out.println("[Sintaxis] DROP TABLE válido"); :}
  86. ;
  87.  
  88. columnas ::=
  89. IDENTIFICADOR TIPO_DATO_SQL
  90. | columnas COMA IDENTIFICADOR TIPO_DATO_SQL;
  91.  
  92. accion ::=
  93. ADD
  94. | DROP;
  95.  
  96. // === Función con parámetros ===
  97. funcion ::= DEF IDENTIFICADOR PARENTESIS_IZQ (parametros)? PARENTESIS_DER DOS_PUNTOS bloque
  98. {: System.out.println("[Sintaxis] Función definida"); :};
  99.  
  100.  
  101. parametros ::=
  102. IDENTIFICADOR
  103. | parametros COMA IDENTIFICADOR;
  104.  
  105. instruccion_for ::=
  106. FOR IDENTIFICADOR IN IDENTIFICADOR DOS_PUNTOS bloque
  107. {: System.out.println("[Sintaxis] Bucle FOR válido"); :};
  108.  
  109. instruccion_while ::=
  110. WHILE IDENTIFICADOR DOS_PUNTOS bloque
  111. {: System.out.println("[Sintaxis] Bucle WHILE válido"); :};
  112.  
  113. bloque ::=
  114. LLAVE_IZQ instruccion+ LLAVE_DER
  115. {: System.out.println(" Bloque de código"); :};
  116.  
  117. /* ---------- SQL ---------- */
  118. opt_where ::=
  119. WHERE condicion
  120. | /* vacío */;
  121.  
  122. condicion ::=
  123. expresion OPERADOR_COMPARACION expresion
  124. {: System.out.println(" Condición: comparación"); :};
  125.  
  126. expresion ::=
  127. expresion SUMA expresion
  128. | expresion RESTA expresion
  129. | expresion MULTIPLICACION expresion
  130. | expresion DIVISION expresion
  131. | PARENTESIS_IZQ expresion PARENTESIS_DER
  132. | IDENTIFICADOR
  133. | VALOR; // <-- Punto y coma añadido
  134.  
  135. identificadores ::=
  136. IDENTIFICADOR
  137. | identificadores COMA IDENTIFICADOR;
Tags: Java jflex jcup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement