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:
switch (t->kind.decl) {
case FuncK: {
BucketList entry;
if (st_lookup(t->attr.name) != NULL) {
BucketList entry = st_lookup(t->attr.name);
if (entry != NULL) {
fprintf(listing, "Error: Symbol \"%s\" is redefined at line %d (already defined at line", t->attr.name, t->lineno);
LineList lines = entry->lines;
while (lines != NULL) {
@@ -140,7 +140,9 @@ static void insertNode(TreeNode *t) {
fprintf(listing, "%d", lines->lineno);
lines = lines->next;
}
st_entry_insert_line(entry, t->lineno);
fprintf(listing, ")\n");
Error = TRUE;
} else {
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 TreeNode * savedTree; /* stores syntax tree for later return */
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
@@ -120,13 +120,16 @@ param_list : param_list COMMA param {
} | param {$$ = $1; };
param : type_specifier name_specifier {
$$ = newDeclNode(NonArrParamK );
$$ = newDeclNode(NonArrParamK);
$$->attr.name = savedName;
$$->type = $1->type;
} | type_specifier name_specifier LBRACE RBRACE {
$$ = newDeclNode(ArrParamK);
$$->type = $1->type;
$$->attr.name = savedName;
if ($1->type == Integer)
$$->type = IntegerArray;
else
$$->type = Void;
};
compound_stmt : LCURLY local_declarations statement_list RCURLY {

View File

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

View File

@@ -1,16 +1,42 @@
int main(void)
int zero(void)
{
x(1, 2);
return 0;
}
/*
int main(void) {
if (x) {
int x;
x = 3;
} else {
int y;
y = 0;
x = 5;
int first(int data[])
{
return data[0];
}
void scopedemo(void)
{
int outer;
outer = 1;
{
int outer;
outer = outer + 2;
}
output(x);
}*/
}
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;
}