Advertisement
Guest User

Untitled

a guest
May 25th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. import java_cup.runtime.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. parser code {:
  6.  
  7. Yylex analizadorLexico;
  8.  
  9. public static void main(String[] argv){
  10. if (argv.length == 0) {
  11. System.out.println("Uso : java Parser <inputfile)>");
  12. }
  13. else{
  14. try {
  15. java.io.FileInputStream stream = new java.io.FileInputStream(argv[0]);
  16. java.io.Reader reader = new java.io.InputStreamReader(stream);
  17. Yylex analizadorLexico= new Yylex(reader);
  18. Parser parserObj = new Parser();
  19. parserObj.analizadorLexico= analizadorLexico;
  20. parserObj.setScanner(parserObj.analizadorLexico);
  21. parserObj.parse();
  22. }
  23. catch(Exception x) {
  24. System.out.println("Error fatal.");
  25. }
  26. }
  27. }
  28.  
  29. public void error_sintactico(Symbol current_token){
  30. report_error("Error de sintaxis con token: "+sym.terminalNames[current_token.sym]+" en linea "+analizadorLexico.linea()+" columna "+analizadorLexico.columna(),current_token);
  31.  
  32. }
  33. :};
  34.  
  35.  
  36.  
  37. /*Terminales tokens que puede devolver el lexico*/
  38. terminal INTEGER,REAL,CHAR,BOOLEAN;
  39. terminal POINTER,TO,ARRAY,OF,PROCEDURE;
  40. terminal CARACTER;
  41. terminal ABREPAR,CIERRAPAR,ABRECOR,CIERRACOR,DOSPUNTOS,COMA,PTOYCOMA, IGUAL;
  42. terminal String ID;
  43. terminal Integer NUM;
  44. terminal Float NUMREAL;
  45.  
  46. non terminal Stack tipo, expr, declaracion;
  47. non terminal String listaargumentos;
  48. non terminal programa, valor, funcion;
  49.  
  50. /*Gramatica*/
  51.  
  52. programa ::= /* Epsilon*/
  53. | programa declaracion:d PTOYCOMA
  54. {: System.out.println(Simbolo.tipotoString(d)); :}
  55. | programa error PTOYCOMA
  56. {: this.parser.error_sintactico(this.parser.cur_token);:}
  57. ;
  58.  
  59. declaracion ::= ID:id COMA declaracion:t
  60. {: RESULT=t;
  61. /* Se busca en tabla de simbolos y se inserta o devuelve error*/
  62. System.out.println("ID:"+id+" en no final");
  63. :}
  64. | ID:id DOSPUNTOS tipo:t
  65. {: RESULT=t;
  66. /* Se busca en tabla de simbolos y se inserta o devuelve error*/
  67. System.out.println(id);
  68. :}
  69. | ID:id DOSPUNTOS tipo:t IGUAL valor:v
  70. {: RESULT=t;
  71. /* Se busca en tabla de simbolos y se inserta o devuelve error*/
  72. System.out.println(id);
  73. :}
  74. ;
  75. valor ::= NUM
  76. {: System.out.println("numeral");:}
  77. | NUMREAL
  78. {: System.out.println("real");:}
  79. | CARACTER
  80. {: System.out.println("char");:}
  81. ;
  82. tipo ::= INTEGER
  83. {: RESULT= new Stack(); RESULT.push(new Character('i'));:}
  84. | REAL
  85. {: RESULT= new Stack(); RESULT.push(new Character('r'));:}
  86. | CHAR
  87. {: RESULT= new Stack(); RESULT.push(new Character('c'));:}
  88. | BOOLEAN
  89. {: RESULT= new Stack(); RESULT.push(new Character('b'));:}
  90. | POINTER TO tipo:t
  91. {: RESULT= t; RESULT.push(new Character('p'));:}
  92. | PROCEDURE ABREPAR listaargumentos CIERRAPAR DOSPUNTOS tipo:t
  93. {: RESULT= t; RESULT.push(new Character('f'));:}
  94. | ARRAY ABRECOR NUM CIERRACOR OF tipo:t
  95. {: RESULT= t; RESULT.push(new Character('m'));:}
  96. ;
  97. listaargumentos::= {: RESULT=new String("Sin argumentos");
  98. System.out.println(RESULT); :}
  99.  
  100. ;
  101. funcion ::= ID:id DOSPUNTOS PROCEDURE ABREPAR CIERRAPAR DOSPUNTOS tipo:t
  102. {:
  103. System.out.println("Funcion:"+id+" del tipo "+t);
  104. :}
  105. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement