Showing posts with label Semester 6. Show all posts
Showing posts with label Semester 6. Show all posts

Kannur University Exam Results - Semester 6 - Btech -2011 - Regular/Supplimentary/Improvement

Kannur University Exam Results - Btech -2011

Semester 6 - Regular/Supplimentary/Improvement




Kannur University has published the results for B.Tech  Semester 6 Examinations on 15/11/2011. The candidates can access their results from the official site of kannur university.Follow the link given below.




Matrix Multiplication - TCP - Client Server Program - Network-DBMS Lab - C Program

Program:

// Client Program: mc.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>

main()
{
 struct sockaddr_in client,server;
 int a[10][10],b[10][10],c[10][10],mt[4];
 int s,sock,r,i,j;
 client.sin_family=AF_INET;
 client.sin_port=3000;
 client.sin_addr.s_addr=inet_addr("127.0.0.1");
 s=socket(AF_INET,SOCK_STREAM,0);
 connect(s,(struct sockaddr *)&client,sizeof(client));
 
 printf("Enter order of matrix 1:");
  scanf("%d %d",&mt[0],&mt[1]);
 printf("Enter order of matrix 2:");
  scanf("%d %d",&mt[2],&mt[3]);
 if(mt[1]!=mt[2])
  printf("Matrices cannot be multiplied");
 else
 {
  send(s,&mt,sizeof(mt),0);
  printf("Enter Elements for matrix 1:\n");
  for(i=0;i<mt[0];i++)
  {
   for(j=0;j<mt[1];j++)
   {
    scanf("%d",&a[i][j]);
   }
  }
  send(s,&a,sizeof(a),0);
  printf("Enter Elements for matrix 2:\n");
  for(i=0;i<mt[2];i++)
  {
   for(j=0;j<mt[3];j++)
   {
    scanf("%d",&b[i][j]);
   }
  } 
  send(s,&b,sizeof(b),0);
  
  recv(s,&c,sizeof(c),0);
  printf("\nResult:\n"); 
  for(i=0;i<mt[0];i++)
  {
   printf("\n");  
   for(j=0;j<mt[3];j++)
   {
    printf(" %d ",c[i][j]);
   }
  } 
 }
 printf("\n"); 
 close(s);
}




Server Program: ms.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>

main()
{
 struct sockaddr_in client,server;
 int a[10][10],b[10][10],c[10][10],mt[4];
 int s,sock,r,i,j,sz,k;
 server.sin_family=AF_INET;
 server.sin_port=3000;
 server.sin_addr.s_addr=inet_addr("127.0.0.1");
 s=socket(AF_INET,SOCK_STREAM,0);
 bind(s,(struct sockaddr *)&server,sizeof(server));
 listen(s,1);
 sz=sizeof(server);
 sock=accept(s,(struct sockaddr *)&server,&sz);
 recv(sock,&mt,sizeof(mt),0);
 recv(sock,&a,sizeof(a),0);
 printf("\nReceived Matrix 1\n");
 for(i=0;i<mt[0];i++)
 {
  printf("\n");  
  for(j=0;j<mt[1];j++)
  {
   printf(" %d ",a[i][j]);
  }
 } 
  
 recv(sock,&b,sizeof(b),0);
 printf("\nReceived Matrix 2\n"); 
 for(i=0;i<mt[2];i++)
 {
  printf("\n");  
  for(j=0;j<mt[3];j++)
  {
   printf(" %d ",b[i][j]);
  }
 } 
  
 printf("\nMultiplying........\n"); 
 for(i=0;i<mt[0];i++)
 for(j=0;j<mt[3];j++)
 {
  c[i][j]=0;
  for(k=0;k<mt[1];k++)
   c[i][j]+=a[i][k]*b[k][j];
 }
 printf("\nResult:\n") ; 
 for(i=0;i<mt[0];i++)
 {
  printf("\n");  
  for(j=0;j<mt[3];j++)
  {
   printf(" %d ",c[i][j]);
  }
 }
 printf("\n"); 
 send(sock,&c,sizeof(c),0);
 close(sock);
 close(s); 
}



Output:

Terminal 1: (Server)
nn@linuxmint ~ $ gcc ms.c -o s
nn@linuxmint ~ $ ./s

Received Matrix 1

1 1 
1 1 
Received Matrix 2

2 2
2 2
Multiplying........

Result:

4 4 
4 4 
nn@linuxmint ~ $ 

Terminal 2: (Client)
nn@linuxmint ~ $ gcc mc.c -o c
nn@linuxmint ~ $ ./c
Enter order of matrix 1:2
2
Enter order of matrix 2:2
2
Enter Elements for matrix 1:
1
1
1
1
Enter Elements for matrix 2:
2
2
2
2

Result:

4 4 
4 4
nn@linuxmint ~ $ 

Floating Point & Integers Lex Program Compiler Design

Program:


// frn.l


%{

%}

DIGIT    [0-9]

%%

{DIGIT}*        {ECHO;printf(" Integer");}
{DIGIT}*?\.{DIGIT}*    {ECHO;printf(" Float ");}

%%
main()
{
    yylex();
}


Output:


nn@linuxmint ~ $ lex frn.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<test1.txt
1 Integer
500 Integer
10.1 Float
.1 Float
1.0 Float

nn@linuxmint ~ $

// input : test1.txt

1
500
10.1
.1
1.0

Parser for SQL Nested Queries - Compiler Design - Yacc Program

Program:

// Lex file: sq.l
alpha [A-Za-z]
digit [0-9]
%%

[ \t\n]
select        return SELECT;
distinct     return DISTINCT;
from         return FROM;
where       return WHERE;
like           return LIKE;
desc         return DESC;
asc           return ASC;
"group by"    return GROUP;
having           return HAVING;
"order by"     return ORDER;
or                  return OR;
and                return AND;
in                   return IN;
{digit}+        return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="              return LE;
">="              return GE;
"=="              return EQ;
"!="               return NE;
.                     return yytext[0];
%%

// Yacc file: sq.y

%{
#include <stdio.h>
#include <stdlib.h>

%}
%token ID NUM SELECT DISTINCT FROM WHERE LE GE EQ NE OR AND LIKE GROUP HAVING ORDER ASC DESC IN
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE

%%

    S    : ST1';' {printf("INPUT ACCEPTED...\n");exit(0);};
    ST1    : SELECT attributeList FROM tableList ST2
        | SELECT DISTINCT attributeList FROM tableList ST2
        ;
    ST2    : WHERE COND ST3
        | ST3
        ;
    ST3    : GROUP attributeList ST4
        | ST4
        ;
    ST4    : HAVING COND ST5
        | ST5
        ;
    ST5    : ORDER attributeList ST6
        |
        ;
    ST6    : DESC
        | ASC
        |
        ;
  attributeList :     ID','attributeList
        | '*'
        | ID
        ;
 tableList    : ID',' tableList
        | ID
        ;
    COND    : COND OR COND
        | COND AND COND
        | E
        | ID IN '(' ST1 ')'
        ;
    E    : F'=' F
        | F '<' F
        | F '>' F 
        | F LE F
        | F GE F
        | F EQ F
        | F NE F
        | F OR F
        | F AND F
        | F LIKE F
        ;
    F    : ID
        | NUM 
        ;
%%
#include"lex.yy.c"
#include<ctype.h>
main()
{
    printf("Enter the query:");
    yyparse();
}       

Output:

nn@linuxmint ~ $ lex sq.l
nn@linuxmint ~ $ yacc sq.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the query:select model from product where manufacture in ( select manuid from manufactures where manufacture = keltron);
INPUT ACCEPTED...
nn@linuxmint ~ $

Parser for Switch Statements with If-then and While Statements inside - Compiler Design - Yacc Program

Program:

// Lex file: pars.l


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

%%

[ \n\t]
if                  return IF;
then             return THEN;
while           return WHILE;
switch         return SWITCH;
case             return CASE;
default         return DEFAULT;
break           return BREAK;
{digit}+       return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="            return LE;
">="            return GE;
"=="            return EQ;
"!="             return NE;
"&&"           return AND;
"||"               return OR;
.                   return yytext[0];

%%

// Yacc file: pars.y

%{
#include<stdio.h>
#include<stdlib.h>
%}

%token ID NUM SWITCH CASE DEFAULT BREAK LE GE EQ NE AND OR IF THEN WHILE
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'

%%

S    :    ST{printf("\nInput accepted.\n");exit(0);};
      ;
ST  :    SWITCH'('ID')''{'B'}'
      ;
B    :    C
      |    C D
      ;
C  :    C C
    |    CASE NUM':'ST1 BREAK';'
    ;
D  :    DEFAULT':'ST1 BREAK';'
    |    DEFAULT':'ST1
    ;
ST1    :    WHILE'('E2')' E';'
    |    IF'('E2')'THEN E';'
    |    ST1 ST1
    |    E';'
    ;
E2    :    E'<'E
    |    E'>'E
    |    E LE E
    |    E GE E
    |    E EQ E
    |    E NE E
    |    E AND E
    |    E OR E
    ;
E    :    ID'='E
    |    E'+'E
    |    E'-'E
    |    E'*'E
    |    E'/'E
    |    E'<'E
    |    E'>'E
    |    E LE E
    |    E GE E
    |    E EQ E
    |    E NE E
    |    E AND E
    |    E OR E
    |    ID
    |    NUM
    ;

%%

#include"lex.yy.c"

main()
{
    printf("\nEnter the expression: ");
    yyparse();
}

Output:

nn@linuxmint ~ $ lex pars.l
nn@linuxmint ~ $ yacc pars.y
conflicts: 5 shift/reduce
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out

Enter the expression: switch(s)
{
case 1:a=b+c;break;
case 2:    if(a<10)
    then a=b*c;
    break;
case 3:    while(a<5)
    b=b+a;
    break;
}

Input accepted.
nn@linuxmint ~ $

Intermediate Code Generator for For Loop - Compiler Design - Yacc Program

Program:

// Lex file: im4.l

alpha [A-Za-z]
digit [0-9]

%%

[\t \n]
for             return FOR;
{digit}+    return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="         return LE;
">="         return GE;
"=="         return EQ;
"!="          return NE;
"||"            return OR;
"&&"         return AND;
.                return yytext[0];

%%

// Yacc file: im4.y

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM FOR LE GE EQ NE OR AND
%right "="
%left OR AND
%left '>' '<' LE GE EQ NE
%left '+' '-'
%left '*' '/'
%right UMINUS
%left '!'

%%

S       : FOR '(' E ';'{lab1();} E {lab2();}';' E {lab3();}')' E';'{lab4(); exit(0);}
         ;
E       : V '='{push();} E{codegen_assign();}
         | E '+'{push();} E{codegen();}
         | E '-'{push();} E{codegen();}
         | E '*'{push();} E{codegen();}
         | E '/'{push();} E{codegen();}
         | '(' E ')'
         | '-'{push();} E{codegen_umin();} %prec UMINUS
         | V
         | NUM{push();}
         ;
V       : ID {push();}
         ;

%%

#include "lex.yy.c"
#include<ctype.h>
char st[100][10];
int label[20];
int top=0;
char i_[2]="0";
char temp[2]="t";

int lno=0,ltop=0;
int start=1;

main()
{
    printf("Enter the expression:\n");
    yyparse();
}

push()
{
   strcpy(st[++top],yytext);
}

codegen()
{
    strcpy(temp,"t");
    strcat(temp,i_);
    printf("%s = %s %s %s\n",temp,st[top-2],st[top-1],st[top]);
    top-=2;
    strcpy(st[top],temp);
    i_[0]++;
}

codegen_umin()
{
    strcpy(temp,"t");
    strcat(temp,i_);
    printf("%s = -%s\n",temp,st[top]);
    top--;
    strcpy(st[top],temp);
    i_[0]++;
}

codegen_assign()
{
    printf("%s = %s\n",st[top-2],st[top]);
    top-=2;
}
lab1()
{
    printf("L%d: \n",lno++);
}
lab2()
{
    strcpy(temp,"t");
    strcat(temp,i_);
    printf("%s = not %s\n",temp,st[top]);
    printf("if %s goto L%d\n",temp,lno);
    i_[0]++;
    label[++ltop]=lno;
    lno++;
    printf("goto L%d\n",lno);
    label[++ltop]=lno;
    printf("L%d: \n",++lno);
 }
lab3()
{
    int x;
    x=label[ltop--];
    printf("goto L%d \n",start);
    printf("L%d: \n",x);
   
}

lab4()
{
    int x;
    x=label[ltop--];
    printf("goto L%d \n",lno);   
    printf("L%d: \n",x);
}

Output:
nn@linuxmint ~ $ lex im4.l
nn@linuxmint ~ $ yacc im4.y
conflicts: 4 shift/reduce
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression:
for(i=0;i=b;i=i+1) a=a+b;
i = 0
L0:
i = b
t0 = not i
if t0 goto L1
goto L2
L3:
t1 = i + 1
i = t1
goto L0
L2:
t2 = a + b
a = t2
goto L3
L1:
nn@linuxmint ~ $

Substring Search & Replacement - UDP - Client Server Program - Network-DBMS Lab - C Program

Program:

// Server : sbs.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>


main ()
{
    struct sockaddr_in client,server;
    int s,sock,found,sz,ack=0,length=0,i,j,bi1=0;
    char buffer[100],substring[20],newstring[20];
    FILE *ptr,*op;
   
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    sz=sizeof(client);
    bind(s,(struct sockaddr *)&server,sizeof(server));
   
   
   
    recvfrom(s,substring,sizeof(substring),0,(struct sockaddr *)&client,&sz);
    printf("\nSubstring received: %s.",substring);
    recvfrom(s,newstring,sizeof(newstring),0,(struct sockaddr *)&client,&sz);
    printf("\nNewstring received: %s",newstring);
   ptr=fopen("testrecord.txt","r");
    op=fopen("result.txt","w");
    while((buffer[bi1]=getc(ptr))!=EOF)
        {
                   found=0;
                   bi1++;
                   if(buffer[bi1-1]==substring[0])
                   {
                       length=1;
                       for(i=1;i<strlen(substring);i++)
                    {
                        if((buffer[bi1]=getc(ptr))==substring[i])
                        {                           
                            length++;
                            bi1++;
                        }
                        else
                {
                    bi1++;
                    break;
                }
                    }
                    if(length==strlen(substring))
                    {
                        found=1;
                        ack=1;
                        printf("\nSubstring found.");
            }
        }
        if(!found)
        {
            fprintf(op,"%s",buffer);
           
        }
        else
        {
            fprintf(op,"%s",newstring);printf("\nString replaced.");
        }
        bi1=0;bzero(buffer,100);
       
    }
   
    sendto(s,&ack,sizeof(int),0,(struct sockaddr *)&client,sz);

  if(ack)
  {
  remove("testrecord.txt");
  rename("result.txt","testrecord.txt");
         printf("\nSaved Successfully.\n");
 }
 remove("result.txt");

 fclose(ptr);
 fclose(op);
 close(s);
}


// Client: sbc.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>

main()
{
    struct sockaddr_in client;
    int s,found=0,choice=0,ack=0;;
    char buffer[20],newstring[20],substring[20];
   
   
    s=socket(AF_INET,SOCK_DGRAM,0);
    client.sin_family=AF_INET;
    client.sin_port=2000;
    client.sin_addr.s_addr=inet_addr("127.0.0.1");
   
   
    printf("Enter the substring: ");
    scanf("%s",substring);
    printf("\nNewstring: ");
       scanf("%s",newstring);
    sendto(s,substring,sizeof(substring),0,(struct sockaddr *)&client,sizeof(client));
   sendto(s,newstring,sizeof(newstring),0,(struct sockaddr *)&client,sizeof(client));

 recvfrom(s,&found,sizeof(int),0,NULL,NULL);
    if(found)
    {
        printf("\nSubstring found & replaced.\n");
    }
    else
    {
            printf("\nNot found.\n");

    }
    close(s);

}




// testrecord.txt

i like www.2k8618.blogspot.com.

Output:

Terminal 1: (Server)
nn@linuxmint ~ $ gcc sbs.c -o server
nn@linuxmint ~ $ ./server

Substring received: 2k8618.blogspot.com.
Newstring received: 2k8cse.cu.cc
Substring found.
String replaced.
Saved Successfully.
nn@linuxmint ~ $

Terminal 2: (Client)
nn@linuxmint ~ $ gcc sbc.c -o client
nn@linuxmint ~ $ ./client
Enter the substring: 2k8618.blogspot.com

Newstring: 2k8cse.cu.cc

Substring found & replaced.
nn@linuxmint ~ $

// testrecord.txt

i like www.2k8cse.cu.cc.

Postfix to Infix - Yacc Program - Compiler Design


Program:-"Beta version"-(Partial Output Only)

// Lex file: ptoi.l

DIGIT [0-9]
%%
{DIGIT}+    {yylval=atoi(yytext);return ID;}
[-+*/]            {return yytext[0];}
.                      ;
\n         yyterminate();

// Yacc file: ptoi.y
%{
    #include<stdio.h>
    #include<string.h>
    void push();
    char* top();
    void a1(char* a);
%}

%token ID

%%

S    : E  { printf("= %s \n",top());}
    ;
E    : E E '+' {a1(" + ");}
    | E E '*' {a1(" * ");}
    | E E '-' {a1(" - ");}
    | E E '/' {a1(" / ");}
    | ID    {push();}
    ;

%%
#include"lex.yy.c"

char st[100][10];
int indx=0;

void push()
{
   strcpy(st[indx++],yytext);
}

char* pop()
{
    return st[--indx];
}

char* top()
{
    return st[indx-1];
}
void a1(char* a)
{
    char buffer[20];
    char* c1=pop();
    char* c2=pop();
    bzero(buffer,20);
    strcat(buffer,c2);
    strcat(buffer,a);
    strcat(buffer,c1);
    strcpy(st[indx++],buffer);
}
main()
{
    yyparse();

}

Output:
nn@linuxmint ~ $ lex ptoi.l
nn@linuxmint ~ $ yacc ptoi2.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
2 5 * 3 2 * +
= 2 * 5 + 3 * 2
nn@linuxmint ~ $ ./a.out
2 5 +
= 2 + 5
nn@linuxmint ~ $ ./a.out
2 3 *
= 2 * 3
nn@linuxmint ~ $ ./a.out
2 3 -
= 2 - 3
nn@linuxmint ~ $ ./a.out
2 5 /
= 2 / 5
nn@linuxmint ~ $

Substring Search & Replacement 2 - Client Server Program - TCP - Network-DBMS Lab - C Program

Program:

// Server: sbstrs.c
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>


main ()
{
    struct sockaddr_in client,server;
    int s,sock,found,sz,ack=0,length=0,i,j,bi1=0;
    char buffer[100],substring[20],newstring[20];
    FILE *ptr,*op;
   
    s=socket(AF_INET,SOCK_STREAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    sz=sizeof(client);
    bind(s,(struct sockaddr *)&server,sizeof(server));
    listen(s,1);
    sock=accept(s,(struct sockaddr *)&client,&sz);
   
    recv(sock,substring,sizeof(substring),0);
    printf("\nSubstring received: %s.",substring);
    recv(sock,newstring,sizeof(newstring),0);
    printf("\nNewstring received: %s",newstring);
   
    ptr=fopen("testrecord.txt","r");
    op=fopen("result.txt","w");
    while((buffer[bi1]=getc(ptr))!=EOF)
        {
                   found=0;
                   bi1++;
                   if(buffer[bi1-1]==substring[0])
                   {
                       length=1;
                       for(i=1;i<strlen(substring);i++)
                    {
                        if((buffer[bi1]=getc(ptr))==substring[i])
                        {                           
                            length++;
                            bi1++;
                        }
                        else
                {
                    bi1++;
                    break;
                }
                    }
                    if(length==strlen(substring))
                    {
                        found=1;
                        ack=1;
                        printf("\nSubstring found.");
            }
        }
        if(!found)
        {
            fprintf(op,"%s",buffer);
           
        }
        else
        {
            fprintf(op,"%s",newstring);printf("\nString replaced.");
        }
        bi1=0;bzero(buffer,100);
       
    }
   
    send(sock,&ack,sizeof(int),0);
    fclose(ptr);
    fclose(op);
    remove("testrecord.txt");
        rename("result.txt","testrecord.txt");
        remove("result.txt");
        printf("\nSaved Successfully.\n");
    close(sock);
    close(s);
}

// Client: sbstrc.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>

main()
{
    struct sockaddr_in client;
    int s,found=0,choice=0,ack=0;;
    char buffer[20],string[20],substring[20];
   
   
    s=socket(AF_INET,SOCK_STREAM,0);
    client.sin_family=AF_INET;
    client.sin_port=2000;
    client.sin_addr.s_addr=inet_addr("127.0.0.1");
    connect(s,(struct sockaddr *)&client,sizeof(client));
   
    printf("Enter a string to search in server:");
    scanf("%s",string);
    send(s,&string,sizeof(string),0);
    recv(s,&found,sizeof(int),0);
    if(found)
    {
        printf("\nThe record found.");
    }
    else
    {
            printf("\nNot found...\n");
            exit(0);
    }
   
    printf("\nDo you want to replace it ?\nEnter 1 -> Yes 2 -> No\n");
    scanf("%d",&choice);
    send(s,&choice,sizeof(int),0);
    if(choice)
    {
        printf("\nSubstring: ");
        scanf("%s",substring);
        send(s,&substring,sizeof(substring),0);
        recv(s,&ack,sizeof(int),0);
        if(ack)
        {
                printf("\nSuccess... ");
        }
        else
        {
                printf("\nError... ");
        }
    }
    else
    {
        printf("\nThanks... ");
            exit(0);
    }
    close(s);

}

// testrecord.txt
    2K8CSE
2K861
2K862
2K863
2K864
2K865
2K866
2K867
2K868
2K869
2K8610
2K8611
2K8612
2K8613
2K8614
2K8615
2K8616
2K8617
2K8618
2K8619
2K8620
2K8621
2K8622
2K8623
2K8624
2K8625
2K8626
2K8627
2K8628
2K8629
2K8630
2K8631
2K8632
2K8633
2K8634
2K8635
2K8636
2K8637
2K8638

Output:

Terminal 1: (server)
nn@linuxmint ~ $ gcc sbstrs.c -o server
nn@linuxmint ~ $ ./server

Substring received: 2K8618.
Newstring received: NIDHEESH
Substring found.
String replaced.
Saved Successfully.
nn@linuxmint ~ $

Terminal 2: (Client)
nn@linuxmint ~ $ gcc sbstrc.c -o client
nn@linuxmint ~ $ ./client
Enter the Substring: 2K8618

Enter the Newstring: NIDHEESH

Substring found & Replaced...
nn@linuxmint ~ $

// testrecord.txt

    2K8CSE
2K861
2K862
2K863
2K864
2K865
2K866
2K867
2K868
2K869
2K8610
2K8611
2K8612
2K8613
2K8614
2K8615
2K8616
2K8617
NIDHEESH
2K8619
2K8620
2K8621
2K8622
2K8623
2K8624
2K8625
2K8626
2K8627
2K8628
2K8629
2K8630
2K8631
2K8632
2K8633
2K8634
2K8635
2K8636
2K8637
2K8638

Substring Search & Replacement 1 - Client Server Program - Network-DBMS Lab - C Program

Program:

// Server: sbstrs1.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>


main ()
{
    struct sockaddr_in client,server;
    int s,sock,found,sz,ack=0,length=0,i,j,bi2=0,bi1=0;
    char buf1[200],buf2[200],substring[20],newstring[20];
    FILE *ptr,*temp;
   
    s=socket(AF_INET,SOCK_STREAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    sz=sizeof(client);
    bind(s,(struct sockaddr *)&server,sizeof(server));
    listen(s,1);
    sock=accept(s,(struct sockaddr *)&client,&sz);
   
    recv(sock,substring,sizeof(substring),0);
    printf("\nSubstring received: %s.",substring);
    recv(sock,newstring,sizeof(newstring),0);
    printf("\nNewstring received: %s.",newstring);
   
    ptr=fopen("testrecord.txt","r");
    while((buf1[bi1]=getc(ptr))!=EOF)
        {
                   found=0;
                   bi1++;
                   if(buf1[bi1-1]==substring[0])
                   {
                       length=1;
                       for(i=1;i<strlen(substring);i++)
                    {
                        if((buf1[bi1]=getc(ptr))==substring[i])
                        {                           
                            length++;
                            bi1++;
                        }
                        else
                {
                    bi1++;
                    break;
                }
                    }
                    if(length==strlen(substring))
                    {
                        found=1;
                        ack=1;
                        printf("\nSubstring found.");
            }
        }
        if(!found)
        {
            for(i=0;i<bi1;i++)
            {
                buf2[bi2]=buf1[i];
                bi2++;
            }
           
        }
        else
        {
            for(i=0;i<=strlen(newstring)-1;i++)
                    {
                        buf2[bi2]=newstring[i];
                bi2++;
            }
            printf("\nReplaced.\n");
        }
        bi1=0;
    }
    send(sock,&ack,sizeof(int),0);
    fclose(ptr);
    temp=fopen("temp.txt","w");
           fprintf(temp,"%s",buf2);
    fclose(temp);
    remove("testrecord.txt");
        rename("temp.txt","testrecord.txt");
        remove("temp.txt");
    close(sock);
    close(s);
}


// Client: sbstrc.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>

main()
{
    struct sockaddr_in client;
    int s,found=0,choice=0,ack=0;;
    char buffer[20],substring[20],newstring[20];
   
   
    s=socket(AF_INET,SOCK_STREAM,0);
    client.sin_family=AF_INET;
    client.sin_port=2000;
    client.sin_addr.s_addr=inet_addr("127.0.0.1");
    connect(s,(struct sockaddr *)&client,sizeof(client));
   
    printf("Enter the Substring: ");
    scanf("%s",substring);
    printf("\nEnter the Newstring: ");
    scanf("%s",newstring);
    send(s,&substring,sizeof(substring),0);

    send(s,&newstring,sizeof(newstring),0);
   
    recv(s,&ack,sizeof(int),0);

    if(ack)
    {
        printf("\nSubstring found & Replaced...\n");
    }
    else
    {
            printf("\nNot found...\n");
    }
    close(s);

}

// testrecord.txt

hi, goodnight...
ok.

Output:

// Terminal 1: (Server)
nn@linuxmint ~ $ gcc sbstrs1.c -o server
nn@linuxmint ~ $ ./server

Substring received: night.
Newstring received: morning.
Substring found.
Replaced.
nn@linuxmint ~ $

// Terminal 2: (Client)
nn@linuxmint ~ $ gcc sbstrc.c -o client
nn@linuxmint ~ $ ./client
Enter the Substring: night

Enter the Newstring: morning

Substring found & Replaced...
nn@linuxmint ~ $


// testrecord.txt
hi, goodmorning...
ok.

String Search and Replacement - Client-Server Program - Network-DBMS Lab - C Program

Program:

// Server : strs.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>


main ()
{
    struct sockaddr_in client,server;
    int s,sock,found,sz,choice=0,ack=0;
    char buffer[20],string[20],substring[20];
    FILE *fp,*temp;
   
    s=socket(AF_INET,SOCK_STREAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    sz=sizeof(client);
    bind(s,(struct sockaddr *)&server,sizeof(server));
    listen(s,1);
    sock=accept(s,(struct sockaddr *)&client,&sz);
    recv(sock,string,sizeof(string),0);
    printf("String recieved: %s",string);
    found=0;
    if((fp=fopen("testrecord.txt","r"))==NULL)
    {
        printf("! The file is empty...\n");
        send(sock,&found,sizeof(int),0);
    }
    else
    {
        while(!feof(fp)&&found==0)
                {
                    fscanf(fp,"%s",buffer);
                    if(strcmp(buffer,string)==0)
                    {
                        found=1;
                printf("\nThe record found.\n");
            }
        }
        send(sock,&found,sizeof(int),0);
        fclose(fp);
    }
   
   
    recv(sock,&choice,sizeof(int),0);
    if(choice)
    {
       
        recv(sock,substring,sizeof(substring),0);
        printf("\nString Replacement:\nSubstring: %s",substring);
        temp=fopen("temp.txt","w");
        if((fp=fopen("testrecord.txt","r"))==NULL)
        {
            printf("! The file is empty...\n");
            send(sock,&ack,sizeof(int),0);
        }
        else
        {
           
            while(!feof(fp))
                {
                    fscanf(fp,"%s",buffer);
                           if(strcmp(buffer,string)==0)
                           {
                               fprintf(temp,"%s ",substring);
                    ack=1;                                               
                }
                else
                {
                    fprintf(temp,"%s ",buffer);
                }
            }
            send(sock,&ack,sizeof(int),0);
        }
        fclose(fp);
        fclose(temp);
        remove("testrecord.txt");
                rename("temp.txt","testrecord.txt");
                remove("temp.txt");
    }
    else
    {
        printf("\nNo replacement... \n");
            exit(0);
    }   
   
    close(sock);
    close(s);
}

// Client: strc.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>

main()
{
    struct sockaddr_in client;
    int s,found=0,choice=0,ack=0;;
    char buffer[20],string[20],substring[20];
   
   
    s=socket(AF_INET,SOCK_STREAM,0);
    client.sin_family=AF_INET;
    client.sin_port=2000;
    client.sin_addr.s_addr=inet_addr("127.0.0.1");
    connect(s,(struct sockaddr *)&client,sizeof(client));
   
    printf("Enter a string to search in server:");
    scanf("%s",string);
    send(s,&string,sizeof(string),0);
    recv(s,&found,sizeof(int),0);
    if(found)
    {
        printf("\nThe record found.");
    }
    else
    {
            printf("\nNot found...\n");
            exit(0);
    }
   
    printf("\nDo you want to replace it ?\nEnter 1 -> Yes 2 -> No\n");
    scanf("%d",&choice);
    send(s,&choice,sizeof(int),0);
    if(choice)
    {
        printf("\nSubstring: ");
        scanf("%s",substring);
        send(s,&substring,sizeof(substring),0);
        recv(s,&ack,sizeof(int),0);
        if(ack)
        {
                printf("\nSuccess... \n");
        }
        else
        {
                printf("\nError... \n");
        }
    }
    else
    {
        printf("\nThanks... ");
            exit(0);
    }
    close(s);

}

// testrecord.txt

BAJAJ YAMAHA HONDA HEROHONDA TVS SUZUKI

Output:

Terminal 1: (Server)
nn@linuxmint ~ $ gcc strs.c -o server
nn@linuxmint ~ $ ./server
String recieved: HEROHONDA
The record found.

String Replacement:
Substring: HEROnn@linuxmint ~ $

Terminal 2: (Client)
nn@linuxmint ~ $ gcc strc.c -o client
nn@linuxmint ~ $ ./client
Enter a string to search in server:HEROHONDA

The record found.
Do you want to replace it ?
Enter 1 -> Yes 2 -> No
1

Substring: HERO

Success... nn@linuxmint ~ $

// testrecord.txt

BAJAJ YAMAHA HONDA HERO TVS SUZUKI

Convert Lowercase to Uppercase & Reverse - Lex Progam - Compiler Design

CHANGE CASE - LEX - PROGRAM


Program:

// Lex file: cap.l


lower [a-z]
CAPS  [A-Z]
space    [ \t\n]

%%
{lower}         {printf("%c",yytext[0]- 32);}
{CAPS}        {printf("%c",yytext[0]+ 32);}
{space}        ECHO;
.                    ECHO;
%%

main()
{
    yylex();
   
}

Output:
nn@linuxmint ~ $ lex cap.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<tst.txt
WWW.2K8618.BLOGSPOT.COM
sanjana    jamsheena chaithanya neethu
GOVINDAPRASAD VIPIN ADARSH SHIVIN
baby brinda kavya helen
SALMAN TINU RICHARD  SIBIN
SHIVIN laji NABEEL
www.2k8cse.cu.cc
nn@linuxmint ~ $

// tst.txt

www.2k8618.blogspot.com
SANJANA    JAMSHEENA CHAITHANYA NEETHU
govindaprasad vipin adarsh shivin
BABY BRINDA KAVYA HELEN
salman tinu richard  sibin
shivin LAJI nabeel
WWW.2K8CSE.CU.CC

Count of Words Starting with a - Lex Program - Compiler Design

Program:

// Lex file: aa.l

%{
        int count=0;
%}
alpha    [a-zA-Z]
digit      [0-9]
space    [ \t\n]
start      ^a
%%

{start}                                           {count++;}
{space}(a|A)({alpha}|{digit})*    {count++;}
.                                                      ;

%%

main()
{
    yylex();
    printf("count= %d\n",count);
}
Output:

nn@linuxmint ~ $ lex aa.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<tst.txt

count= 6
nn@linuxmint ~ $



// tst.txt

afsal ARIFA aaa www.2k8618.blogspot.com
aiswarya saranya    sooraj
arun reshmi
a www.2k8cse.cu.cc

Intermediate Code Generator for Arithmetic Expression II (Advanced)- Yacc Program - Compiler Lab

Program:

// Lex file: imadv.l

ALPHA [A-Za-z]
DIGIT [0-9]

%%

{ALPHA}({ALPHA}|{DIGIT})*        return ID;
{DIGIT}+ {yylval=atoi(yytext);       return NUM;}
[\n\t]                                             yyterminate();
.                                                   return yytext[0];

%%


// Yacc file: imadv.y

%token ID NUM
%right '='
%left '+' '-'
%left '*' '/'
%left UMINUS
%%

S    :    ID{push();} '='{push();} E{codegen_assign();}
      ;
E    :    E '+'{push();} T{codegen();}
      |    E '-'{push();} T{codegen();}
      |    T
      ;
T    :    T '*'{push();} F{codegen();}
      |    T '/'{push();} F{codegen();}
      |    F
      ;
F    :    '(' E ')'
      |    '-'{push();} F{codegen_umin();} %prec UMINUS
      |    ID{push();}
      |    NUM{push();}
      ;

%%

#include "lex.yy.c"
#include<ctype.h>
#include<string.h>
char st[100][25];
int top=0,ptr=0;
int tint=0; int tintar[200];

main()
{
    printf("Enter the expression : ");
    yyparse();
}

push()
{
  strcpy(st[++top],yytext);
  ptr++;
}

codegen()
{
    printf("t%d = %s",tint,st[top-2]);
    printnum(2);
    printf(" %s %s",st[top-1],st[top]);
    printnum(0);
    printf("\n");
    top-=2;ptr-=2;
    strcpy(st[top],"t");
    tintar[ptr]=tint;
    tint++;
}

codegen_umin()
{
    printf("t%d = -%s\n",tint,st[top]);
    printnum(0);
    top--;ptr--;
    strcpy(st[top],"t");
    tintar[ptr]=tint;
    tint++;
}
codegen_assign()
{
    printf("%s = ",st[top-2]);
    printnum(2);
    printf("%s",st[top]);
    printnum(0);
    printf("\n");
    top-=2;ptr-=2;
}
printnum(int n)
{
    if( strcmp(st[top-n],"t")==0)
    {
         printf("%d",tintar[ptr-n]);
    }
}

Output:

nn@linuxmint ~ $ lex imadv.l
nn@linuxmint ~ $ yacc imadv.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression : a=b*c+d*c-c/5+2*8-5+5*5/8-8*9/8+8*7
t0 = b * c
t1 = d * c
t2 = t0 + t1
t3 = c / 5
t4 = t2 - t3
t5 = 2 * 8
t6 = t4 + t5
t7 = t6 - 5
t8 = 5 * 5
t9 = t8 / 8
t10 = t7 + t9
t11 = 8 * 9
t12 = t11 / 8
t13 = t10 - t12
t14 = 8 * 7
t15 = t13 + t14
a = t15
nn@linuxmint ~ $

Parser for FOR Loop Statements - YACC Program - Compiler Design

Program:

// Lex file: for.l

alpha [A-Za-z]
digit [0-9]

%%

[\t \n]
for             return FOR;
{digit}+    return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="         return LE;
">="         return GE;
"=="         return EQ;
"!="          return NE;
"||"            return OR;
"&&"         return AND;
.                return yytext[0];

%%


// Yacc file: for.y

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM FOR LE GE EQ NE OR AND
%right "="
%left OR AND
%left '>' '<' LE GE EQ NE
%left '+' '-'
%left '*' '/'
%right UMINUS
%left '!'

%%
   
S         : ST {printf("Input accepted\n"); exit(0);}
ST       : FOR '(' E ';' E2 ';' E ')' DEF
           ;
DEF    : '{' BODY '}'
           | E';'
           | ST
           |
           ;
BODY  : BODY BODY
           | E ';'       
           | ST
           |            
           ;
       
E        : ID '=' E
          | E '+' E
          | E '-' E
          | E '*' E
          | E '/' E
          | E '<' E
          | E '>' E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | E '+' '+'
          | E '-' '-'
          | ID 
          | NUM
          ;

   
E2     : E'<'E
         | E'>'E
         | E LE E
         | E GE E
         | E EQ E
         | E NE E
         | E OR E
         | E AND E
         ;   
%%

#include "lex.yy.c"
main() {
    printf("Enter the expression:\n");
    yyparse();
}     
     


Output:
nn@linuxmint ~ $ lex for.l
nn@linuxmint ~ $ yacc for.y
conflicts: 25 shift/reduce, 4 reduce/reduce
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression:
for(i=0;i<n;i++)
i=i+1;
Input accepted
nn@linuxmint ~ $

Select Lines Ending with 'com' - Lex Program - Compiler Design

Program:

// Lex file: com.l

%{
int count=0;
%}

%%
.*com\n {count++;ECHO;}
. ;

%%

main()
{
    yylex();
    printf("\nCount= %d\n",count);
    return 0;
   
}

Output:
nn@linuxmint ~ $ lex com.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<com.txt

www.google.com
www.yahoo.com
www.2k8618.blogspot.com




Count= 3
nn@linuxmint ~ $

// com.txt

www.2k8cs.tk
www.google.com
www.yahoo.com
www.2k8618.blogspot.com
www.2k8cse.cu.cc

Convert Decimal Number to Hexadecimal Number in a File - Lex Program - Compiler Design

Program:




// lex file: conv.l

%{
    #include<stdio.h>
    int num,r,digit=0,count,pcount=0,i;
    char a[20];
%}
DIGIT [0-9]

%%

{DIGIT}+ {    num=atoi(yytext);
        while(num!=0)
        {
            r=num%16;
            digit='0'+r;
            if(digit>'9')
            digit+=7;
            a[count++]=digit;
            num=num/16;
        }
        for(i=count-1;i>=pcount;--i)
                printf("%c",a[i]);
                pcount=count;
        }
.|\n    ECHO;
       
%%
main()
{
    yylex();
    return 0;
}      

Output:
nn@linuxmint ~ $ lex conv.l
nn@linuxmint ~ $ gcc lex.yy.c -lfl
nn@linuxmint ~ $ ./a.out<c.txt
DEEPAK A
F HUNAIF
NAVITHA 10
15 RAJINA
ABID 18
1A SANITHA
nn@linuxmint ~ $


// c.txt

DEEPAK 10
15 HUNAIF
NAVITHA 16
21 RAJINA
ABID 24
26 SANITHA

Postfix Expression Evaluation - Compiler Design - YACC Program

Program:

// Lex file: pos.l

DIGIT [0-9]
%%
{DIGIT}+    {yylval=atoi(yytext);return ID;}
[-+*/]        {return yytext[0];}
. ;
\n         yyterminate();

// Yacc File: pos.y

%{
    #include<stdio.h>
    #include<assert.h>
    void push(int val);
%}

%token ID

%%

S     : E  {printf("= %d\n",top());}
      ;
E     : E E '+' {push(pop()+pop());}
     | E E '-' {int temp=pop();push(pop()-temp);}
     | E E '*' {push(pop()*pop());}
     | E E '/' {int temp=pop();push(pop()/temp);}
     | ID    {push(yylval);}
     ;

%%
#include"lex.yy.c"

int st[100];
int i=0;

void push(int val)
{
    assert(i<100);
    st[i++]=val;
   
}

int pop()
{
    assert(i>0);
    return st[--i];

}

int top()
{
    assert(i>0);
    return st[i-1];
}
int main()
{
    yyparse();
    return 0;
}

Output:

nn@linuxmint ~ $ lex pos.l
nn@linuxmint ~ $ yacc pos.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
5 5 -
= 0
nn@linuxmint ~ $

Desk Calculator with Error Recovery - Yacc Program - Compiler Design

Program:

// Lex file: desk.l

DIGIT [0-9]+\.?|[0-9]*\.[0-9]+

%%

[ ]
{DIGIT}    {yylval=atof(yytext);return NUM;}
\n|.             {return yytext[0];}

// Yacc file: desk.y

%{
    #include<ctype.h>
    #include<stdio.h>
    #define YYSTYPE double
%}

%token NUM

%left '+' '-'
%left '*' '/'
%right UMINUS

%%

S         : S E '\n' { printf("Answer: %g \nEnter:\n", $2); }
           | S '\n'
           |
           | error '\n' { yyerror("Error: Enter once more...\n" );yyerrok; }
           ;
E         : E '+' E    { $$ = $1 + $3; }
           | E'-'E    { $$=$1-$3; }
           | E'*'E    { $$=$1*$3; }
           | E'/'E    { $$=$1/$3; }
           | '('E')'    { $$=$2; }
           | '-'E %prec UMINUS { $$= -$2; }
           | NUM       
           ;

%%

#include "lex.yy.c"

int main()
{
    printf("Enter the expression: ");
    yyparse();
}

Output:

nn@linuxmint ~ $ lex desk.l
nn@linuxmint ~ $ yacc desk.y
nn@linuxmint ~ $ gcc y.tab.c -ly -lfl
nn@linuxmint ~ $ ./a.out
Enter the expression: 1.8+2.8
Answer: 4.6
Enter:
1.8*10
Answer: 18
Enter:
20.34/.2
Answer: 101.7
Enter:
101.7*.2
Answer: 20.34
Enter:
20.34-18.31
Answer: 2.03
Enter:
^C
nn@linuxmint ~ $

Stevens W. Richard - UNIX Network Programming




UNIX Network Programming
Stevens W. Richard






Related Posts Plugin for WordPress, Blogger...