TC & ASC Implemented. yyerrors updated.
This commit is contained in:
126
src/grammar.y
126
src/grammar.y
@ -106,6 +106,7 @@
|
||||
|
||||
program:
|
||||
prototype_or_definition_list
|
||||
| error { yyerrok; }
|
||||
;
|
||||
|
||||
|
||||
@ -115,12 +116,17 @@ prototype_or_definition_list:
|
||||
| definition prototype_or_definition_list
|
||||
| prototype
|
||||
| definition
|
||||
| error { yyerrok; }
|
||||
;
|
||||
|
||||
|
||||
|
||||
prototype:
|
||||
L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID;
|
||||
L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
|
||||
@ -259,7 +265,7 @@ definition:
|
||||
}
|
||||
}
|
||||
|
||||
;
|
||||
;
|
||||
|
||||
function_declaration:
|
||||
FUNCTION ID COLON ID
|
||||
@ -285,6 +291,7 @@ function_declaration:
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -344,6 +351,9 @@ idlist:
|
||||
printdebug("Type of entry is %s", getType(entry));
|
||||
printdebug("tag is %d", getAdInfoType(entry));
|
||||
}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -391,6 +401,9 @@ sblock:
|
||||
}
|
||||
R_BRACE
|
||||
{$$ = $5;}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -408,13 +421,20 @@ dblock:
|
||||
printdebug("Created a new scope when seeing a dblock");
|
||||
}
|
||||
}
|
||||
declaration_list R_BRACKET;
|
||||
declaration_list R_BRACKET
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
|
||||
declaration_list:
|
||||
declaration SEMI_COLON declaration_list
|
||||
| declaration
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -452,6 +472,9 @@ declaration:
|
||||
CreateEntry(cur,d,(TableNode*)$1,$3,getAdInfo((TableNode*)$1));
|
||||
}
|
||||
}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -468,6 +491,9 @@ id_or_types:
|
||||
printdebug("string of type is %s in types pattern of id_or_type rule.",getName((TableNode*)$1));
|
||||
$$ = (TableNode*)$1;
|
||||
}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -503,6 +529,7 @@ compound_statement statement_list {
|
||||
| simple_statement SEMI_COLON {
|
||||
$$ = $1;
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -523,9 +550,11 @@ WHILE L_PAREN expression R_PAREN sblock {
|
||||
$$ = undefined;
|
||||
}
|
||||
}
|
||||
| sblock {
|
||||
|
||||
| sblock {
|
||||
$$ = $1;
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -557,7 +586,10 @@ simple_statement:
|
||||
}
|
||||
|
||||
|
||||
| RETURN expression {$$ = getTypeEntry((TableNode*)$2);}
|
||||
| RETURN expression {$$ = getTypeEntry((TableNode*)$2);}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -565,6 +597,9 @@ simple_statement:
|
||||
rec_op:
|
||||
DOT
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
ablock:
|
||||
@ -573,6 +608,9 @@ ablock:
|
||||
$$ = $2;
|
||||
printdebug("ablock is %d", $$);
|
||||
}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -591,6 +629,9 @@ argument_list:
|
||||
CreateEntry(cur,getAdInfoType((TableNode*)$1),(TableNode*)$1, getName((TableNode*)$1), NULL);
|
||||
$$ = 1; printdebug("[ARGUMENT_LIST] argument list is %d", $$);
|
||||
}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -786,6 +827,8 @@ expression:
|
||||
$$=undefined;
|
||||
}
|
||||
}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
@ -969,6 +1012,8 @@ assignable:
|
||||
printdebug("[ASSIGNABLE - RULE 3] record = name: %s | field = %s", getName((TableNode*)($1)), getName((TableNode*)$3));
|
||||
}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -984,6 +1029,8 @@ memOp:
|
||||
printdebug("release expression");
|
||||
}
|
||||
|
||||
| error { yyerrok; }
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -1043,7 +1090,7 @@ constant:
|
||||
printdebug("string of C_FALSE in constant is false");
|
||||
$$ = node;
|
||||
}
|
||||
|
||||
|
||||
;
|
||||
|
||||
|
||||
@ -1071,8 +1118,7 @@ types:
|
||||
{
|
||||
$$ = $1;
|
||||
printdebug("string of T_BOOLEAN in types is %s",getName((TableNode*)$1));
|
||||
}
|
||||
|
||||
}
|
||||
;
|
||||
|
||||
%%
|
||||
@ -1104,24 +1150,50 @@ void throw_error(ErrorType error_type, const char *format, ...) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (asc_flag) {
|
||||
/* yyerror(""); */
|
||||
int needed = snprintf(NULL, 0, " LINE (%d:%d) ** %s ERROR: ", line, column, error_name);
|
||||
char *error_message = malloc(needed + 1);
|
||||
snprintf(error_message, needed + 1, " LINE (%d:%d) ** %s ERROR: ", line, column, error_name);
|
||||
|
||||
if (tc_flag) {
|
||||
yyerror("");
|
||||
if (asc_flag != NULL) {
|
||||
fprintf(asc_flag, "(%d:%d) ** %s ERROR: ", line, column, error_name);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(asc_flag, format, args);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
int needed2 = vsnprintf(NULL, 0, format, args_copy) + 1;
|
||||
va_end(args_copy);
|
||||
|
||||
char *error_message2 = malloc(needed2);
|
||||
if (error_message2 == NULL) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
va_end(args);
|
||||
fprintf(asc_flag, "\n");
|
||||
} else {
|
||||
fprintf(stderr, "%s(%d:%d) ** %s ERROR%s: %s", COLOR_RED, line, column, error_name, COLOR_WHITE, COLOR_YELLOW);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
va_end(args);
|
||||
fprintf(stderr, "%s\n", COLOR_WHITE);
|
||||
return;
|
||||
}
|
||||
|
||||
vsnprintf(error_message2, needed2, format, args);
|
||||
va_end(args);
|
||||
|
||||
int total_needed = needed + needed2 + 2;
|
||||
char *total_error_message = malloc(total_needed);
|
||||
if (total_error_message == NULL) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
free(error_message);
|
||||
free(error_message2);
|
||||
return;
|
||||
}
|
||||
|
||||
snprintf(total_error_message, total_needed, "%s%s\n\n", error_message, error_message2);
|
||||
if (tc_flag) {
|
||||
insert_code_line(total_error_message, line);
|
||||
} else {
|
||||
if (error_type != ERROR_TYPE) {
|
||||
insert_code_line(total_error_message, line);
|
||||
}
|
||||
}
|
||||
|
||||
free(error_message);
|
||||
free(error_message2);
|
||||
free(total_error_message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1132,11 +1204,13 @@ void yyerror(const char *err) {
|
||||
// Grammar Fallback Case
|
||||
if (strcmp(err, "syntax error") == 0) {
|
||||
if (asc_flag != NULL) {
|
||||
fprintf(asc_flag, "(%d:%d) ** SYNTAX ERROR: Incorrect syntax at token '%s'\n", line, column, yytext);
|
||||
int needed = snprintf(NULL, 0, " LINE (%d:%d) ** SYNTAX ERROR: Incorrect syntax at token '%s'\n\n", line, column, yytext);
|
||||
char *error_message = malloc(needed + 1);
|
||||
snprintf(error_message, needed + 1, " LINE (%d:%d) ** SYNTAX ERROR: Incorrect syntax at token '%s'\n\n", line, column, yytext);
|
||||
insert_code_line(error_message, line);
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "%s(%d:%d) ** SYNTAX ERROR%s: Incorrect syntax at token '%s%s%s'\n",
|
||||
COLOR_RED, line, column, COLOR_WHITE, COLOR_YELLOW, yytext, COLOR_WHITE);
|
||||
fprintf(stderr, " LINE (%d:%d) ** SYNTAX ERROR: Incorrect syntax at token %s\n\n", line, column, yytext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user