header{ import java.util.*; import antlr.ASTFactory; } class ResNom extends TreeParser; options{ buildAST = true; importVocab=Analex; importVocab=Anasint; } { Tabla_Global t; void resolver_identificadores_tipo(){ Identificador_Tipo elemento; String nombre; LinkedList i; i = t.identtiposinresolver.getIdentificadores(); ListIterator li = i.listIterator(); while(li.hasNext()){ elemento = (Identificador_Tipo) li.next(); nombre = elemento.getNombre(); if(t.ambitomodulo.getDeclaracion(nombre) == null){ //identificador de tipo no declarado System.out.println("ERROR RN: identificador " + nombre + " no declarado"); } } } AST resolver_acceso_simple(String numero){ int no = Integer.parseInt(numero); //nombre y ambito contenedor del acceso Acceso acc = t.accesossinresolver.getAcceso(no); String nom = acc.getNombre(); Ambito amb = acc.getContenedor(); Simbolo sim; AST dec = #(#[ERROR, "error"]); //comprobar si se ha declarado, buscando en la tabla de ambitos y declaraciones if((sim = t.tablaambitos.buscar_simbolo(amb, nom)) == null){ //acceso simple no declarado System.out.println("ERROR RN: acceso simple " + nom + " no declarado"); } else { dec = sim.getDeclaracion(); //referencia adelantada } return dec; } AST declaracion_tipo(AST ident){ AST tipo = #(#[ERROR, "error"]); Ambito modulo = t.ambitomodulo; Simbolo s = modulo.getDeclaracion(ident.getText()); if (s != null){ ASTFactory f = new ASTFactory(); tipo = f.dupTree(s.getDeclaracion()); } return tipo; } } declaracion_modulo [Tabla_Global tab]: {t = tab; resolver_identificadores_tipo();} #(MODULO nombre_modulo definicion_modulo); // =============================== del fichero anasem.g ============================ nombre_modulo : IDENT ; definicion_modulo: (importacion)? (exportacion)+ (implementacion)? ; importacion : #(IMPORTACION lista_ident); lista_ident: (IDENT)+; exportacion : #(EXPORTACION lista_declaraciones_clases); implementacion : #(IMPLEMENTACION lista_declaraciones_clases); lista_declaraciones_clases: (declaracion_clase)* ; declaracion_clase : #(CLASE nombre_clase cualificador_clase definicion_clase) ; cualificador_clase : INST | NO_INST ; nombre_clase : IDENT ; definicion_clase : declaraciones_elementos_clase ; declaraciones_elementos_clase : (declaracion_elemento_clase)* ; declaracion_elemento_clase : #(METODO declaracion_metodo cualificador_elemento_clase) | #(ATRIBUTO IDENT tipo cualificador_elemento_clase) ; cualificador_elemento_clase: OCULTO | VISIBLE ; declaracion_metodo : prototipo_metodo definicion_metodo; prototipo_metodo: #(PROTOTIPO IDENT #(PARAMETROS declaracion_parametros) #(RESULTADO (tipo | VACIO))); declaracion_parametros : (declaracion_parametro)* ; declaracion_parametro : #(PARAMETRO IDENT tipo) ; definicion_metodo: #(DEFINICION #(VARIABLES_LOCALES declaracion_variables_locales) #(INSTRUCCIONES instrucciones)) ; declaracion_variables_locales : (declaracion_variable_local)* ; declaracion_variable_local : #(VARIABLE_LOCAL IDENT tipo) ; declaracion_error : ERROR; instrucciones : (instruccion)* ; instruccion : #(INSTRUCCION ( instruccion_simple | instruccion_compuesta)) ; instruccion_simple : llamada_metodo | crear | escribir | asignacion | retorno ; instruccion_compuesta : condicion | iteracion ; crear : #(CREAR expresion expresion); escribir : #(ESCRIBIR expresion); asignacion : #(ASIGNACION expresion expresion ); retorno: #(DEV expresion) ; llamada_metodo : #(LLAMADA acceso #(EXPRESIONES lista_expresiones)) ; lista_expresiones: (expresion)* ; condicion: #(SI expresion #(INSTRUCCIONES instrucciones) (#(INSTRUCCIONES instrucciones))?) ; iteracion : #(MIENTRAS expresion #(INSTRUCCIONES instrucciones)); expresion: #(O expresion expresion) | #(Y expresion expresion) | #(NO expresion) | #(op1:MAYOR expresion expresion) | #(op2:MAYOR_IGUAL expresion expresion) | #(op3:MENOR expresion expresion) | #(op4:MENOR_IGUAL expresion expresion) | #(op5:IGUAL expresion expresion) | #(op6:DISTINTO expresion expresion) | #(MAS expresion expresion) | #(MENOS expresion expresion) | #(MENOSUNARIO expresion) | #(POR expresion expresion) | #(DIVISION expresion expresion) | #(LLAMADA acceso #(EXPRESIONES lista_expresiones)) | #(ACCESO_TABLA acceso #(EXPRESIONES lista_expresiones_nv)) | acceso | LIT_ENTERO | LIT_REAL | LIT_CAR | CIERTO | FALSO | #(ENTERO_A_REAL expresion) | #(REAL_A_ENTERO expresion) | ERROR ; acceso : acceso_simple | #(ACCESO_OBJETO acceso_simple a:IDENT); acceso_simple : #(ACCESO_SIMPLE IDENT d:declaracion_acceso) ; declaracion_acceso: declaracion_modulo[t] | declaracion_clase | declaracion_elemento_clase | declaracion_parametro | declaracion_variable_local | declaracion_error | i:LIT_ENTERO {#declaracion_acceso = resolver_acceso_simple(i.getText());} ; lista_expresiones_nv : (expresion)+; tipo : tipo_predefinido_simple | tipo_predefinido_compuesto | declaracion_error | i:IDENT {#tipo = declaracion_tipo(#i);} ; tipo_predefinido_simple : ENTERO | REAL | LOGICO | CARACTER ; tipo_predefinido_compuesto : formacion; formacion : #(FORMACION #(LISTA_ENTEROS lista_enteros) (tipo_predefinido_simple | IDENT)); lista_enteros : (LIT_ENTERO)+ ;