📄 decafcerror.c
字号:
/**************************************************************************** * File name: decafcError.c * * Description: error report routine * * Input: none * * Output: none * * Author: Luojian Chen * * Date: March 2, 1997 * ****************************************************************************/#include <stdio.h>#include <string.h>#include "decafcError.h"/* external global variables */extern int yyerrflag;/*extern int currentLineNumber;extern int currentColumnNumber;*//* global variables */char currentLine[MAX_LINE_BUFFER_LENGTH + 1]; /* stores the current line*/int errorNumber = 0;/* global function prototypes */void yyerror(char *);/* void ReportError(ErrorIndex); */void ReportError(ErrorIndex, String, int, int);/* void ReportError2(ErrorIndex, String, int, int); *//* table store error messages */static Error error[] = { {"Syntax error found", "See the next error message and suggestion or check the source file."}, {"Unrecognized character", "Delete it."}, {"Unrecognized word", "Delete illegal characters from the word."}, {"Unexpected end of file met", "Insert missing ; and/or ) and/or } and/or ]."}, {"No class name defined for this class declaration", "Insert an identifier as the class name."}, {"Statement before this class declaration not in any class declaration", "Delete those statements or put them in a class declaration"}, {"Errors found in the class declaration", "Check the class body for missing ; and/or ) and/or } and/or ]."}, {"Missing ; in the last variable declaration", "Insert a ;."}, {"Error found in the last variable declaration", "Insert an identifier and/or delete illegal characters."}, {"Unbalanced ['s and ]'s in the variable declaration", "Insert missing [ or ] or delete extra [ or ]."}, {"No array name defined for this array declaration", "Insert an identifier as the array name."}, {"Invalid statement(s) found before this statment", "Insert missing ; and/or keywords."}, {"Invalid Argument List", "Insert missing , and/or delete illegal characters."}, {"Invalid relation expression", "Delete extra realtion expressions."}, {"Redeclaration of variable", "Delete the redundant declaration or change the variable name."}, {"Redeclaration of parameter", "Delete the redundant declaration or change the parameter name."}, {"Incompatible types of operands for operator `%s'", "Use operands with compatible types."}, {"Redeclaration of class with name `%s'", "Change the class name."}, {"Redeclaration of method with name `%s'", "Change the method name."}, {"Redeclaration of method `%s'", "Only one class can have a method `main'."}, {"Method '%s' could not take parameters", "Delete the parameters."}, {"No method `main' defined", "There must be a class that has a method `main'."}, {"Type name `%s' not defined", "Define the type name before using it."}, {"Type name `%s' defined after this usage", "Define the type name before using it."}, {"Constructor `%s' has a different name from the class name", "Change the constructor name to the same as the class name."}, {"Method `%s' has the same name as the class name", "Change the method name to a different name from the class name."}, {"Constructor `%s' is explicitly called", "Do not call constructor explicitly."}, {"Method `%s' not defined in the class requested", "Define the method in the requested class."}, {"Conflicting return type declaration of method `%s' with usage before", "Declare and use the method consistently."}, {"Variable `%s' not defined in the class requested", "Define the variable in the requested class."}, {"Variable `%s' is not an instance of a class object", "Use an instance of a class object to access its member."}, {"Undefined array attribute `%s'", "Array has only one attribute `length'."}, {"Misused keyword `%s'", "Use `this' to access a variable or method defined in the current class or do comparison."}, {"Attempted use of l-value `%s'", "Do not use l-value `this'"}, {"Variable `%s' does not have the number of dimensions requested", "Access it by correct number of dimensions."}, {"Index is not of integer type", "Apply variable of integer type."}, {"Operands for operator `%s' is not of integer type", "Apply variable of integer type."}, {"Non-class type found in the new expression", "Apply class type or array type."}, {"Too many argument(s) for method `%s'", "Pass the same number of arguments as you define the method."}, {"Too few argument(s) for method `%s'", "Pass the same number of arguments as you define the method."}, {"Non-integer type expression found in the `%s' statement", "Use only integer type expressions."}, {"Conflicting return type `%s' found", "Use the same return type as in the method declaration."}, {"Conflicting argument type `%s' found", "Use the same argument type as in the method declaration."}, {"%s divisor found", "Use non-zeor divisor."}};/**************************************************************************** Function name: yyerror Description: yacc error report routine Procedure: if the error is recognized by yacc call ReportError Return value: none Input parameter: message error message Output parameter: none ****************************************************************************/void yyerror(char *message){ if (strcmp(message, "syntax error") == 0) { /* this error is recognized by yacc call ReportError to give more detailed error message */ /* ReportError(ERROR_OTHER); */ ReportError(ERROR_OTHER, NULL, 0, 0); }}/**************************************************************************** Function name: ReportError Description: detailed error report routine Procedure: 1. if this error has been recognized by yacc increament error number 2. display the appropriate portion of the current line 3. display the error message 4. display the suggestion 5. if too many errors found stop parsing Return value: none Input parameter: errorIndex error index name error symbol name lineNumber error line number columnNumber error column number Output parameter: none ****************************************************************************//*void ReportError(ErrorIndex errorIndex)*/void ReportError(ErrorIndex errorIndex, String name, int lineNumber, int columnNumber){ static lastLineNumber; static lastColumnNumber; char lineSegment[SCREEN_WIDTH + 1]; char buffer[MAX_LINE_BUFFER_LENGTH + 1]; if (name == NULL) { /* if ((lastLineNumber != currentLineNumber) || (lastColumnNumber != currentColumnNumber)) { */ if ((lastLineNumber != lineNumber) || (lastColumnNumber != columnNumber)) { /* increament error number if this is a new error */ errorNumber ++; } /* lastLineNumber = currentLineNumber; lastColumnNumber = currentColumnNumber; */ lastLineNumber = lineNumber; lastColumnNumber = columnNumber; /* display the appropriate portion of the current line and error message */ /* if (currentColumnNumber < SCREEN_WIDTH) { */ if (columnNumber < SCREEN_WIDTH) { /* the whole line can be displayed */ strncpy(lineSegment, currentLine, SCREEN_WIDTH); lineSegment[SCREEN_WIDTH] = EOS; fprintf(stderr, "Error #%d at line %d, column %d:\n%s", errorNumber, /* currentLineNumber, currentColumnNumber, */ lineNumber, columnNumber, lineSegment); fprintf(stderr, "%*s%s\n", /* currentColumnNumber, */ columnNumber, "^ ", error[errorIndex].message); } else { /* the line is too long be be display in one line */ strncpy(lineSegment, currentLine + /* currentColumnNumber / SCREEN_WIDTH * SCREEN_WIDTH, */ columnNumber / SCREEN_WIDTH * SCREEN_WIDTH, SCREEN_WIDTH); lineSegment[SCREEN_WIDTH] = EOS; fprintf(stderr, "Error #%d at line %d, column %d:\n%s", errorNumber, /* currentLineNumber, currentColumnNumber, */ lineNumber, columnNumber, lineSegment); fprintf(stderr, "%*s%s\n", /* currentColumnNumber % SCREEN_WIDTH + 1, */ columnNumber % SCREEN_WIDTH + 1, "^ ", error[errorIndex].message); } } else { errorNumber ++; sprintf(buffer, error[errorIndex].message, name); fprintf(stderr, "Error #%d at line %d, column %d:\n%s\n", errorNumber, lineNumber, columnNumber, buffer); } /* display correction suggestion */ fprintf(stderr, "%s%s\n\n", "Correction suggestion: ", error[errorIndex].suggestion); /* stop parsing if too many erros found */ if (errorNumber >= MAX_ALLOWED_ERROR_NUMBER) { fprintf(stderr, "Too many error found. Stop.\n"); exit(0); }}/*void ReportError2(ErrorIndex errorIndex, String name, int lineNumber, int columnNumber){ char buffer[MAX_LINE_BUFFER_LENGTH + 1]; errorNumber ++; sprintf(buffer, error[errorIndex].message, name); fprintf(stderr, "Error #%d at line %d, column %d: %s\n", errorNumber, lineNumber, columnNumber, buffer); fprintf(stderr, "%s%s\n\n", "Correction suggestion: ", error[errorIndex].suggestion);}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -