The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Wednesday, November 28, 2012

Design and Implement a Calculator using ‘YACC’ and ‘LEX’


Calc.y

%{
#include <stdlib.h>
#include <stdio.h>
int yylex(void);
#include "y.tab.h"
%}

%token INTEGER

%%
program:
line program
| line

line:
expr '\n' { printf("%d\n",$1); }
| 'n'

expr:
expr '+' mulex { $$ = $1 + $3; }
| expr '-' mulex { $$ = $1 - $3; }
| mulex { $$ = $1; }

mulex:
mulex '*' term { $$ = $1 * $3; }
| mulex '/' term { $$ = $1 / $3; }
| term { $$ = $1; }

term:
'(' expr ')' { $$ = $2; }
| INTEGER { $$ = $1; }

%%
void yyerror(char *s)
{
fprintf(stderr,"%s\n",s);
return;
}
yywrap()
{
  return(1);
}

int main(void)
{
/*yydebug=1;*/
yyparse();
return 0;
}

Lexx.l

%{
#include <stdlib.h>
#include <stdio.h>
#include "y.tab.h"
void yyerror(char*);
extern int yylval;
%}


%%
[ \t]+ ;
[0-9]+ {yylval = atoi(yytext);
return INTEGER;}
[-+*/] {return *yytext;}
"(" {return *yytext;}
")" {return *yytext;}
\n {return *yytext;}
. {char msg[25];
sprintf(msg,"%s <%s>","invalid character",yytext);
yyerror(msg);}


Output :-

[S@localhost Desktop]$ flex lexx.l
[S@localhost Desktop]$ yacc -d calc.y
[S@localhost Desktop]$ gcc y.tab.c lex.yy.c
[S@localhost Desktop]$ ./a.out

5+6
11
8+9-7
10
6++9
syntax error
[S@localhost Desktop]$

Monday, November 26, 2012

Generate Three Address Code For Selected Statements


CODING :-

#include<stdio.h>
#include<string.h>

main()
{      
char str[10];
int i,j;
char op;
  printf(“\nEnter the expression:”);
scanf("%s",str);
op=str[0];
i=0;

while((op!='0')||(op!='+')||(op!='-')||(op!=’*’)||(op!=’/’))
{
op=str[++i];
}

switch(op)
{
case '+':printf("MOV R2,%c\n",str[i-1]);
printf("MOV R3,%c\n",str[i+1]);
printf("ADD R1,R2,R3\n");
printf("STORE R1,%c\n",str[i-3]);
break;
case '-':printf("MOV R2,%c\n",str[i-1]);
printf("MOV R3,%c\n",str[i+1]);
printf("SUB R1,R2,R3\n");
printf("STORE R1,%c\n",str[i-3]);
break;
                case '*':printf("MOV R2,%c\n",str[i-1]);
                         printf("MOV R3,%c\n",str[i+1]);
                         printf("MUL R1,R2,R3\n");
                         printf("STORE R1,%c\n",str[i-3]);
                         break;
                case '/':printf("MOV R2,%c\n",str[i-1]);
                         printf("MOV R3,%c\n",str[i+1]);
                         printf("DIV R1,R2,R3\n");
                         printf("STORE R1,%c\n",str[i-3]);
                         break;
default:printf("ERROR\n");
                break;
}
}

Output :-

Enter the expression: x=a+b

MOV R2,a
MOV R3,b
ADD R1,R2,R3
STORE R1,x


Saturday, November 24, 2012

To Generate A Parser For a Subset of ‘C’ Language Using ‘YACC’


CODING :-

%{
#include <stdio.h>
int regs[26];
int base;
%}

%start list

%token DIGIT LETTER

%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS  /*supplies precedence for unary minus */

%%                   /* beginning of rules section */

list:                       /*empty */
         |
        list stat '\n'
         |
        list error '\n'
         {
           yyerror();
         }
         ;

stat:    expr
         {
           printf("%d\n",$1);
         }
         |
         LETTER '=' expr
         {
           regs[$1] = $3;
         }
         ;

expr:    '(' expr ')'
         {
           $$ = $2;
         }
         |
         expr '*' expr
         {
           $$ = $1 * $3;
         }
         |
         expr '/' expr
         {
           $$ = $1 / $3;
         }
         |
         expr '%' expr
         {

           $$ = $1 % $3;
         }
         |
         expr '+' expr
         {
           $$ = $1 + $3;
         }
|
         expr '-' expr
         {
           $$ = $1 - $3;
         }
         |
         expr '&' expr
         {
           $$ = $1 & $3;
         }
         |
         expr '|' expr
         {
           $$ = $1 | $3;
         }
         |

        '-' expr %prec UMINUS
         {
           $$ = -$2;
         }
         |
         LETTER
         {
           $$ = regs[$1];
         }

         |
         number
         ;

number:  DIGIT
         {
           $$ = $1;
           base = ($1==0) ? 8 : 10;
         }       |
         number DIGIT
         {
           $$ = base * $1 + $2;
         }
         ;
%%
main()
{
 return(yyparse());
}

yyerror(s)
char *s;
{
  fprintf(stderr, "%s\n",s);
}

yywrap()
{
  return(1);
}

Thursday, November 22, 2012

Create a 2-Pass 8085 Assembler


CODING :-

#include<stdio.h>
#include<conio.h>

void gethex(int v,char*str)
{ str[3]=(v%16>9)?((v%16-10)+'A'):(v%16+'0');
  v=v/16;
  str[2]=(v%16>9)?((v%16-10)+'A'):(v%16+'0');
  v=v/16;
  str[1]=(v%16>9)?((v%16-10)+'A'):(v%16+'0');
  v=v/16;
  str[0]=(v%16>9)?((v%16-10)+'A'):(v%16+'0');
  str[4]='\0';
}

int main()
{
  char codes[100][2][100];
  int csize[100];
  int opos[100];
  int codemem[100];
  int tot=0,num=0,l=0;
  char str[100]="",ch;
  char*ptr=NULL;
  int i=0,j=0,k=0,fpos=0;

  for(i=1;i<=5;i++)
  for(j=1;j<=5;j++)
  if(i!=j)
  { strcpy(str,"MOV ");
    str[4]='A'+i-1;
    str[5]=',';
    str[6]='A'+j-1;
    str[7]='\0';
    strcpy(codes[tot][0],str);
    str[0]='A'+i-1;
    str[1]='A'+j-1;
    str[2]='\0';
    strcpy(codes[tot][1],str);
    csize[tot]=1;
    opos[tot]=0;
    tot++;
  }

  for(i=1;i<=5;i++)
  { strcpy(str,"ADD ");
    str[4]='A'+i-1;
    str[5]='\0';
    strcpy(codes[tot][0],str);
    str[0]='1';
    str[1]='A'+i-1;
    str[2]='\0';
    strcpy(codes[tot][1],str);
    csize[tot]=1;
    opos[tot]=0;
    tot++;
  }




  for(i=1;i<=5;i++)
  { strcpy(str,"LOAD ");
    str[5]='A'+i-1;
    str[6]='\0';
    strcpy(codes[tot][0],str);
    str[0]='2';
    str[1]='A'+i-1;
    //str[4]='H';
    str[2]='\0';
    strcpy(codes[tot][1],str);
    csize[tot]=2;
    opos[tot]=7;
    tot++;
  }  

  for(i=1;i<=5;i++)
  { strcpy(str,"SUB ");
    str[4]='A'+i-1;
    str[5]='\0';
    strcpy(codes[tot][0],str);
    //str[0]='0';
    //str[1]='4';
    str[0]='3';
    str[1]='A'+i-1;
    //str[4]='H';
    str[2]='\0';
    strcpy(codes[tot][1],str);
    csize[tot]=1;
    opos[tot]=0;
    tot++;
  }

  int jump=tot;
  strcpy(str,"JMP ");
  strcpy(codes[tot][0],str);
  strcpy(str,"06");
  strcpy(codes[tot][1],str);
  csize[tot]=3;
  opos[tot]=4;
  tot++;

  strcpy(str,"JNZ ");
  strcpy(codes[tot][0],str);
  strcpy(str,"07");
  strcpy(codes[tot][1],str);
  csize[tot]=3;
  opos[tot]=4;
  tot++;

  strcpy(str,"JZ ");
  strcpy(codes[tot][0],str);
  strcpy(str,"08");
  strcpy(codes[tot][1],str);
  csize[tot]=3;
  opos[tot]=3;
  tot++;

  strcpy(str,"JNC ");
  strcpy(codes[tot][0],str);
  strcpy(str,"09");
  strcpy(codes[tot][1],str);


  csize[tot]=3;
  opos[tot]=4;
  tot++;

  strcpy(str,"JC ");
  strcpy(codes[tot][0],str);
  strcpy(str,"0A");
  strcpy(codes[tot][1],str);
  csize[tot]=3;
  opos[tot]=3;
  tot++;
 
  char code[100][100];
  printf("\nTo end the code enter END\n");
  int size=0;
  while(1)
  { gets(code[size]);
    if(strcmp(code[size],"END")==0)
    break;
    size++;
  }

  char symb[100][2][100];
  int symbsize=0;
  int mempos=8192;

  for(i=0;i<size;i++)
  { int type=-1;
    int islabel=-1;
    for(j=0;code[i][j]!='\0';j++)
    if(code[i][j]==':')
    { islabel=j;
      break;
    }
    if(islabel!=-1)
    {
      for(j=0;j<islabel;j++)
      str[j]=code[i][j];
      str[j]='\0';
      strcpy(symb[symbsize][0],str);
      sprintf(str,"%d",mempos);
      strcpy(symb[symbsize][1],str);
      symbsize++;
    }
    else
    { for(j=0;j<tot;j++)
      { ptr=strstr(code[i],codes[j][0]);
        if(ptr!=NULL)
        { type=j;
         
          break;
        }
      }
      if(type==-1)
      { printf("\nIllegal Code\n");
        getch();
        exit(0);
      }
      mempos+=csize[type];
    }      
  }  



  mempos=8192;
  char output[100][100];
  int curs=0;

  for(i=0;i<size;i++)
  { int type=-1,islabel=-1;
    for(j=0;code[i][j]!='\0';j++)
    if(code[i][j]==':')
    { islabel=j;
      break; }
    if(islabel==-1)
    {
      for(j=0;j<tot;j++)
      { ptr=strstr(code[i],codes[j][0]);
        if(ptr!=NULL)
        { type=j;
          if(opos[j]==0)
          { strcpy(output[curs],codes[j][1]);
            codemem[curs]=mempos;
            mempos+=csize[j];
            curs++;
          }
          else if(opos[j]==7)
          { strcpy(output[curs],codes[j][1]);
            strcat(output[curs]," ");
            str[0]=code[i][opos[j]];
            str[1]=code[i][opos[j]+1];
            str[2]='\0';
            strcat(output[curs],str);
            codemem[curs]=mempos;
            mempos+=csize[j];
           
            curs++;
          }  
          else if(j>=jump)
          { strcpy(output[curs],codes[j][1]);
            strcat(output[curs]," ");
            for(k=opos[j];code[i][k]!='\0';k++)
            str[k-opos[j]]=code[i][k];
            str[k-opos[j]]='\0';
            fpos=-1;
            for(k=0;k<symbsize;k++)
            if(strcmp(symb[k][0],str)==0)
            { fpos=k;
              break;
            }
            if(fpos==-1)
            { printf("\nLabel named %s not defined\n",str);
              getch();
              exit(0);
            }
            num=0;
            for(l=0;l<4;l++)
            num=num*10+symb[k][1][l]-'0';
            gethex(num,str);
            ch=str[0];
            str[0]=str[2];
            str[2]=ch;
            ch=str[1];
            str[1]=str[3];
            str[3]=ch;
            strcat(output[curs],str);



            codemem[curs]=mempos;
            mempos+=csize[j];
            curs++;
          }
          break;  
        }
      }
    }
  }

  printf("\nMachine Code is\nMemory  Opcode+Operand\n");
  for(i=0;i<curs;i++)
  { gethex(codemem[i],str);
    printf("%s        %s\n",str,output[i]);
  }
  getch();
}


Input File:-

MOV A,B
MOV C,D
LOAD E,10H
MOV A,E
TEST:
ADD C
SUB D
JMP TEST
RUN:
ADD B
JC RUN
MOV D,E
JNC RUN
SUB A
JZ TEST
JNZ RUN
END

OUTPUT SCREEN:-

Machine Code is
Memory  Opcode+Operand
2000        AB
2001        CD
2002        2E 10
2004        AE
2005        1C
2006        3D
2007        06 0520
200A        1B
200B        0A 0A20
200E        DE
200F        09 0A20
2012        3A
2013        08 0520
2016        07 0A20


Tuesday, November 20, 2012

Create a Lexical Analyzer for a Subset of ‘C’ Language


CODING :-

%{                                                            
/* need this for the call to atof() below */
#include <math.h>
%}

DELIM  [ \t\n]
WHITESPACE {DELIM}+
DIGIT     [0-9]
LETTER  [a-zA-Z]
ID        {LETTER}({LETTER}|{DIGIT})*
NUMBER  {DIGIT}+(\.{DIGIT}+)?(e[+\-]?{DIGIT}+)?


%%
{DIGIT}+     {
             printf( "An integer: %s (%d)\n", yytext,
                     atoi( yytext ) );
             }
{DIGIT}+"."{DIGIT}*         {
             printf( "A float: %s (%g)\n", yytext,
                     atof( yytext ) );
             }
if|else|return|main|include|int|float|char         {
             printf( "A keyword: %s\n", yytext );
             }
{ID}         printf( "An identifier: %s\n", yytext );
"+"|"-"|";"|"="|"("|")"|"{"|"}"|"<"|">"|"*"|"/"    printf( "An operator: %s\n", yytext );

"{"[^}\n]*"}"      /* eat up one-line comments */
[ \t\n]+           /* eat up whitespace */
.            printf( "Unrecognized character: %s\n", yytext );
%%


main( argc, argv )
int argc;
char **argv;
    {
    ++argv, --argc; /* skip over program name */
    if ( argc > 0 )
             yyin = fopen( argv[0], "r" );
    else
             yyin = stdin;
    yylex();
    }

File.c

main()
{
int a1, a2;
float f1=9.09;
a1=10;
if(a1>10)
a2=a1;
}



OUTPUT SCREEN:-

[S@localhost ~]$ flex lex.l
[S@localhost ~]$ gcc -c lex.yy.c
[S@localhost ~]$ gcc -o final lex.yy.o -lfl
[S@localhost ~]$ ./final file.c

A keyword: main
An operator: (
An operator: )
An operator: {
A keyword: int
An identifier: a1
Unrecognized character: ,
An identifier: a2
An operator: ;
A keyword: float
An identifier: f1
An operator: =
A float: 9.09 (9.09)
An operator: ;
An identifier: a1
An operator: =
An integer: 10 (10)
An operator: ;
A keyword: if
An operator: (
An identifier: a1
An operator: >
An integer: 10 (10)
An operator: )
An identifier: a2
An operator: =
An identifier: a1
An operator: ;
An operator: }
[S@localhost ~]$

Wednesday, November 14, 2012

Implement Ceaser Substitution Cipher encryption and decryption technique

#include<stdio.h>
#include<conio.h>
#include<string.h>

char mssg[100];
int k;
 
void get_mssg()
{
   printf("\n Enter the Plaintext (in caps): ");
   gets(mssg);
   printf("\n Enter the key (less than 26): ");
   scanf("%d",&k);
}

void en_mssg()
{
  int i = 0;
  while(mssg[i] != '\0')
  { 
    if(mssg[i] >= 65 && mssg[i] <= 90)          
     mssg[i] = ((mssg[i] - 65 + k) % 26)+65;
    i++;

  } 
  printf("\n The Ciphertext is : ");
  puts(mssg);
}

void de_mssg()
{
  int i = 0;
  while(mssg[i] != '\0')
  { 
    if(mssg[i] >= 65 && mssg[i] <= 90)    
    {    
      if((mssg[i]-65) >= k)
       mssg[i] = mssg[i] - k;
      else
       mssg[i] = 90-(k-(mssg[i]-65))+1;
    }     
    i++;
  } 
  printf("\n The Decrypted Plaintext is : ");
  puts(mssg);
}


int main()
{
  get_mssg();  en_mssg();  de_mssg();
  return 0;
}

Tuesday, November 13, 2012

Implement Hill Substitution Cipher encryption and decryption technique

#include<stdio.h>
#include<conio.h>
#include<string.h>

char mssg[100]; 
int k[][3] = { {17,17,5},
               {21,18,21},
               {2,2,19}
             }; 
         
int ki[][3] = { {4,9,15},
                {15,17,6},
                {24,0,17}
             }; 

int c[3];
int p[3];            
 
void get_mssg()
{
   printf("\n Enter the Plaintext (in caps): ");
   gets(mssg);
}

void multiply()
{
  for(int i=0;i<3;i++)
  {
   p[i] = 0;     
   for(int j=0;j<3;j++)
     p[i] += c[j]*k[i][j];
   p[i] %= 26;            
  }      
}

void multiply2()
{
  for(int i=0;i<3;i++)
  {
   c[i] = 0;     
   for(int j=0;j<3;j++)
     c[i] += p[j]*ki[i][j];
   c[i] %= 26;            
  }      
}

void en_mssg()
{
  int i = 0;
  while(i < strlen(mssg))
  {  
    c[0] = mssg[i]-65;
    if(mssg[i+1] == '\0')
      c[1] = c[2] = 0;
    else
    {
      c[1] = mssg[i+1]-65; 
      if(mssg[i+2] == '\0')
       c[2] = 0;
      else
       c[2] = mssg[i+2]-65;
    }
  
   multiply();



   int l =0;
   while(i<strlen(mssg) && l<3)
    mssg[i++] = p[l++]+65;         
   
  }  
  printf("\n The Ciphertext is : ");
  puts(mssg);
}

void de_mssg()
{
  int i = 0;
  while(i < strlen(mssg))
  {  
    p[0] = mssg[i]-65;
    if(mssg[i+1] == '\0')
      p[1] = p[2] = 0;
    else
    {
      p[1] = mssg[i+1]-65; 
      if(mssg[i+2] == '\0')
       p[2] = 0;
      else
       p[2] = mssg[i+2]-65;
    }
  
   multiply2();
   int l =0;
   while(i<strlen(mssg) && l<3)
    mssg[i++] = c[l++]+65;         
  } 
  printf("\n The Decrypted Plaintext is : ");
  puts(mssg);
}

int main()
{
  get_mssg();
  en_mssg();
  de_mssg(); 
  getch();
  return 0; 
}