YYSTYPE 構造体 node1 を定義します *
%token INT FLOAT CHAR DOUBLE VOID
start: 宣言関数 Declaration1 {$$ = mknode($1, $2, $3,NULL,0);}
|Declaration Function {$$=mknode($1,$2,NULL,NULL,0);}
| Declaration {$$ =mknode($1,NULL,NULL,NULL,0); }
| Function {$$ =mknode($1,NULL,NULL,NULL,0); }
;
Declaration1 :Function {$$ =mknode($1,NULL,NULL,NULL,NULL); }
;
Function:
Type ID '(' ArgListOpt ')' CompoundStmt {$$ = mknode($1,$2,$4,$6,NULL); }
;
タイプ: INT {$$ = mknode(NULL,NULL,NULL,NULL,"INT"); }
| | FLOAT {$$ = mknode(NULL,NULL,NULL,NULL,"FLOAT"); }
| | CHAR {$$ = mknode(NULL,NULL,NULL,NULL,"CHAR"); }
| | DOUBLE {$$ = mknode(NULL,NULL,NULL,NULL,"DOUBLE"); }
| | VOID {$$ = mknode(NULL,NULL,NULL,NULL,"VOID"); }
;
CompoundStmt: '{' StmtList '}' {$$ =$2; }
;
StmtList: StmtList Stmt {$$ = mknode($1,$2,NULL,NULL,NULL); }
| {$$=mknode(NULL,NULL,NULL,NULL,NULL);}
;
when i am running this on the input
int mian()
{}
it is giving segementation fault at
char* newnode =(char*)malloc(strlen(token));
node1 *mknode(node1 *left1, node1 *left2, node1* left3,node1* left4,char *token)
{
/* malloc the node */
node1 *newnode = (node1 *)malloc(sizeof(node1));
char *newstr = (char *)malloc(strlen(token));
strcpy(newstr, token);
newnode->left1 = left1;
newnode->left2 = left2;
newnode->left3 = left3;
newnode->left4 = left4;
newnode->token = newstr;
return(newnode);
}
ここでのエラーは何ですか?助けてください