Advertisement
Guest User

grammar

a guest
Nov 12th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.76 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <conio.h>
  5. #include "y.tab.h"
  6. int yystopparser=0;
  7. FILE  *yyin;
  8.  
  9. %}
  10.  
  11. %token PROGRAM
  12. %token DEFINE
  13. %token ENDDEFINE
  14. %token CONST_STR
  15. %token REAL
  16. %token CONST_INT
  17. %token INTEGER
  18. %token STRING
  19. %token IF DO ELSE ENDIF
  20. %token WHILE ENDWHILE
  21. %token ENDPROGRAM
  22. %token CONST
  23. %token ENDCONST
  24. %token OPERADOR_NEGACION
  25. %token OPERADOR_LOGICO
  26. %token OPERADOR_ASIGNACION
  27. %token OPERADOR_MAYOR
  28. %token OPERADOR_MENOR
  29. %token OPERADOR_IGUAL
  30. %token OPERADOR_MENORIGUAL
  31. %token OPERADOR_MAYORIGUAL
  32. %token OPERADOR_DISTINTO
  33. %token OPERADOR_SUMA
  34. %token OPERADOR_RESTA
  35. %token OPERADOR_MULTIPLICACION
  36. %token OPERADOR_DIVISION
  37. %token SEPARADOR_DECIMALES
  38. %token SEPARADOR_IDENTIFICADORES
  39. %token SEPARADOR_SENTENCIA
  40. %token PARENTESIS_ABRE PARENTESIS_CIERRA
  41. %token APERTURA_COMENTARIO CIERRE_COMENTARIO
  42. %token DOSPUNTOS
  43. %token DISPLAY
  44. %token COMILLA
  45. %token ID
  46.  
  47. %%
  48.  
  49. START:
  50.         PROGRAM ID SEPARADOR_SENTENCIA zona_declaracion_constantes zona_declaracion_variables bloque ENDPROGRAM
  51.         ;
  52.  
  53.        
  54. zona_declaracion_variables:
  55.         DEFINE conjunto_definiciones ENDDEFINE
  56.         ;
  57.  
  58.  
  59. zona_declaracion_constantes:
  60.         CONST constantes ENDCONST
  61.         ;
  62.  
  63.        
  64. constantes:
  65.         constantes SEPARADOR_SENTENCIA const
  66.         | const SEPARADOR_SENTENCIA
  67.         ;
  68.  
  69.        
  70. const:
  71.         declaracion_const_reales
  72.         | declaracion_const_int
  73.         | declaracion_const_str
  74.         ;
  75.  
  76.        
  77. declaracion_const_str:
  78.            STRING ID OPERADOR_ASIGNACION literal_string
  79.         ;
  80.  
  81.  
  82. declaracion_const_reales:
  83.           REAL ID OPERADOR_ASIGNACION literal_real
  84.         ;
  85.  
  86.  
  87. declaracion_const_int:
  88.          INTEGER ID OPERADOR_ASIGNACION literal_entero
  89.         ;
  90.        
  91.        
  92. conjunto_definiciones:
  93.         conjunto_definiciones definiciones
  94.         | definiciones
  95.         ;
  96.        
  97.  
  98. seleccion:
  99.  
  100.          IF condicion DO bloque ELSE bloque ENDIF
  101.         ;
  102.  
  103.        
  104. condicion: 
  105.         comparacion OPERADOR_LOGICO comparacion
  106.         | OPERADOR_NEGACION comparacion
  107.         | comparacion
  108.         | comparacion OPERADOR_LOGICO OPERADOR_NEGACION comparacion
  109.         ;
  110.  
  111.        
  112. comparacion:
  113.         expresion OPERADOR_MAYOR expresion
  114.         | expresion OPERADOR_MENOR expresion
  115.         | expresion OPERADOR_MAYORIGUAL expresion
  116.         | expresion OPERADOR_MENORIGUAL expresion
  117.         | expresion OPERADOR_DISTINTO expresion
  118.         | expresion OPERADOR_IGUAL expresion
  119.         ;  
  120.  
  121.        
  122. expresion: 
  123.         expresion OPERADOR_SUMA termino
  124.         | expresion OPERADOR_RESTA termino
  125.         | termino
  126.         ;
  127.  
  128.        
  129. termino:
  130.         termino OPERADOR_MULTIPLICACION factor
  131.         | termino OPERADOR_DIVISION factor
  132.         | factor
  133.         ;
  134.  
  135.        
  136. factor:    
  137.         ID
  138.         | literal_entero
  139.         | literal_real
  140.         | PARENTESIS_ABRE expresion PARENTESIS_CIERRA
  141.         ;
  142.  
  143.  
  144. iteracion_condicional:
  145.             WHILE condicion DO bloque ENDWHILE 
  146.             ;
  147.  
  148.            
  149. bloque:
  150.         sentencia SEPARADOR_SENTENCIA
  151.         | bloque sentencia SEPARADOR_SENTENCIA
  152.         ;
  153.  
  154.        
  155. sentencia:
  156.         asignacion
  157.         | estructura_control
  158.         | salida
  159.         | comentario
  160.         ;
  161.        
  162.        
  163. comentario:
  164.         APERTURA_COMENTARIO literal_string CIERRE_COMENTARIO
  165.         ;
  166.  
  167.        
  168. estructura_control:
  169.             seleccion
  170.             | iteracion_condicional
  171.             ;
  172.  
  173.  
  174. asignacion:
  175.         ID OPERADOR_ASIGNACION concatenacion  
  176.         | ID OPERADOR_ASIGNACION expresion
  177.         | ID OPERADOR_ASIGNACION literal_string
  178.         ;
  179.  
  180.        
  181. concatenacion:
  182.         ID OPERADOR_SUMA ID
  183.         | ID OPERADOR_SUMA literal_string
  184.         | literal_string OPERADOR_SUMA ID
  185.         | literal_string OPERADOR_SUMA literal_string
  186.         ;
  187.        
  188.        
  189. literal_string:
  190.         COMILLA CONST_STR COMILLA
  191.         ;
  192.        
  193.        
  194. literal_real:
  195.          signo numero_int SEPARADOR_DECIMALES numero_int
  196.          | numero_int SEPARADOR_DECIMALES numero_int
  197.          | signo SEPARADOR_DECIMALES numero_int
  198.          | SEPARADOR_DECIMALES numero_int
  199.          | signo numero_int SEPARADOR_DECIMALES
  200.          | numero_int SEPARADOR_DECIMALES
  201.         ;
  202.  
  203.        
  204. literal_entero:
  205.         signo numero_int
  206.         | numero_int
  207.         ;
  208.        
  209.        
  210. numero_int:
  211.         CONST_INT
  212.         ;
  213.  
  214.        
  215. signo:
  216.         OPERADOR_RESTA 
  217.         ;
  218.        
  219.        
  220. definiciones:
  221.         definicion_string
  222.         | definicion_entero
  223.         | definicion_real
  224.         ;  
  225.        
  226.        
  227. definicion_string:
  228.         STRING DOSPUNTOS iteracion_id SEPARADOR_SENTENCIA
  229.         ;
  230.        
  231.        
  232. definicion_entero:
  233.         INTEGER DOSPUNTOS iteracion_id SEPARADOR_SENTENCIA
  234.         ;
  235.        
  236.        
  237. definicion_real:
  238.         REAL DOSPUNTOS iteracion_id SEPARADOR_SENTENCIA
  239.         ;
  240.        
  241.        
  242. iteracion_id:
  243.         ID
  244.         | ID SEPARADOR_IDENTIFICADORES iteracion_id
  245.         ;
  246.        
  247.        
  248. salida:
  249.         DISPLAY PARENTESIS_ABRE ID PARENTESIS_CIERRA
  250.         | DISPLAY PARENTESIS_ABRE literal_entero PARENTESIS_CIERRA
  251.         | DISPLAY PARENTESIS_ABRE literal_real  PARENTESIS_CIERRA
  252.         | DISPLAY PARENTESIS_ABRE literal_string PARENTESIS_CIERRA
  253.         ;  
  254.  
  255.  
  256.  
  257.  
  258. %%
  259. int main(int argc,char *argv[])
  260. {
  261.   if ((yyin = fopen(argv[1], "rt")) == NULL)
  262.   {
  263.     printf("\nNo se puede abrir el archivo: %s\n", argv[1]);
  264.   }
  265.   else
  266.   {
  267.     yyparse();
  268.   }
  269.   fclose(yyin);
  270.   return 0;
  271. }
  272. int yyerror(void)
  273.      {
  274.        printf("Syntax Error\n");
  275.      system ("Pause");
  276.      exit (1);
  277.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement