You are on page 1of 3

/*

Name:- Jaidip Ghosh


Topic:- Operator Precedence Parsing
*/
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node{ //node information
struct node *prev;
char val;
struct node * next;
}nodetype;
nodetype *head = NULL;
nodetype * create(){
//create a node
nodetype *p;
p = (nodetype *)malloc(sizeof(nodetype));
p->next = NULL;
p->prev=NULL;
return p;
}
void insert(char c){
nodetype *p,*q;
p=create();
p->val = c;
if(head==NULL)
head=p;
else{
q=head;
while(q->next!=NULL)
q=q->next;
p->prev=q;
q->next=p;
}
}
void display(){
nodetype *p;
p=head;
while(p->next!=NULL){
printf("%c ",p->val);
p=p->next;
}
printf("%c \n",p->val);
}
int update(){
nodetype *p;
int val=1;
p=head;
while(p!=NULL){
if((p->val=='<')||(p->val=='>')){
val=0;
p->prev->next=p->next;
p->next->prev = p->prev;
}
p=p->next;
}
if(val)
return val;
else
return val;

}
char table[5][5][1] = { 'N','i','+','*','$', //Parsing Table
'i','E','>','>','>',
'+','<','>','<','>',
'*','<','>','>','>',
'$','<','<','<','A'};
char str[20];
void main()
{
int i,len,j,value=0,ct=0;
nodetype *p,*q,*temp,*temp2,*temp3;
printf("Enter the string(Note:-start with $ and end with $):- ");
gets(str);
//contains the input string
len = strlen(str);
//string length
for(i=0;i<len;i++)
insert(str[i]);
printf("Contents of the Linked List\n");
display();
do{
p=head;
q=p->next;
while(q!=NULL){ //to update the string with < and >
for(i=1;i<5;i++)
if(table[i][0][0]==p->val)
break;
for(j=1;j<5;j++)
if(table[0][j][0]==q->val)
break;
if(table[i][j][0]=='E')
break;
else{
temp=create();
temp->val = table[i][j][0];
temp->prev=p;
p->next = temp;
q->prev= temp;
temp->next = q;
p=q;
q=q->next;
}
}
if(table[i][j][0]=='E') //if the string is ii then it an error
break;
display();
//display the contents
temp=head;
temp2=temp->next;
while(temp!=NULL){
//to remove the identified handles
while(temp->val!='>'){
temp=temp->next;
if(temp==NULL){
break;
}
}
if(temp==NULL){
continue;
}
else{
temp2=temp->prev;
while(temp2->val!='<'){
temp2=temp2->prev;

if(temp2==NULL){
break;
}
}
if(temp2==NULL){
break;
}
else{
temp3=temp2->prev;
temp3->next = temp->next;
temp->next->prev=temp3;
temp=temp3;
}
}
}
value=update();
if(value==0 && ct==0)
break;
ct++;
}while(head->next->val!='$');
display();
p=head;
q=p->next;
for(i=1;i<5;i++)
if(table[i][0][0]==p->val) //to determine the row
break;
for(j=1;j<5;j++)
if(table[0][j][0]==q->val) //to determine the column
break;
if(table[i][j][0]=='A') //String is accepted
printf("Accept\n");
else
printf("String is Not Accepted\n");
}

You might also like