The Programming Project: university
Showing posts with label university. Show all posts
Showing posts with label university. Show all posts

Wednesday, July 10, 2013

Tokens

Given a program statement in C, list the tokens in the order as they appear in the source statement. Assume that the source statement may contain more than one blank space between two successive tokens and classify the tokens

      

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
void  classification(char iden[]);
#define TRUE 1
#define FALSE 0
int main(void)
{

     char *str,ar[10];
    char *token;
     int FLAG=FALSE,count=0,n,len,i,t=0;
    printf("\n Enter the maximum length of the statement:");
     scanf(" %d", &n);
     str=(char *)malloc( n*sizeof(char));
    fflush(stdin);
    printf("\n Enter the C statement:");
     scanf("%[^\n]",str);
     len=strlen(str);
     if(len>n)
        {
        printf("\n Length of the statement is greater than the input length:");
      exit(1);
        }
    token=str;
 //   printf("\n The tokens are:\n");
    while(1)
        {
        while(*token!= ' ')
            {
            if(*token== '\0')
                {
                FLAG=TRUE;
                break;
                }
             //    printf("%c",*token);
                ar[count++]=*token;
                token++;
                }
                ar[count]='\0';
      //      printf(" %s ",ar);
             classification(ar);
      //   printf("\n");
          if(FLAG==TRUE)
            break;
        while(*token== ' ')    
            {
            *token++;
            if(*token== '\0')
                {
                     FLAG=TRUE;
                break;
                }
                }
          if(FLAG==TRUE)
                break;
          count=0;
          }
     return 0;
}
void classification( char ar[])
{

int  FLAG=FALSE,len,count=0,flag=TRUE,numb=0,d,t=0;
char *keyword[]={ "if","for","while","int","float","char"};
char *operatr[]={ "+","-","*","/","=","<",">"};
char *symbols[]={ "{","}",",","(",")",";","?",":"};
int i=0;
len=strlen(ar);
for(i=0;i<len;i++)
            if(ar[i]==' ')
                t++;
            if(t==len)
         return;
if(len>8)
    FLAG=FALSE;
for(i=0;i<=5;i++)
    {
    if(strcmp(keyword[i],ar)==0)
      {
      printf("\n%s\tis a Keyword:",ar);
      FLAG=TRUE;
      flag=FALSE;
      }
    }
    i=0;
for(i=0;i<=6;i++)
    {
    if(strcmp(operatr[i],ar)==0)
        {
        printf("\n%s\tis a Operator:",ar);
        FLAG=TRUE;
        flag=FALSE;
        }
    }
    i=0;
for(i=0;i<=7;i++)
    {
    if(strcmp(symbols[i],ar)==0)
        {
        printf("\n%s\tis a Symbol:",ar);
        FLAG=TRUE;
        flag=FALSE;
        }
    }
if ( ((65 <= ar[0] && ar[0] <= 93 )|| ( 97 <= ar[0] && ar[0] <= 122)) && len <= 8 && FLAG==FALSE)
    {
    printf("\n%s\tis an Identifier:",ar);
    FLAG=TRUE;
    flag=FALSE;
    }
for(i=0;i<len;i++)
    {
    if(49 <= ar[i] && ar[i] <=57 )
        count++;
    else
       continue;
    }
if(count==len && flag==TRUE && FLAG==FALSE)
    {
    printf("\n%s\tis a Constant:",ar);
    FLAG=TRUE;
    }
i=0;
if( FLAG==FALSE)
    {
    if(ar[0]=='-' || ar[0]=='0')
        numb=atoi(ar);
    if(numb <= 0)
    printf("%d\tis not a valid Constant ",numb);
   else
    printf("\n%s\tis an INVALID TOKEN:",ar);
   }
printf("\n");
return;
}

Monday, June 10, 2013

Interchanging specific bits

There are N bytes stored from m/m location 0001H. The value of N is stored in 000H. Write an 8085 program to interchange the bits D2 and D6 and store them into the m/m locations starting from 0010H.

LXI H,0000H
MOV B,M
INX H
LXI D,0010H
XRA A
MOV C,A
LOOP: MOV A,M
ANI 0EDH
ADD C
MOV C,A
MOV A,M
ANI 02H
RLC
RLC
RLC
ADD C
MOV C,A
MOV A,M
ANI 10H
RRC
RRC
RRC
ADD C
MVI C,00H
STAX D
INX H
INX D
DCR B
JNZ LOOP
HLT


Friday, March 29, 2013

C Program #19


/* Dynamic Implementation of queue and few basic operations!*/

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define TRUE 1
#define FALSE 0
struct queue{
                                                int items;
                                                struct queue *next;
                                                };
typedef struct queue node;
struct stack{
                        int top;
                        int items[MAXSIZE];
                        };
struct stack stk;
struct queue p,q,temp;
int pop(struct stack *);
int remove(node **);
int Isempty(node *);
void push(struct stack *,int);
int isempty_st(struct stack *);
void insert(node *,int);
void reverse_queue(node **);
void display_front_rear(node **);
void display_rear_front(node **);
void append(node *,node *);
void replace(node**,int,int,node **);
void display_front_rear_unchanged(node **,node **);
void display_rear_front_unchanged(node **,node **);
int Isequal(node **,node**,node**,node**);
int main()
{
            int choice,x,ins,e;
            stk.top=-1;
            node *rootp,*rootq,*tqueue,*rqueue;
            rootp=(node *)malloc(sizeof(node));
            rootp->next=NULL;
            rootq=(node *)malloc(sizeof(node));
            rootq->next=NULL;
            tqueue=(node *)malloc(sizeof(node));
            tqueue->next=NULL;
            rqueue=(node *)malloc(sizeof(node));
            rqueue->next=NULL;
            do
                        {
                        printf("\n Press 1 to add elements in Queue:");
                        printf("\n Press 2 to remove elements from Queue:");
                        printf("\n Press 3 to append queue Q at the end of queue P:");
                        printf("\n Press 4 to print elements of queue P from front to end with P becoming empty:");
                        printf("\n Press 5 to print elements of queue P from rear to front with P becoming empty:");
                        printf("\n Press 6 to print elements of queue P from front to end with P remaining same:");
                        printf("\n Press 7 to print elements of queue P from rear to front with P remaining same:");
                        printf("\n Press 8 to reverse the queue P:");
                        printf("\n Press 9 to replace a specific element of a queue:");
                        printf("\n Press 10 to check whether the queues P and Q are equal or not:");
                        printf("\n Type 11 to exit:");
                        scanf("%d",&choice);
                        switch(choice)
                                    {
                                    case 1:
                                                            printf("\n Enter 1 to insert in queue P, 2 to insert in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                                                    printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(rootp,ins);
                                                                                    break;
                                                                                    case 2:
                                                                                    printf("\n Enter the element to be inserted:");
                                                                                    scanf("%d",&ins);
                                                                                    insert(rootq,ins);
                                                                                    break;
                                                                                    }
                          break;
                          case 2:
                                                            printf("\n Enter 1 to remove in queue P, 2 to remove in queue Q:");
                                                            scanf("%d",&x);
                                                                        switch(x)
                                                                                    {
                                                                                    case 1:
                                                                                    e=remove(&rootp);
                                                                                    //if(e=-9999)
                                                                                    //                      {}
                                                                           //       else
                                                                                    printf("\n Element removed is:%d\n",e);
                                                                                    break;
                                                                                    case 2:
                                                                                    e=remove(&rootq);
                                                                                    //if(e=-9999)
                                                                                    //          {}
                                                                                    //else
                                                                                    printf("\n Element removed is: %d\n",e);
                                                                                    break;
                                                                                    }
                                    break;
                                    case 3:
                                    append(rootp,rootq);
                                    break;
                                    case 4:
                                    display_front_rear(&rootp);
                                    break;
                                    case 5:
                                    display_rear_front(&rootp);
                                    break;
                                    case 6:
                                    display_front_rear_unchanged(&rootp,&tqueue);
                                    break;
                                    case 7:
         case 8:
                                    reverse_queue(&rootp);
                                    break;
                                    display_rear_front_unchanged(&rootp,&tqueue);
                                    break;
                                    case 9:
                                    printf("\n Enter the element to be replaced:");
                                    scanf("%d",&e);
                                    printf("\n Enter the element to be placed:");
                                    scanf("%d",&x);
                                    replace(&rootp,e,x,&tqueue);
                                    break;
                                    case 10:
                                    if(Isequal(&rootp,&rootq,&tqueue,&rqueue))
                                                printf("\n Queues are equal:\n");
                                    else
                                                printf("\n Queues are not equal:\n");
                                    break;
                                    }
                        }while(choice>=1 && choice<=10);
return 0;
}
int Isequal(node **qp,node **qq,node **t1,node **t2)
            {
            int FLAG=TRUE,s1=0,s2=0,x,y;
            if((Isempty(*qp) && !Isempty(*qq)) ||(!Isempty(*qp) && Isempty(*qq)))
                        return (FALSE);
            else if(Isempty(*qp) && Isempty(*qq))
                        return (TRUE);
            else
                                    {
                                    while(!Isempty(*qp))
                                                {insert(*t1,remove(qp)); s1++;}
                                    while(!Isempty(*qq))
                                                {insert(*t2,remove(qq)); s2++;}
                                    if(s1!=s2)
                                                return (FALSE);
                                    else
                                                {
                                                while(!Isempty(*t1))
                                                                        {
                                                                        x=remove(t1); y=remove(t2);
                                                                        if(x!=y)
                                                                        FLAG=FALSE;
                                                                        insert(*qp,x); insert(*qq,y);
                                                                        }
                                                }
                                     if(FLAG==TRUE)
                                                return (TRUE);
                                     else
                                                return (FALSE);
                                    }

   }
void replace(node**qp,int e,int x,node **qq)
            {
            int y;
            node *tmp;
            tmp=*qp;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                        insert(*qq,remove(qp));
            while(!Isempty(*qq))
                        {
                        y=remove(qq);
                        if(y==e)
                                    {
                                    printf("%d ",x);
                                    insert(tmp,x);
               }
                        else
                                    {
                                    insert(tmp,y);
                                    printf("%d ",y);
                                    }
                        }
            printf("\n");
            }
void display_rear_front_unchanged(node **qp,node **qq)
            {
            int x;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                         {
                         x=remove(qp);
                         push(&stk,x);
                         insert(*qq,x);
                         }
            while(!isempty_st(&stk))
                        {
                        printf("%d ",pop(&stk));
                        insert(*qq,remove(qq));
                        }
            printf("\n");
            }
void display_front_rear_unchanged(node **qp,node **qq)
            {
            int x;
            node *tmp;
            tmp=*qp;
            printf("\n Elements of the queue:");
            while(!Isempty(*qp))
                        insert(*qq,remove(qp));
            while(!Isempty(*qq))
                        {
                        x=remove(qq);
                        printf("%d ",x);
                        insert(tmp,x);
                        }
            printf("\n");
            }

void reverse_queue(node **use)
{
            node *curr,*prev;
   prev=(node *)malloc(sizeof(node));
            prev->next=NULL;
            curr=*use;
            while((*use)->next!=NULL)
                        {
                                                curr=*use;
                                                *use=(*use)->next;
                                                curr->next=prev;
                                                prev=curr;
                        }
                        *use=curr;
}
void display_rear_front(node **use)
            {
            printf("\n Elements of the queue:");
            while(!Isempty(*use))
                        push(&stk,remove(use));
            while(!isempty_st(&stk))
                        printf("%d ",pop(&stk));
            printf("\n");
            }
void display_front_rear(node **use)
            {
            printf("\n Elements of the queue:");
            while(!Isempty(*use))
                        printf("%d ",remove(use));
            printf("\n");
            }
void append(node *p,node *q)
            {
            while(p->next->next!=NULL)
                        p=p->next;
            p->next=q;
            }
int Isempty(node *use)
            {
            return ((use->next==NULL)? TRUE : FALSE);
            }
int remove(node **use)
            {
            if(Isempty(*use))
                        {printf("\n Queue is empty:\n");
                        return (-9999);}
            else{
            node *temp,*tp;
            temp=*use;
            tp=temp;
            temp=temp->next;
            *use=temp;
            return(tp->items);
            free(tp);}
            }
void insert(node *use,int x)
            {
            node *p;
            while(use->next!=NULL)
            use=use->next;
            use->items=x;
            p=(node *)malloc(sizeof(node));
            use->next=p;
            p->next=NULL;
            }
int isempty_st(struct stack *stk)
            {
             return((stk->top==-1)?TRUE:FALSE);
            }
int pop(struct stack *stk)
            {
            return(stk->items[(stk->top)--]);
            }
void push(struct stack *stk,int x)
            {
            stk->items[++stk->top]=x;
            }