fix parse and semantic v2.1

This commit is contained in:
2025-12-05 00:47:51 +09:00
parent 2b6193a2c7
commit d46fd36bcb
4 changed files with 51 additions and 20 deletions

View File

@@ -131,8 +131,8 @@ static void insertNode(TreeNode *t) {
case DeclK: case DeclK:
switch (t->kind.decl) { switch (t->kind.decl) {
case FuncK: { case FuncK: {
BucketList entry; BucketList entry = st_lookup(t->attr.name);
if (st_lookup(t->attr.name) != NULL) { if (entry != NULL) {
fprintf(listing, "Error: Symbol \"%s\" is redefined at line %d (already defined at line", t->attr.name, t->lineno); fprintf(listing, "Error: Symbol \"%s\" is redefined at line %d (already defined at line", t->attr.name, t->lineno);
LineList lines = entry->lines; LineList lines = entry->lines;
while (lines != NULL) { while (lines != NULL) {
@@ -140,7 +140,9 @@ static void insertNode(TreeNode *t) {
fprintf(listing, "%d", lines->lineno); fprintf(listing, "%d", lines->lineno);
lines = lines->next; lines = lines->next;
} }
st_entry_insert_line(entry, t->lineno);
fprintf(listing, ")\n"); fprintf(listing, ")\n");
Error = TRUE; Error = TRUE;
} else { } else {
func_entry = st_try_insert(t->attr.name, SymbolFunc, t->type, t->lineno); func_entry = st_try_insert(t->attr.name, SymbolFunc, t->type, t->lineno);

View File

@@ -18,7 +18,7 @@ static int savedNumber;
static int savedLineNo; /* ditto */ static int savedLineNo; /* ditto */
static TreeNode * savedTree; /* stores syntax tree for later return */ static TreeNode * savedTree; /* stores syntax tree for later return */
static int yylex(void); // added 11/2/11 to ensure no conflict with lex static int yylex(void); // added 11/2/11 to ensure no conflict with lex
int yyerror(char * message);
%} %}
%token IF ELSE WHILE RETURN INT VOID %token IF ELSE WHILE RETURN INT VOID
@@ -120,13 +120,16 @@ param_list : param_list COMMA param {
} | param {$$ = $1; }; } | param {$$ = $1; };
param : type_specifier name_specifier { param : type_specifier name_specifier {
$$ = newDeclNode(NonArrParamK ); $$ = newDeclNode(NonArrParamK);
$$->attr.name = savedName; $$->attr.name = savedName;
$$->type = $1->type; $$->type = $1->type;
} | type_specifier name_specifier LBRACE RBRACE { } | type_specifier name_specifier LBRACE RBRACE {
$$ = newDeclNode(ArrParamK); $$ = newDeclNode(ArrParamK);
$$->type = $1->type;
$$->attr.name = savedName; $$->attr.name = savedName;
if ($1->type == Integer)
$$->type = IntegerArray;
else
$$->type = Void;
}; };
compound_stmt : LCURLY local_declarations statement_list RCURLY { compound_stmt : LCURLY local_declarations statement_list RCURLY {

View File

@@ -39,7 +39,7 @@ FILE *code;
/* allocate and set tracing flags */ /* allocate and set tracing flags */
int EchoSource = FALSE; int EchoSource = FALSE;
int TraceScan = FALSE; int TraceScan = FALSE;
int TraceParse = FALSE; int TraceParse = TRUE;
int TraceAnalyze = TRUE; int TraceAnalyze = TRUE;
int TraceCode = FALSE; int TraceCode = FALSE;

View File

@@ -1,16 +1,42 @@
int main(void) int zero(void)
{ {
x(1, 2); return 0;
}
int first(int data[])
{
return data[0];
}
void scopedemo(void)
{
int outer;
outer = 1;
{
int outer;
outer = outer + 2;
}
}
void output(void)
{
return;
}
int input;
void main(void)
{
int arr[2];
arr[0] = 5;
scopedemo();
first(arr);
zero();
zero(1);
{
int blockOnly;
blockOnly = arr[0];
}
blockOnly;
return;
} }
/*
int main(void) {
if (x) {
int x;
x = 3;
} else {
int y;
y = 0;
x = 5;
}
output(x);
}*/