I am currently working on creating a small compiler using C++. I have defined the following objects:
struct ValueNode
{
std::string name;
int value;
};
struct StatementNode
{
StatementType type;
union
{
struct AssignmentStatement * assign_stmt;
struct PrintStatement * print_stmt;
struct IfStatement * if_stmt;
struct GotoStatement * goto_stmt;
};
struct StatementNode * next; // next statement in the list or NULL
};
I have defined a series of functions relating to different types of statements in the language. One of these functions is called parse_assignment_stmt(). The segmentation fault I am experiencing is happening in this function, immediately after attempting to assign a value to recently-allocated memory. Here is that function:
struct StatementNode* parse_assign_stmt() {
//Object to be returned. Holds an object representing a statement
//made within the input program.
struct StatementNode* st = (struct StatementNode*)malloc(sizeof(struct StatementNode));
st->type = ASSIGN_STMT;
//First token should be an ID. Represents memory location we are assigning to.
Token tok = lexer->GetToken();
if(tok.token_type == ID) {
//Second token in an assignment should be an equal sign
Token tok2 = lexer->GetToken();
if (tok2.token_type == EQUAL) {
//This function reads the next token, makes sure it is of type NUM or ID, then creates and returns a ValueNode containing the relevant value.
struct ValueNode* rhs1 = parse_primary();
Token tok3 = lexer->GetToken();
//Assignment format for this logical branch: "x = 5;"
if(tok3.token_type == SEMICOLON) {
//first type
//Allocate memory for objects needed to build StatementNode st
struct AssignmentStatement* assign_stmt = (struct AssignmentStatement*)malloc(sizeof(struct AssignmentStatement));
struct ValueNode* lhs = (struct ValueNode*)malloc( sizeof(struct ValueNode));
printf("Name: %s, Value: %d\n", lhs->name.c_str(), lhs->value);
//PROBLEM ARISES HERE***
//lhs->name = tok.lexeme;
//return the proper structure
return st;
}
else if(tok3.token_type == PLUS || tok3.token_type == MINUS || tok3.token_type == DIV || tok3.token_type == MULT) {
//second type
//TODO
}
else {
printf("Syntax error. Semicolon or operator expected after first primary on RHS of assignment.");
exit(1);
}
}
else {
//not of proper form
printf("Syntax error. EQUAL expected after LHS of assignment.");
exit(1);
}
}
else {
//Not of proper form. Syntax error
printf("Syntax error. ID expected at beginning of assignment.");
exit(1);
}
}
Essentially, I'm allocating memory for a new ValueNode to create the variable lhs. I am printing out the name and value fields immediately to ensure that there isn't anything present. In my compiler output (I'm using g++, by the way), it's telling me that the name is (null) and the value is 0, which is expected. As soon as I uncomment the line
lhs->name = tok.lexeme;
I get a segmentation fault. At this point, I have no idea what could be going wrong. I'm creating the variable, using malloc to allocate memory to the location, making sure that there isn't anything stored there, and then immediately trying to write a value. And it always gives me a segmentation fault.
Here is the input program (.txt file) that is being fed to the program through stdin.
i;
{
i = 42 ;
print i;
}
I have tried using calloc() instead, since that should make sure that the memory is cleared before returning the pointer, but that didn't change anything. Any suggestions would be wonderful. Thank you!