#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0

    Struct pointer reference error


    Hello all. So I am writing a compiler for a simple language in C. My I have created a parser for lang which creates a graph of pointers. It compiles the code by traversing the "graph" of pointers. here is the code below

    compiler.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    #include <ctype.h>
    #include <string.h>
    #include "compiler.h"
    
    #define DEBUG 1	 // 1 => Turn ON debugging, 0 => Turn OFF debugging
    int g;
    void print_debug(const char * format, ...)
    {
    	va_list args;
    	if (DEBUG)
    	{
    		va_start (args, format);
    		vfprintf (stdout, format, args);
    		va_end (args);
    	}
    }
    
    //---------------------------------------------------------
    // Lexer
    
    char token[MAX_TOKEN_LENGTH];	  // token string
    int  ttype;						// token type
    int  activeToken = FALSE;
    int  tokenLength;
    int  line_no = 1;
    
    char *reserved[] = 
    	 {	"",
    		"VAR",
    		"IF",
    		"WHILE",
    		"REPEAT",
    		"UNTIL",
    		"print",
    		"ARRAY",
    		"+",
    		"-",
    		"/",
    		"*",
    		"=",
    		":",
    		",",
    		";",
    		"[",
    		"]",
    		"(",
    		")",
    		"{",
    		"}",
    		"<>",
    		">",
    		"<",
    		"ID",
    		"NUM",
    		"ERROR"
    	 };
    
    void skipSpace()
    {
    	char c;
    
    	c = getchar(); 
    	line_no += (c == '\n');
    	while (!feof(stdin) && isspace(c))
    	{
    		c = getchar(); 
    		line_no += (c == '\n');
    	}
    
    	// return character to input buffer if eof is not reached
    	if (!feof(stdin)) 
    		ungetc(c, stdin);
    }
    
    int isKeyword(char *s)
    {
    	int i;
    
    	for (i = 1; i <= KEYWORDS; i++)
    		if (strcmp(reserved[i],s) == 0)
    			return i;
    	return FALSE;
    }
    
    int scan_number()
    {
    	char c;
    
    	c = getchar();
    	if (isdigit(c))
    	{
    		if (c == '0')
    		{
    			token[tokenLength] = c;
    			tokenLength++;
    		}
    		else
    		{
    			while (isdigit(c))
    			{
    				token[tokenLength] = c;
    				tokenLength++;;
    				c = getchar();
    			}
    			ungetc(c, stdin);
    		}
    		token[tokenLength] = '\0';
    		return NUM;
    	}
    	else
    		return ERROR;
    }
    
    
    int scan_id_or_keyword()
    {
    	int ttype;
    	char c;
    
    	c = getchar();
    	if (isalpha(c))
    	{
    		while (isalnum(c))
    		{
    			token[tokenLength] = c;
    			tokenLength++;
    			c = getchar();
    		}
    		if (!feof(stdin)) 
    			ungetc(c, stdin); 
    			 
    		token[tokenLength] = '\0';
    		ttype = isKeyword(token);
    		if (ttype == 0) 
    			ttype = ID;
    		return ttype;
    	} else
    		return ERROR;
    }
    
    void ungetToken()
    {
    	activeToken = TRUE;
    }
    
    int getToken()
    {
    	char c;
    
    	if (activeToken)
    	{
    		activeToken = FALSE;
    		return ttype;
    	}
    
    	skipSpace();
    	tokenLength = 0;
    	c = getchar();
    
    	switch (c)
    	{
    		case '+': ttype = PLUS; break;
    		case '-': ttype = MINUS; break;
    		case '/': ttype = DIV; break;
    		case '*': ttype = MULT; break;
    		case '=': ttype = EQUAL; break;
    		case ':': ttype = COLON; break;
    		case ',': ttype = COMMA; break;
    		case ';': ttype = SEMICOLON; break;
    		case '[': ttype = LBRAC; break;
    		case ']': ttype = RBRAC; break;
    		case '(': ttype = LPAREN; break;
    		case ')': ttype = RPAREN; break;
    		case '{': ttype = LBRACE; break;
    		case '}': ttype = RBRACE; break;
    		case '<':
    			c = getchar();
    			if (c == '>')
    				ttype = NOTEQUAL;
    			else
    			{
    				ungetc(c, stdin);
    				ttype = LESS;
    			}
    			break;
    
    		case '>':
    			ttype = GREATER;
    			break;
    
    		default :
    			if (isdigit(c))
    			{
    				ungetc(c, stdin);
    				ttype = scan_number();
    			}
    			else if (isalpha(c)) 
    			{
    				// token is either a keyword or ID
    				ungetc(c, stdin);
    				ttype = scan_id_or_keyword();
    			}
    			else if (c == EOF)
    			  ttype = EOF;
    			else
    			  ttype = ERROR;
    			break;
    	}	// End Switch
    	return ttype;
    }
    
    
    //---------------------------------------------------------
    // Execute
    void execute_program(struct statementNode * program)
    {
    	struct statementNode * pc = program;
    	int op1, op2, result;
    
    	while (pc != NULL)
    	{
    		switch (pc->stmt_type)
    		{
    			case NOOPSTMT:
    				pc = pc->next;
    				break;
    
    			case PRINTSTMT:
    				if (pc->print_stmt == NULL)
    				{
    					print_debug("Error: pc points to a print statement but pc->print_stmt is null.\n");
    					exit(1);
    				}
    				if (pc->print_stmt->id == NULL)
    				{
    					print_debug("Error: print_stmt->id is null.\n");
    					exit(1);
    				}
    				printf("%d\n", pc->print_stmt->id->value);
    				pc = pc->next;
    				break;
    
    			case ASSIGNSTMT:
    				if (pc->assign_stmt == NULL)
    				{
    					print_debug("Error: pc points to an assignment statement but pc->assign_stmt is null.\n");
    					exit(1);
    				}
    				if (pc->assign_stmt->op1 == NULL)
    				{
    					print_debug("Error: assign_stmt->op1 is null.\n");
    					exit(1);
    				}
    				if (pc->assign_stmt->op == PLUS || pc->assign_stmt->op == MINUS
    					|| pc->assign_stmt->op == MULT || pc->assign_stmt->op == DIV)
    				{
    					if (pc->assign_stmt->op2 == NULL)
    					{
    						print_debug("Error: right-hand-side of assignment is an expression but assign_stmt->op2 is null.\n");
    						exit(1);
    					}
    				}
    				if (pc->assign_stmt->lhs == NULL)
    				{
    					print_debug("Error: assign_stmt->lhs is null.\n");
    					exit(1);
    				}
    				switch (pc->assign_stmt->op)
    				{
    					case PLUS:
    						op1 = pc->assign_stmt->op1->value;
    						op2 = pc->assign_stmt->op2->value;
    						result = op1 + op2;
    						break;
    					case MINUS:
    						op1 = pc->assign_stmt->op1->value;
    						op2 = pc->assign_stmt->op2->value;
    						result = op1 - op2;
    						break;
    					case MULT:
    						op1 = pc->assign_stmt->op1->value;
    						op2 = pc->assign_stmt->op2->value;
    						result = op1 * op2;
    						break;
    					case DIV:
    						op1 = pc->assign_stmt->op1->value;
    						op2 = pc->assign_stmt->op2->value;
    						result = op1 / op2;
    						break;
    					case 0:
    						op1 = pc->assign_stmt->op1->value;
    						result = op1;
    						break;
    					default:
    						print_debug("Error: invalid value for assign_stmt->op (%d).\n", pc->assign_stmt->op);
    						exit(1);
    						break;
    				}
    				pc->assign_stmt->lhs->value = result;
    				pc = pc->next;
    				break;
    
    			case IFSTMT:
    				if (pc->if_stmt == NULL)
    				{
    					print_debug("Error: pc points to an if statement but pc->if_stmt is null.\n");
    					exit(1);
    				}
    				if (pc->if_stmt->true_branch == NULL)
    				{
    					print_debug("Error: if_stmt->true_branch is null.\n");
    					exit(1);
    				}
    				if (pc->if_stmt->false_branch == NULL)
    				{
    					print_debug("Error: if_stmt->false_branch is null.\n");
    					exit(1);
    				}
    				if (pc->if_stmt->op1 == NULL)
    				{
    					print_debug("Error: if_stmt->op1 is null.\n");
    					exit(1);
    				}
    				if (pc->if_stmt->op2 == NULL)
    				{
    					print_debug("Error: if_stmt->op2 is null.\n");
    					exit(1);
    				}
    				op1 = pc->if_stmt->op1->value;
    				op2 = pc->if_stmt->op2->value;
    				switch (pc->if_stmt->relop)
    				{
    					case GREATER:
    						if (op1 > op2)
    							pc = pc->if_stmt->true_branch;
    						else
    							pc = pc->if_stmt->false_branch;
    						break;
    					case LESS:
    						if (op1 < op2)
    							pc = pc->if_stmt->true_branch;
    						else
    							pc = pc->if_stmt->false_branch;
    						break;
    					case NOTEQUAL:
    						if (op1 != op2)
    							pc = pc->if_stmt->true_branch;
    						else
    							pc = pc->if_stmt->false_branch;
    						break;
    					default:
    						print_debug("Error: invalid value for if_stmt->relop (%d).\n", pc->if_stmt->relop);
    						exit(1);
    						break;
    				}
    				break;
    
    			case GOTOSTMT:
    				if (pc->goto_stmt == NULL)
    				{
    					print_debug("Error: pc points to a goto statement but pc->goto_stmt is null.\n");
    					exit(1);
    				}
    				if (pc->goto_stmt->target == NULL)
    				{
    					print_debug("Error: goto_stmt->target is null.\n");
    					exit(1);
    				}
    				pc = pc->goto_stmt->target;
    				break;
    
    			default:
    				print_debug("Error: invalid value for stmt_type (%d).\n", pc->stmt_type);
    				exit(1);
    				break;
    		}
    	}
    }
    
    //---------------------------------------------------------
    // Entry point
    void main()
    {   //var_section();
    
    
    	struct statementNode * program =
     parse_program_and_generate_intermediate_representation();
    	//printf("%d", program->stmt_type);
    	execute_program(program);
    	
    }
    parse_and_graph.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "compiler.h"
    struct ifStatement* relop()
    	{
    	struct ifStatement* relo;
    	ttype = getToken();
    	if (ttype == GREATER)
    		{ 
    		relo = malloc(sizeof(struct ifStatement));
    		relo->relop = GREATER;
    		relo->op1 = malloc(sizeof(struct varNode));
    		relo->op2 = malloc(sizeof(struct varNode)); 
    		relo->true_branch = malloc(sizeof(struct statementNode));
    		relo->false_branch = malloc(sizeof(struct statementNode));
    		relo->true_branch = NULL;
    		relo->false_branch = NULL;
    		return relo;
    		}else
    	if (ttype == LESS)
    		{
    		relo = malloc(sizeof(struct ifStatement));
    		relo->relop = LESS;
    		relo->op1 = malloc(sizeof(struct varNode));
    		relo->op2 = malloc(sizeof(struct varNode)); 
    		relo->true_branch = malloc(sizeof(struct statementNode));
    		relo->false_branch = malloc(sizeof(struct statementNode));
    		relo->true_branch = NULL;
    		relo->false_branch = NULL;
    		return relo;
    		}else
    	if (ttype == NOTEQUAL)
    		{
    		relo = malloc(sizeof(struct ifStatement));
    		relo->relop = NOTEQUAL;
    		relo->op1 = malloc(sizeof(struct varNode));
    		relo->op2 = malloc(sizeof(struct varNode)); 
    		relo->true_branch = malloc(sizeof(struct statementNode));
    		relo->false_branch = malloc(sizeof(struct statementNode));
    		relo->true_branch = NULL;
    		relo->false_branch = NULL;
    		return relo;
    
    		}
    	
    	return 0; 
    	}
    struct varNode * primary()
    	{ 
    	struct varNode *primar;
    	ttype = getToken();
    	if (ttype == NUM)
    		{
    		primar = malloc(sizeof(struct varNode));
    		primar->value = atoi(token);
    		return primar;
    		}
    	if (ttype == ID)
    		{
    		primar = malloc(sizeof(struct varNode));
    		primar->name ;//= malloc(sizeof(char) * 254);
    		strcpy(primar->name,token);
    		//printf("%s\t", primar->name);
    		return primar;
    		}
    
    return 0; 
    	}
    struct assignmentStatement * oper()
    	{
    	struct assignmentStatement * opp;
    	ttype = getToken();
    	if (ttype == PLUS)
    		{
    		opp = malloc(sizeof(struct assignmentStatement));
    		opp->op = PLUS;
    		return opp;
    		}
    	if (ttype == MINUS)
    		{
    		opp = malloc(sizeof(struct assignmentStatement));
    		opp->op = MINUS;
    		return opp;
    		}
    	if (ttype == MULT)
    		{
    		opp = malloc(sizeof(struct assignmentStatement));
    		opp->op = MULT;
    		return opp;
    		}
    	if (ttype == DIV)
    		{
    		opp = malloc(sizeof(struct assignmentStatement));
    		opp->op = DIV;
    		return opp;
    		}
    return 0;
    	}
    struct ifStatement * condition ()
    	{
    	struct ifStatement *cond;
    	
    	ttype = getToken();
    	if((ttype == NUM) | (ttype == ID))
    		{
    		
    		ungetToken();
    		cond = malloc(sizeof(struct ifStatement));
    		cond->op1 = primary();
    		ttype = getToken();
    		if ( (ttype == GREATER) | (ttype == LESS) | (ttype == NOTEQUAL) )
    			{
    			ungetToken();
    			cond = relop();
    			//cond->relop;
    			ttype = getToken();
    			if((ttype == NUM) | (ttype == ID))
    				{
    				ungetToken();
    				cond->op2 = primary();
    				return cond;
    				}
    			}
    		
    		}
    return 0;
    	}
    struct assignmentStatement * expr()
    	{
    	struct assignmentStatement * exp;
    	ttype = getToken();
    	if((ttype == NUM) | (ttype == ID))
    		{
    		ungetToken();
    		exp = malloc(sizeof(struct assignmentStatement));
    		//exp->lhs = NULL;
    		exp->op1 = primary();
    		//exp->op1->value; 
    		//printf("%d", exp->op1->value);
    		ttype = getToken();
    		if ((ttype == PLUS) | (ttype == MINUS) | (ttype == MULT) | (ttype == DIV))
    			{
    			ungetToken();
    			exp = oper();
    			//exp->op;
    			//printf("%d", exp->op);
    			ttype = getToken();
    			if((ttype == NUM) | (ttype == ID))
    				{
    				ungetToken();
    				exp->op2 = primary();
    				//printf("%d", exp->op2->value);
    				return exp;
    				}
    
    			}else{ungetToken();return exp;}
    		}
    return 0;
    	}
    struct assignmentStatement * assignment_stmt()
    	{
    	
    	struct assignmentStatement * assign;
    	
    	ttype = getToken();
    	if (ttype == ID)
    		{
    		ungetToken();
    		assign = malloc(sizeof(struct assignmentStatement));
    		assign->lhs = primary();
    		//printf("%s\t", assign->lhs->name);
    		ttype = getToken();
    		if (ttype == EQUAL)
    			{
    			assign = expr();
    			ttype = getToken();
    			if(ttype == SEMICOLON)
    				{
    				return assign;					
    				}
    			}
    		}
    return 0;
    	}
    struct printStatement * print()
    	{
    	struct printStatement * prin;
    	prin = malloc(sizeof(struct printStatement));
    	ttype  = getToken();
    	if (ttype == PRINT)
    		{
    		prin->id = primary();
    		ttype = getToken();
    		if(ttype == SEMICOLON)
    			{
    			return prin;
    			}
    		}
    return 0;
    	}
    struct statementNode * stmt()
    	{
    	struct statementNode *stmnt;
    	stmnt = malloc(sizeof(struct statementNode));
    	ttype =  getToken();
    	if(ttype == ID)
    		{
    		ungetToken();
    		stmnt->stmt_type = ASSIGNSTMT;
    		stmnt->assign_stmt = assignment_stmt();
    		stmnt->print_stmt = NULL;
    		stmnt->if_stmt = NULL;
    		stmnt->goto_stmt = NULL;
    		stmnt->next = NULL;
    		return stmnt;
    		}else
    	if(ttype == PRINT)
    		{
    		ungetToken();
    		stmnt->stmt_type = PRINTSTMT;
    		stmnt->assign_stmt = NULL;
    		stmnt->print_stmt = print();
    		stmnt->if_stmt = NULL;
    		stmnt->goto_stmt = NULL;
    		stmnt->next = NULL;
    		return stmnt;
    		}else
    	if(ttype == WHILE)
    		{
    		stmnt->stmt_type = IFSTMT;
    		stmnt->assign_stmt = NULL;
    		stmnt->print_stmt = NULL;
    		stmnt->if_stmt = NULL;
    		stmnt->goto_stmt = NULL;
    		stmnt->next = NULL;
    		return stmnt;
    		}else
    	if(ttype == REPEAT)
    		{
    		stmnt->stmt_type = IFSTMT;
    		stmnt->assign_stmt = NULL;
    		stmnt->print_stmt = print();
    		stmnt->if_stmt = NULL;
    		stmnt->goto_stmt = NULL;
    		stmnt->next = NULL;
    		return stmnt;
    		}else
    	if(ttype == IF)
    		{
    		stmnt->stmt_type = IFSTMT;
    		stmnt->assign_stmt = NULL;
    		stmnt->print_stmt = print();
    		stmnt->if_stmt = NULL;
    		stmnt->goto_stmt = NULL;
    		stmnt->next = NULL;
    		return stmnt;
    		}else
    	
    		{
    		stmnt->stmt_type = NOOPSTMT;
    		stmnt->assign_stmt = NULL;
    		stmnt->print_stmt = print();
    		stmnt->if_stmt = NULL;
    		stmnt->goto_stmt = NULL;
    		//stmnt->next;
    		return stmnt;
    		}
    	return 0;
    	}
    struct statementNode * stmt_list()
    	{
    	struct statementNode *list;
    	list = malloc(sizeof(struct statementNode));
    	ttype = getToken();
    	if ((ttype == ID) | (ttype == PRINT) | (ttype == WHILE) | (ttype == IF) | (ttype == REPEAT))
    		{
    		ungetToken();
    		list = stmt();
    		//printf("%d", list->stmt_type);
    		ttype = getToken();
    		if ((ttype == ID) | (ttype == PRINT) | (ttype == WHILE))
    			{
    			ungetToken();
    			list->next = stmt_list();
    			return list;
    			}else{return list;}
    		}
    	return 0;
    	}
    struct statementNode * body()
    	{
    	struct statementNode * bod;
    	bod = malloc(sizeof(struct statementNode));
    	ttype = getToken();
    	if (ttype == LBRACE)
    		{//printf("start");
    		bod = stmt_list();
    		ungetToken();
    		ttype = getToken();
    		if (ttype == RBRACE)
    			{//printf("stop");
    			return bod;
    			}
    		}
    	return 0;
    	}
    struct assignmentStatement * id_list()
    	{
    	struct assignmentStatement *idlist;
    	idlist = malloc(sizeof(struct assignmentStatement));
    	ttype = getToken();
    	if (ttype == ID)
    		{
    		ungetToken();
    		idlist->lhs = primary();
    		ttype = getToken();
    		if (ttype == COMMA)
    			{
    			ttype = getToken();
    			if (ttype == ID)
    				{
    				ungetToken();
    				idlist = id_list();
    				ungetToken();
    				return idlist;
    				}else{return idlist;}
    			}
    		}
    	return 0;
    	}
    struct statementNode * var_section()
    	{
    	struct statementNode *vars;
    	vars = malloc(sizeof(struct statementNode));
    	ttype = getToken();
    	if (ttype == ID)
    		{
    		ungetToken();
    		vars->assign_stmt = id_list();
    		//ungetToken();
    		ttype = getToken();
    		if (ttype == SEMICOLON)
    			{
    			//printf("ok");
    			return vars;
    			}
    		
    		}
    	return 0;
    	}
    struct statementNode * parse_program_and_generate_intermediate_representation()
    	{
    	struct statementNode * prog;
    	prog = malloc(sizeof(struct statementNode));
    	ttype = getToken();
    	if (ttype == ID)
    		{
    		ungetToken();
    		prog = var_section();
    		ungetToken();
    		ttype = getToken();
    		if (ttype == LBRACE)
    			{
    			//printf("OK");
    			ungetToken();
    			prog = body();
    			return prog;
    			
    			}
    		}
    	return 0;
    	}
    compiler.h
    Code:
    #ifndef _COMPILER_H_
    #define _COMPILER_H_
    
    /*
     * compiler.h
     * 
     */
     
    #define TRUE 1
    #define FALSE 0
    
    #define NOOPSTMT	100
    #define PRINTSTMT	101
    #define ASSIGNSTMT	102
    #define IFSTMT		103		// This is used for all control statements (if, while, repeat)
    #define GOTOSTMT	104
    
    #define KEYWORDS	7
    #define RESERVED	27
    #define VAR			1
    #define IF			2
    #define WHILE		3 
    #define REPEAT		4
    #define UNTIL 		5
    #define PRINT 		6
    #define ARRAY 		7
    #define PLUS 		8
    #define MINUS 		9
    #define DIV 		10
    #define MULT 		11
    #define EQUAL 		12
    #define COLON 		13
    #define COMMA 		14
    #define SEMICOLON 	15
    #define LBRAC 		16
    #define RBRAC 		17
    #define LPAREN 		18
    #define RPAREN 		19
    #define LBRACE 		20
    #define RBRACE 		21
    #define NOTEQUAL 	22
    #define GREATER 	23
    #define LESS 		24
    #define ID 			25
    #define NUM 		26
    #define ERROR 		27
    
    // This implementation does not allow tokens that are more
    // than 100 characters long
    #define MAX_TOKEN_LENGTH 100
    
    // The following global variables are defined in compiler.c:
    extern char token[MAX_TOKEN_LENGTH];
    extern int  ttype ;
    
    //---------------------------------------------------------
    // Data structures:
    
    struct varNode
    {
    	char* name;
    	int   value;
    };
    
    struct gotoStatement
    {
    	struct statementNode * target;
    };
    
    struct assignmentStatement
    {
    	struct varNode * lhs;
    	struct varNode * op1;
    	struct varNode * op2;
    	int op;		// PLUS, MINUS, MULT, DIV --> lhs = op1 op op2;
    				// 0                      --> lhs = op1;
    };
    
    struct printStatement
    {
    	struct varNode * id;
    };
    
    struct ifStatement
    {
    	struct varNode * op1;
    	struct varNode * op2;
    	int relop;	// GREATER, LESS, NOTEQUAL
    	struct statementNode * true_branch;
    	struct statementNode * false_branch;
    };
    
    struct statementNode
    {
    	int stmt_type;		// NOOPSTMT, PRINTSTMT, ASSIGNSTMT, IFSTMT, GOTOSTMT
    	struct assignmentStatement * assign_stmt;	// NOT NULL iff stmt_type == ASSIGNSTMT
    	struct printStatement      * print_stmt;	// NOT NULL iff stmt_type == PRINTSTMT
    	struct ifStatement         * if_stmt;		// NOT NULL iff stmt_type == IFSTMT
    	struct gotoStatement       * goto_stmt;		// NOT NULL iff stmt_type == GOTOSTMT
    	struct statementNode       * next;			// next statement in the list or NULL 
    };
    
    //---------------------------------------------------------
    // Functions that are provided:
    
    void print_debug(const char * format, ...);
    void ungetToken();
    int  getToken();
    
    
    struct statementNode * parse_program_and_generate_intermediate_representation();
    
    
    #endif /* _COMPILER_H_ */
    So my problem is when i run the code on say a;{a = 2;} i get "Error: assign_stmt->lhs is null.\n" which i can not understand why because in assign_stmt() i have assign->lhs = primary();
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Why do you continue to ignore warnings?

    C:\otros\dcw>gcc -Wall gatar02*.c
    gatar02a.c:386: warning: return type of `main' is not `int'
    gatar02b.c: In function `primary':
    gatar02b.c:62: warning: statement with no effect

    C:\otros\dcw>
    main is supposed to return int, not void. Newer compilers usually inform you of that fact and I think that C++ compilers won't let you get away with it.

    In primary() in your parse_and_graph.c file, Line 62 is highlighted below:
    Code:
    struct varNode * primary()
        { 
        struct varNode *primar;
        ttype = getToken();
        if (ttype == NUM)
            {
            primar = malloc(sizeof(struct varNode));
            primar->value = atoi(token);
            return primar;
            }
        if (ttype == ID)
            {
            primar = malloc(sizeof(struct varNode));
            primar->name ;//= malloc(sizeof(char) * 254);
            strcpy(primar->name,token);
            //printf("%s\t", primar->name);
            return primar;
            }
    
    return 0; 
        }
    Would that have any bearing on your problem? Though why you would ever have thought it a good idea to run a program that's broken is beyond me. If your program is throwing warnings, then it is broken! What part of that can you not understand?

    And that return 0. Really? The function is declared to return a struct pointer and you return an int? Whatever are you thinking?

    And I see from the other if statements in that file that you still have no clue what the difference is between a bit-wise OR and the OR relational operator. Ironic that you're writing a parser.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    8
    Rep Power
    0
    Yeah I've changed the OR's to relational ORS and the return 0's to their respective pointers. The commented out malloc was just for testing purposes and unfortunately it does not resolve the problem. I cant seem to find the reason why lhs is not being saved as a char when it is called by parse_program_and_generate_intermediate_representation()
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Too much code to try to puzzle out in the little free time we have. So I tried running it:
    C:TEST\gatar>a
    a;{a=2;}
    Error: invalid value for assign_stmt->op (947404114).

    C:TEST\gatar>
    That is not the same error message that you describe.

    How have you verified that your parser works? Is the assign_stmt node getting the right values assigned to it?

    I would recommend that you run the program in a debugger with breakpoints in the functions where assign_stmt is being built. Directly look at what values it contains.

    If you cannot do that and still must depend on us helping you, then do please help us to do that. What is the path of function calls that you expect the program to go through in order to parse that input? IOW, how is your program supposed to work?

    Without some aid like that, we would have to wait until we had a sizable chuck of free time to wade through all that. I know that won't come until this next weekend, if even then. So anything that will help us work more quickly would help you.

    PS

    And if you have indeed made changes as you allude to in Msg #3, then you should post the new code.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Here's something interesting.

    To begin with, I fixed that Line 62 in primary(), since without that fix the next line, the strcpy(), just crashed because of the uninitialized pointer.

    Consider these two runs with different inputs:
    C:TEST\gatar>a
    {a=2;}

    C:TEST\gatar>a
    a;
    {a=2;}
    Error: invalid value for assign_stmt->op (947404114).

    C:TEST\gatar>a
    a;{a=2;}
    Error: invalid value for assign_stmt->op (947404114).

    C:TEST\gatar>
    BTW, in the second one, when I just entered a; , the program then just sat there waiting for more input. Note then that it's only if a; had been entered before the {a=2;} that the error happens; {a=2;} without the preceding a; ran normally (I would assume).

    And just for S&G's, I tried this:
    C:TEST\gatar>a
    a;
    a;

    C:TEST\gatar>
    That ran to completion just as the {a=2;} had done.

    Those might give you some clues as to where to look.

IMN logo majestic logo threadwatch logo seochat tools logo