Icgarrayy

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

%{

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char place[10],array[10],offset[10];
int ndim;
}*n;
int count=0;
char temp[10];
int m;
%}
%union { char str[10]; struct node* n; }
%type<str> ID
%type<n> L E Elist
%token ID
%left '(' ')'
%left '*' '/'
%left '+' '-'
%start start
%%
start: start PROG
| PROG
;
PROG: S '\n' { count=0; }
;
S: L '=' E
{
//printf("\nstart symbol reached");
if(strcmp($1->offset,"#")==0)
{
printf("\n%s=%s",$1->place,$3->place);
}
else
{
printf("\n%s[%s]=%s",$1->place,$1->offset,$3->place);
}
};
E: L
{
//printf("\nE->L");
struct node* n=(struct node*)malloc(sizeof(struct node));
if(strcmp($1->offset,"#")==0)
{
strcpy(n->place,$1->place);
}
else
{
count++;
if(count>=10)
{
temp[1]=(count/10) + '0';
temp[2]=(count%10) + '0';
temp[3]='\0';
}
else
temp[1]=count+'0';
strcpy(n->place,temp);

printf("\n%s=%s[%s]",temp,$1->place,$1->offset);
}
$$=n;
}
;
L: Elist ']'
{
//printf("\nL->Elist]");
struct node* n=(struct node*)malloc(sizeof(struct node));
count++;
if(count>=10)
{
temp[1]=(count/10) + '0';
temp[2]=(count%10) + '0';
temp[3]='\0';
}
else
temp[1]=count+'0';
strcpy(n->place,temp);
printf("\n%s=C(%s)",temp,$1->array);
count++;
if(count>=10)
{
temp[1]=(count/10) + '0';
temp[2]=(count%10) + '0';
temp[3]='\0';
}
else
temp[1]=count+'0';
strcpy(n->offset,temp);
printf("\n%s=%s * w(%s)",temp,$1->place,$1->array);
$$=n;
}
;
L: ID
{
//printf("\n%s matched to L",$1);
n=(struct node*)malloc(sizeof(struct node));
strcpy(n->offset,"#");
strcpy(n->place,$1);
$$=n;
}
;
Elist : Elist ',' E
{
//printf("\nlist of elements in Elist");
count++;
if(count>=10)
{
temp[1]=(count/10) + '0';
temp[2]=(count%10) + '0';
temp[3]='\0';
}
else
temp[1]=count+'0';
m=$1->ndim+1;
printf("\n%s=%s*n%d",temp,$1->place,m);
printf("\n%s=%s+%s",temp,temp,$3->place);
n=(struct node*)malloc(sizeof(struct node));
strcpy(n->array,$1->array);

strcpy(n->place,temp);
n->ndim=m;
$$=n;
}
;
Elist: ID '[' E
{
//printf("\nID[E");
n=(struct node*)malloc(sizeof(struct node));
strcpy(n->array,$1);
strcpy(n->place,$3->place);
n->ndim=1;
$$=n;
}
;
E: E '+' E
{
n=(struct node*)malloc(sizeof(struct node));
count++;
if(count>=10)
{
temp[1]=(count/10) + '0';
temp[2]=(count%10) + '0';
temp[3]='\0';
}
else
temp[1]=count+'0';
strcpy(n->place,temp);
$$=n;
printf("\n%s=%s+%s",temp,$1->place,$3->place);
}
| E '-' E
{
n=(struct node*)malloc(sizeof(struct node));
count++;
if(count>=10)
{
temp[1]=(count/10) + '0';
temp[2]=(count%10) + '0';
temp[3]='\0';
}
else
temp[1]=count+'0';
printf("\n%s=%s-%s",temp,$1->place,$3->place);
strcpy(n->place,temp);
$$=n;
}
| E '*' E
{
n=(struct node*)malloc(sizeof(struct node));
count++;
if(count>=10)
{
temp[1]=(count/10) + '0';
temp[2]=(count%10) + '0';
temp[3]='\0';
}
else

temp[1]=count+'0';
printf("\n%s=%s*%s",temp,$1->place,$3->place);
strcpy(n->place,temp);
$$=n;
}
| E '/' E
{
n=(struct node*)malloc(sizeof(struct node));
count++;
if(count>=10)
{
temp[1]=(count/10) + '0';
temp[2]=(count%10) + '0';
temp[3]='\0';
}
else
temp[1]=count+'0';
printf("\n%s=%s/%s",temp,$1->place,$3->place);
strcpy(n->place,temp);
$$=n;
}
;
%%
int main()
{
temp[0]='t';
temp[1]='0';
temp[2]='\0';
yyparse();
return 0;
}
void yyerror(char* s)
{
printf("\nerror!");
}

You might also like