Traductor e intérprete del lenguaje CL 1.23
|
00001 00026 import java.util.*; 00027 import org.antlr.runtime.*; 00028 00033 enum SemErrorCodes { 00035 identAlreadyDeclared, 00037 identIsNotDeclared, 00039 incompatibleAssignment, 00041 nonReferenceableLeftAssigment, 00043 incompatibleOperation, 00045 fieldAlreadyDefined, 00047 fieldIsNotDefined, 00049 booleanExpressionRequired, 00051 readWriteRequireBasic, 00053 referenciableExpressionRequired, 00055 integerIndexRequired, 00057 identIsNotProcedure, 00059 identIsNotFunction, 00061 incompatibleParameter, 00063 nonReferenceableParameter, 00065 incorrectNumberOfParameters, 00067 incompatibleReturnExpression 00068 }; 00069 00070 00085 final class SemanticError implements Comparable<SemanticError> { 00089 private int line; 00090 00094 private int charPositionInLine; 00095 00100 private SemErrorCodes errorCode; 00101 00105 private String text; 00106 00110 private int num; 00111 00112 // // Atributo adicional para describir y localizar mejor algunos errores 00113 // private CL_AST expr; 00114 00120 SemanticError(SemErrorCodes code, CL_AST ast) { 00121 errorCode = code; 00122 line = ast.getLine(); 00123 charPositionInLine = ast.getCharPositionInLine(); 00124 text = ast.getText(); 00125 } 00126 00133 SemanticError(SemErrorCodes code, CL_AST ast, int n) { 00134 errorCode = code; 00135 line = ast.getLine(); 00136 charPositionInLine = ast.getCharPositionInLine(); 00137 text = ast.getText(); 00138 num = n; 00139 } 00140 00150 public int compareTo(SemanticError error) { 00151 if (line < error.line) return -1; 00152 if (error.line < line) return +1; 00153 if (charPositionInLine < error.charPositionInLine) return -1; 00154 if (error.charPositionInLine < charPositionInLine) return +1; 00155 if (errorCode.compareTo(error.errorCode) == -1) return -1; 00156 if (errorCode.compareTo(error.errorCode) == +1) return +1; 00157 return 0; 00158 } 00159 00167 boolean equals(SemanticError error) { 00168 return line == error.line && 00169 charPositionInLine == error.charPositionInLine && 00170 errorCode == error.errorCode; 00171 } 00172 00182 public String toString( ) { 00183 String s = "L. " + line + ":" + charPositionInLine + ": "; 00184 switch (errorCode) { 00185 case identAlreadyDeclared: 00186 s += "Identifier '" + text + "' already declared in this scope."; 00187 return s; 00188 case identIsNotDeclared: 00189 s += "Identifier '" + text + "' is undeclared."; 00190 return s; 00191 case incompatibleAssignment: 00192 s += "Assigment with incompatible types."; 00193 return s; 00194 case nonReferenceableLeftAssigment: 00195 s += "Left expression of assignment is not referenceable."; 00196 return s; 00197 case incompatibleOperation: 00198 s += "Operator '" + text + "' with incompatible types."; 00199 return s; 00200 case fieldAlreadyDefined: 00201 s += "Field '" + text + "' already defined in the struct."; 00202 return s; 00203 case fieldIsNotDefined: 00204 s += "Field '" + text + "' is not defined in the struct."; 00205 return s; 00206 case booleanExpressionRequired: 00207 s += "Instruction '" + text + "' requires a boolean condition."; 00208 return s; 00209 case readWriteRequireBasic: 00210 s += "Instruction '" + text + "' requires a basic type."; 00211 return s; 00212 case referenciableExpressionRequired: 00213 s += "Instruction '" + text + "' requires a referenceable expression."; 00214 return s; 00215 case integerIndexRequired: 00216 s += "Array index must be of type integer."; 00217 return s; 00218 case identIsNotProcedure: 00219 s += "Procedure call instruction where '" + text + "' is not a procedure."; 00220 return s; 00221 case identIsNotFunction: 00222 s += "Function call expression where '" + text + "' is not a function."; 00223 return s; 00224 case incompatibleParameter: 00225 s += "Parameter #" + num + " with incompatible types."; 00226 return s; 00227 case nonReferenceableParameter: 00228 s += "Parameter #" + num + " is expected to be referenciable."; 00229 return s; 00230 case incorrectNumberOfParameters: 00231 s += "The number of parameters in the call to '" + text + "' does not match."; 00232 return s; 00233 case incompatibleReturnExpression: 00234 s += "Function return with incompatible type."; 00235 return s; 00236 default: 00237 s += "Unknown Semantic Error."; 00238 return s; 00239 } 00240 } 00241 00242 } 00243 00244 00253 final class SemanticErrors { 00256 private List<SemanticError> errors; 00257 00261 SemanticErrors() { 00262 errors = new ArrayList<SemanticError>(); 00263 } 00264 00270 void add_IdentAlreadyDeclared(CL_AST ast) { 00271 errors.add(new SemanticError(SemErrorCodes.identAlreadyDeclared, ast)); 00272 } 00273 00279 void add_IdentIsNotDeclared(CL_AST ast) { 00280 errors.add(new SemanticError(SemErrorCodes.identIsNotDeclared, ast)); 00281 } 00282 00288 void add_IncompatibleAssignment(CL_AST ast) { 00289 errors.add(new SemanticError(SemErrorCodes.incompatibleAssignment, ast)); 00290 } 00291 00297 void add_NonReferenceableLeftAssigment(CL_AST ast) { 00298 errors.add(new SemanticError(SemErrorCodes.nonReferenceableLeftAssigment, ast)); 00299 } 00300 00305 void add_IncompatibleOperation(CL_AST ast) { 00306 errors.add(new SemanticError(SemErrorCodes.incompatibleOperation, ast)); 00307 } 00308 00313 void add_FieldAlreadyDefined(CL_AST ast) { 00314 errors.add(new SemanticError(SemErrorCodes.fieldAlreadyDefined, ast)); 00315 } 00316 00322 void add_FieldIsNotDefined(CL_AST ast) { 00323 errors.add(new SemanticError(SemErrorCodes.fieldIsNotDefined, ast)); 00324 } 00325 00331 void add_BooleanExpressionRequired(CL_AST ast) { 00332 errors.add(new SemanticError(SemErrorCodes.booleanExpressionRequired, ast)); 00333 } 00334 00340 void add_ReadWriteRequireBasic(CL_AST ast) { 00341 errors.add(new SemanticError(SemErrorCodes.readWriteRequireBasic, ast)); 00342 } 00343 00349 void add_ReferenciableExpressionRequired(CL_AST ast) { 00350 errors.add(new SemanticError(SemErrorCodes.referenciableExpressionRequired, ast)); 00351 } 00352 00357 void add_IntegerIndexRequired(CL_AST ast) { 00358 errors.add(new SemanticError(SemErrorCodes.integerIndexRequired, ast)); 00359 } 00360 00366 void add_IdentIsNotProcedure(CL_AST ast) { 00367 errors.add(new SemanticError(SemErrorCodes.identIsNotProcedure, ast)); 00368 } 00369 00375 void add_IdentIsNotFunction(CL_AST ast) { 00376 errors.add(new SemanticError(SemErrorCodes.identIsNotFunction, ast)); 00377 } 00378 00384 void add_IncompatibleParameter(CL_AST ast, int n) { 00385 errors.add(new SemanticError(SemErrorCodes.incompatibleParameter, ast, n)); 00386 } 00387 00393 void add_NonReferenceableParameter(CL_AST ast, int n) { 00394 errors.add(new SemanticError(SemErrorCodes.nonReferenceableParameter, ast, n)); 00395 } 00396 00404 void add_IncorrectNumberOfParameters(CL_AST ast) { 00405 errors.add(new SemanticError(SemErrorCodes.incorrectNumberOfParameters, ast)); 00406 } 00407 00413 void add_IncompatibleReturnExpression(CL_AST ast) { 00414 errors.add(new SemanticError(SemErrorCodes.incompatibleReturnExpression, ast)); 00415 } 00416 00420 int getNumberOfSemanticErrors() { 00421 return errors.size(); 00422 } 00423 00433 public String toString() { 00434 String s = ""; 00435 Collections.sort(errors); 00436 for (int i = 0; i < errors.size(); i++) { 00437 s += errors.get(i).toString() + "\n"; 00438 } 00439 return s; 00440 } 00441 00442 }