dimanche 16 novembre 2014

Yacc/Lex giving segmentation fault with this code


Vote count:

0




I am trying out a homework assignment. The assignment is to generate Intermediate Code. For this I am running the following Yacc program, along with Lex. However, it is giving me a segmentation fault. Why does it give a segmentation fault? Here is the code.



%{
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<ctype.h>

char datatype[5];
char temporaryVariable[5];
int assignment=0;
int tempvarnum=0;

struct sym
{
char datatype[5];
char varname[5];
int size,location;
struct sym *next;
}*first;

struct quadruple
{
char *src1;
char *src2;
char *op;
char *tmp;
struct quadruple *next;
}*qfirst;

char* typeOf(char *lab);


void yyerror(const char *st)
{}
%}

%left '+' '-'
%left '*' '/'
%right '^'
%union
{
struct ICG
{
char *lab;
char code[100];
char datatype[5];
}Icg;
}
%token <Icg> ID
%token INT FLOAT CHAR
%type <Icg> E

%%
S: S Decl
| Decl
| S Assn
| Assn
;

Decl:Type List ';' {printf("Read declaration list");}
;

List:List ',' ID {printf("created node\n");createNode($3.lab,datatype);}
| ID {printf("created node\n");createNode($1.lab,datatype);}
;

Type: INT {strcpy(datatype,"int");}
| FLOAT {strcpy(datatype,"float");}
| CHAR {strcpy(datatype,"char");}
;

Assn:ID '=' E ';' {printf("Assignment statement");assignment=0;}
;


E: E '+' E {printf("Entering code");code(&$$,&$1,&$3,'+');}
| E '-' E {code(&$$,&$1,&$3,'-');}
| E '*' E {code(&$$,&$1,&$3,'*');}
| E '/' E {code(&$$,&$1,&$3,'/');}
| ID {printf("ID");strcpy($$.code,$1.lab); strcpy($$.lab,$1.lab);strcpy($$.datatype,typeOf($1.lab));}
;
%%

void code(struct ICG* one, struct ICG* two, struct ICG* three, char *operator)
{
printf("In code");
char tempvarname[5];
char code[100];
sprintf(tempvarname,"t%d=",tempvarnum++);
strcpy(one->lab,tempvarname);
strcpy(one->lab,two->lab);
createNode(one->lab,one->datatype);
strcpy(code,tempvarname);
strcat(code,two->lab);
strcat(code,three->lab);
strcat(code,operator);
strcat(code,"\n");

if(assignment==0)
{
strcpy(one->code,code);
assignment=1;
}
else
{
strcat(one->code,code);
}
createQuadruples(two->lab,three->lab,operator,one->lab);
}

void createQuadruples(char *lab2,char*lab3,char *operator,char*lab1)
{
struct quadruple* next=qfirst;
if(!next)
{
struct quadruple* new=(struct quadruple*)malloc(sizeof(struct quadruple));
strcpy(new->src1,lab2);
strcpy(new->src2,lab3);
strcpy(new->op,operator);
strcpy(new->tmp,lab1);
new->next=NULL;
qfirst=new;
}
else
{
while(next->next)
{
next=next->next;
}
struct quadruple* new=(struct quadruple*)malloc(sizeof(struct quadruple));
strcpy(new->src1,lab2);
strcpy(new->src2,lab3);
strcpy(new->op,operator);
strcpy(new->tmp,lab1);
new->next=NULL;
next->next=new;
}
}

void displayCode()
{
struct quadruple* temp=qfirst;
int i=0;
printf("\t| %s | Label | size | location |\n","Index");
while(temp)
{
printf("\t|%7d|%7s|%6s|%7s|%8s|\n",i++,temp->src1,temp->op,temp->src2,temp->tmp);
temp=temp->next;
}
}

char* typeOf(char *lab)
{
struct sym *new=first;
while(new)
{
if(strcmp(new->varname,lab)==0)
{
return new->datatype;
}
new=new->next;
}
}




void createNode(char *name, char *type)
{
struct sym* new=first;
int size=0;
if(strcmp(type,"char")==0)
size=1;
if(strcmp(type,"float")==0)
size=4;
if(strcmp(type,"int")==0)
size=2;

if(!new)
{
struct sym* next=(struct sym*)malloc(sizeof(struct sym));
strcpy(next->datatype,type);
strcpy(next->varname,name);
next->size=size;
next->location=0;
next->next=NULL;
first=next;
}
else
{
while(new->next)
{
new=new->next;
}
struct sym* next=(struct sym*)malloc(sizeof(struct sym));
strcpy(next->datatype,type);
strcpy(next->varname,name);
next->size=size;
next->location=new->location+new->size;
next->next=NULL;
new->next=next;
}
}
int main()
{
yyparse();
printf("In main");
displayCode();
}


And the corresponding lex file is this:



%{
#include<stdio.h>
#include "y.tab.h"
%}

letter [a-zA-Z]
digit [0-9]

%%
"int" {return INT;}
"float" {return FLOAT;}
"char" {return CHAR;}
"+"|"-"|"*"|"/"|"="|","|";" {return yytext[0];}
{letter}({letter}|{digit})* {yylval.Icg.lab=yytext;return ID;}
%%


I did try to debug the program, but it is simply getting me nowhere. And I don't even how to start debugging it. I did try with printf statements, but I found that they didn't help much.


The only thing I know for sure is that it is detecting the Identifiers.



asked 1 min ago







Yacc/Lex giving segmentation fault with this code

Aucun commentaire:

Enregistrer un commentaire