Wednesday, May 18, 2022

All Programs

 1 Subset of c


%{

#include<stdio.h>

int line=0;

char choice;

%}

INTEGER    [0-9]+

DELEMETER  [\[\]{}();:]

%%


[ \t\n\r]+;

("/"([^\n\r]*)(\n)) {

 printf("\n Comment is found : %s ",yytext);

  line++;

  if(line > 40)

{

line=0;

printf("\n Press Y To Continue.....");

scanf("%c",choice);

}

}


[+-]?{INTEGER} {

  printf("\n Integer is found : %d ",atoi(yytext));

  line++;

  if(line > 40)

{

line=0;

printf("\n Press Y To Continue.....");

scanf("%c",choice);

}

}

[+-]?{INTEGER}\.{INTEGER}([eE][+-]?[0-9]+)?  {

  printf("\n Floating Number is found : %s ",yytext);

  line++;

  if(line > 40)

{

      line=0;

      printf("\n Press Y To Continue.....");

    scanf("%c",choice);

}

       }


\'.\' {

  printf("\n Charater Literal is found : %s ",yytext);

  line++;

  if(line > 40)

{

line=0;

printf("\n Press Y To Continue.....");

scanf("%c",choice);

}

}


(\"([^\n]*)\") {

printf("\n String is found : %s ",yytext);

line++;

if(line > 40)

   {

   line=0;

     printf("\n Press Y To Continue.....");

   scanf("%c",choice);

   }

}

int |

double |

char |

float |

short |

long |

struct |

union |

void |

return |

if |

else |

for |

do |

case |

switch |

while |

sizeof |

typedef |

signed |

unsigned |

FILE {

printf("\n Keyword is found : %s ",yytext);

line++;

if(line > 40)

  {

  line=0;

 printf("\n Press Y To Continue.....");

  scanf("%c",choice);

  }

}


[a-zA-Z]+[a-zA-Z0-9]* {

printf("\n Variable is found : %s ",yytext);

line++;

if(line > 40)

  {

   line=0;

  printf("\n Press Y To Continue.....");

   scanf("%c",choice);

   }

}

{DELEMETER} {

printf("\n Delemeter is found : %s ",yytext);

line++;

if(line > 40)

    {

    line=0;

   printf("\n Press Y To Continue.....");

    scanf("%c",choice);

    }

}


%%

int main (int argc, char** argv)

{

FILE *file;


if (argc==2)

{

file=fopen(argv[1],"r");


if(!file)

{

printf("\n File Can Not Open ");

exit(1);

}

yyin=file;

yylex();

}

return 0;

}

File for input :

#include<stdio.h>

main()

{

int a,b,c;

printf("Enter the value of a,b,c");

scanf("%d%d%d",&a,&b,&c);

getch();

}


Running commands :

linuxlab1@linuxlab1-Veriton-M200-H81:~$ lex 4subsetc.l

linuxlab1@linuxlab1-Veriton-M200-H81:~$ gcc lex.yy.c -ll

linuxlab1@linuxlab1-Veriton-M200-H81:~$ ./a.out ch.txt

#

 Variable is found : include <

 Variable is found : stdio .

 Variable is found : h >


 Variable is found : main 

 Delemeter is found : ( 

 Delemeter is found : ) 


 Delemeter is found : { 


 Keyword is found : int  

 Variable is found : a ,

 Variable is found : b ,

 Variable is found : c 

 Delemeter is found : ; 


 Variable is found : printf 

 Delemeter is found : ( 

 String is found : "Enter the value of a,b,c" 

 Delemeter is found : ) 

 Delemeter is found : ; 


 Variable is found : scanf 

 Delemeter is found : ( 

 String is found : "%d%d%d" ,&

 Variable is found : a ,&

 Variable is found : b ,&

 Variable is found : c 

 Delemeter is found : ) 

 Delemeter is found : ; 


 Variable is found : getch 

 Delemeter is found : ( 

 Delemeter is found : ) 

 Delemeter is found : ; 


 Delemeter is found : } 

linuxlab1@linuxlab1-Veriton-M200-H81:~$ 


File for input :

#include<stdio.h>

main()

{

int a,b,c;

printf("Enter the value of a,b,c");

scanf("%d%d%d",&a,&b,&c);

getch();

}



2 Replace in string 

%{

        #include<string.h>

        char find[10],*replace[10];

%}       

%%           
[a-zA-z]*       {       if(strcmp(yytext,find)==0)
                        fprintf(yyout,"%s",replace);
                        else
                        fprintf(yyout,"%s",yytext);

                }
%%
main()

{   

        yyin=fopen("x.txt","r");
        yyout=fopen("y.txt","w");
        printf("Enter to find:  ");
        scanf("%s",find);
        printf("Enter string to replace:  ");
        scanf("%s",replace); 
        yylex();

}

Files required

x.txt
Shyam is Student 

y.txt
blank

Commands

linuxlab1@linuxlab1-Veriton-M200-H81:~$ lex 1replace.l
linuxlab1@linuxlab1-Veriton-M200-H81:~$ cc lex.yy.c -ll
1replace.l: In function ‘yylex’:
1replace.l:11:25: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char **’ [-Wformat=]
                         fprintf(yyout,"%s",replace);
                         ^
1replace.l: In function ‘main’:
1replace.l:27:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat=]
         yylex();
         ^
linuxlab1@linuxlab1-Veriton-M200-H81:~$ ./a.out
Enter to find:  is    
Enter string to replace:  was
linuxlab1@linuxlab1-Veriton-M200-H81:~$ 


3 Count word character

%{
#include<string.h>
int wo=0,li=0,ch=0,vw=0;
FILE *fp;
%}
vowel "a"|"e"|"i"|"o"|"u"|"A"|"E"|"I"|"O"|"U"
eo1 [\n]
word [ \t]
%%
{vowel} {vw++;ch++;printf("%s",yytext);}
{word} {wo++;ch+=yyleng;}
{eo1} {li++;wo++;}
.   {
          ch++;printf("%s",yytext);
       }
%%
main(int args,char *argv[])
{
  fp=fopen(argv[1],"r");
if(!fp)
{
printf("cant open");
fclose(fp);
exit(0);
}
else
yyin=fp;
while(!feof(fp))
{
yylex();
}
printf("\n No. of lines = %d \n no.of words = %d \n No. of chars = %d \n No. of vowels = %d \n",li,wo,ch,vw);
fclose(fp);
}


Commands

linuxlab1@linuxlab1-Veriton-M200-H81:~$ lex 3count.l
linuxlab1@linuxlab1-Veriton-M200-H81:~$ gcc lex.yy.c -ll
linuxlab1@linuxlab1-Veriton-M200-H81:~$ ./a.out x.txt
ShyamisStudent
 No. of lines = 1 
 no.of words = 3 
 No. of chars = 16 
 No. of vowels = 4 
linuxlab1@linuxlab1-Veriton-M200-H81:~$ 


4 Parser for sample language using YACC.

%{
#include<math.h>
      #include "y.tab.h"
%}
%%
[0-9]+|[0-9]*\.[0-9]+ { yylval.p = atof(yytext);
return num;
}
sin return SIN ;
cos return COS ;
tan return TAN ;
log return LOG ;
sqrt return SQRT ;
[\t] ;
\n return 0;
. return yytext[0];

%%

commands

//……………………………………..OUTPUT…………………………………………
[softlab@localhost ~]$ yacc -d cal.y
 [softlab@localhost ~]$ lex cal.l
[softlab@localhost ~]$ cc lex.yy.c y.tab.c -ll -lm
[softlab@localhost ~]$ ./a.out
3+5
=8
sin(90)
=0.893997
sqrt(100)
=10


// Program to implement the yacc calculator (cal.y)

%{

            #include<stdio.h>

            #include<math.h>

%}

%union            {          double p;         }

%token <p> num

%token SIN COS TAN LOG SQRT

%left  '+','-'

%left  '*','/'

%nonassoc uminus

%type <p> exp

%%

 

ss:                    exp                              {          printf("=%g\n",$1);}

exp:     exp '+' exp                   {          $$ = $1 + $3;   }

                        |exp '-' exp       {          $$ = $1 - $3;    }

                        |exp '*' exp      {          $$ = $1 * $3;   }

                        |exp '/' exp       {

                                                                                    if($3==0)

                                                                                    {

                                                                                                printf("can't divide");

                                                                                                exit(0);

                                                                                    }

                                                                                    else  $$=$1/$3;

                                                                        }

 

                        |'(' exp ')'                      { $$ = $2; }

                        |SIN '(' exp ')'  { $$ = sin($3); }

                        |COS '(' exp ')' { $$ = cos($3); }

                        |TAN '(' exp ')'             { $$ = tan($3); }

                        |LOG '(' exp ')'             { $$ = log($3); }

                        |SQRT '(' exp ')'           { $$ = sqrt($3);}

                        |num;

%%

 

extern FILE *yyin;

main()

{

            do

            {

                        yyparse();

            }while(!feof(yyin));

}

 

yyerror(s)

char *s;

{

            printf("Error");

}

 

 

// Program to implement the calculator (cal.l)

%{

            #include<math.h>

      #include "y.tab.h"

%}

%%

[0-9]+|[0-9]*\.[0-9]+    {          yylval.p = atof(yytext);

                                                return num;

                                    }

sin                                            return SIN ;

cos                                           return COS ;

tan                                           return TAN ;

log                                           return LOG ;

sqrt                                          return SQRT ;

[\t]                               ;

\n                                             return 0;

.                                               return yytext[0];

 

%%

 

 

 

//……………………………………..OUTPUT…………………………………………

[softlab@localhost ~]$ yacc -d cal.y

 [softlab@localhost ~]$ lex cal.l

[softlab@localhost ~]$ cc lex.yy.c y.tab.c -ll -lm

[softlab@localhost ~]$ ./a.out

3+5

=8

sin(90)

=0.893997

sqrt(100)

=10

6

%{
//#include "parsing_lab.h"
#include "y.tab.h"
extern int yylval;
 
%}

%%



[0-9]+ { yylval=(int)(yytext);return NUMBER;}
   /*cast pointer to int for compiler warning*/
[\t\n]+   ;
"+"  return(PLUS);
"-"  return(MINUS);
"*"  return(TIMES);
"/"  return(DIVIDE);
"^"  return(POWER);
"("   return(LEFT_PARENTHESIS);
")"   return(RIGHT_PARENTHESIS);
";"   return(END);

%%

int yywrap(void) {return 1;}

Commands

linuxlab15@linuxlab15-p2-1401il:~$ yacc -d parser.y
linuxlab15@linuxlab15-p2-1401il:~$ lex lexer.l
linuxlab15@linuxlab15-p2-1401il:~$ gcc y.tab.c lex.yy.c
lexer.l: In function ‘yylex’:
lexer.l:12:10: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
 [0-9]+ { yylval=(int)(yytext);return NUMBER;}
          ^
linuxlab15@linuxlab15-p2-1401il:~$ ./a.out
(1+3*7-1^5);
( ^ ( - ( +  1 ( *  3  7 )) 1 ) 5 )

PUSH 1 
PUSH 3 
PUSH 7 
POP A
POP B
MULT A=A*B
PUSH A
POP A
POP B
ADD A=A+B
PUSH A
PUSH 1 
POP A
POP B
SUB A=A-B
PUSH A
PUSH 5 
POP A
POP B
POWER A=A^B
PUSH A

7

/*-------------------------------------------------------------

AIM: To implement ICG for arithematic expression

---------------------------------------------------------------

Name: Vaibhav Ghatol

Roll No: B4136

Class: BE comp A

---------------------------------------------------------------*/

/***********LEX FILE*************/

%{

     #include<ctype.h>

     #include<stdio.h>

     #include "y.tab.h"

%}

 

Digit [0-9]+

Letter [a-zA-Z]+

%%   //end of declaration section

 

 

{Digit}    {yylval.p=(char)yytext[0];return Digit;}

{Letter}   {yylval.p=(char)yytext[0];return Letter;}

.          {return yytext[0];}

\n         {return 0;}

%%   //end of rules section

 

 

/***********YACC FILE*************/

%{

     #include<stdio.h>

     #include<string.h>

 

     char addintotable(char,char,char);

     int search(char);

 

     struct icode

     {

           char op1;

           char op2;

           char op;

           char res;

     }code[20];

  

      int index1=0;

%}

 

%union

{

     char p;

}

  

%token <p> Letter Digit

%type <p> expr

 

%left '-' '+'

%left '*' '/'

%%   //end of declaration section

 

/* ---------------------------------------------------------------- */

 

 stat:Letter '=' expr ';' {addintotable((char)$1,(char)$3,'=');}

      |expr ';'

      ;

 expr:expr '+' expr {$$=addintotable((char)$1,(char)$3,'+');}

     |expr '-' expr {$$=addintotable((char)$1,(char)$3,'-');}

     |expr '*' expr {$$=addintotable((char)$1,(char)$3,'*');}

     |expr '/' expr {$$=addintotable((char)$1,(char)$3,'/');}

     |'('expr')' {$$=(char)$2;}

     |Digit {$$=(char)$1;}

     |Letter {$$=(char)$1;}

     ;

 %%  //end of rules section

 /* --------------------------------------------------------------- */

 

int yywrap()

{

     return 1;

}

 

yyerror(char *s)

{

     printf("hey vaibhav!! error %s",s);

}

 

 

 

 /* ---------------------------------------------------------------- */

 

char temp='A';

 

 /* ---------------------------------------------------------------- */

 

char addintotable(char op1,char op2,char op)

{

     temp++;

     code[index1].op1=op1;

     code[index1].op2=op2;

     code[index1].op=op;

     code[index1].res=temp;

     index1++;

 

     return temp;

}

 

 /* ---------------------------------------------------------------- */

 

void threeaddress()

{

     int cnt=0;

     char temp='A';

     temp++;

     printf("\nThe three address code is:\n");

     printf("");

     while(cnt<index1)

     {

           printf("%c:=\t",temp);

           printf("%c\t",code[cnt].op1);      

           printf("%c\t",code[cnt].op);

           printf("%c\t",code[cnt].op2);

           temp++;

           cnt++;

           printf("\n");

     }

}  

 /* ---------------------------------------------------------------- */

void quadruple()

 

{

     int cnt=0;

     char temp='A';

     temp++;

     printf("\nThe Quadruple format is:\n");

     printf("Operator\tArg1\tArg2\tResult\n");

     while(cnt<index1)

     {

           printf("%c\t\t",code[cnt].op);

           printf("%c\t",code[cnt].op1);      

           printf("%c\t",code[cnt].op2);

           printf("%c\t",code[cnt].res);

           temp++;

           cnt++;

           printf("\n");

     }

}  

 

 /* ---------------------------------------------------------------- */

 

void triple()

{

     int cnt=0;

     int flag;

     char temp='A';

     temp++;

     printf("\nThe Triple format is:\n");

     printf("Arg1\tArg2\toperator\n");

     while(cnt<index1)

     {

           if(isalpha(code[cnt].op1) && isupper(code[cnt].op1))

           {

                flag= search(code[cnt].op1);

                printf("%d\t",flag);

           }

 

           else if(isalpha(code[cnt].op1))

                printf("%c\t",code[cnt].op1);

 

           else

                printf("%c\t",temp);

 

           if(isalpha(code[cnt].op2) && isupper(code[cnt].op2))

           {

                flag= search(code[cnt].op2);

                printf("%d\t",flag);

           }

 

           else if(isalpha(code[cnt].op2))

                printf("%c\t",code[cnt].op2);

 

           else

                printf("%c\t",temp);

                printf("%c\t",code[cnt].op);

                printf("\n");

                cnt++;

     }

}      

/* ----------------------------------------------------------------- */

int search(char find)

{

     int i;

 

     for(i=0;i<index1;i++)

     {

           if(code[i].res==find)

           return(i);

     }

 

     return 0;

}

 /* ---------------------------------------------------------------- */

void main()

{

     yyparse();

     threeaddress();

     quadruple();

     triple();

}

 

 

 

/*************output******************/

vaibhav@ubuntu:~$ lex ic.l

vaibhav@ubuntu:~$ yacc -d ic.y

vaibhav@ubuntu:~$ cc lex.yy.c y.tab.c -ll -lm -ly

vaibhav@ubuntu:~$ ./a.out

a=b+c-d*e;

 

The three address code is:

B:=  b    +    c   

C:=  d    *    e   

D:=  B    -    C   

E:=  a    =    D   

 

The Quadruple format is:

Operator   Arg1 Arg2 Result

+          b    c    B   

*          d    e    C   

-          B    C    D   

=          a    D    E   

 

The Triple format is:

Arg1 Arg2 operator

b    c    +   

d    e    *   

0    1    -   

a    2    =   

vaibhav@ubuntu:~$

 

 8 code generation dag


#include<stdio.h>

#include<stdlib.h>

#include<string.h>


int label[20];

int no=0;

int main()

{

FILE *fp1,*fp2;

int check_label(int n);

char fname[10],op[10],ch;

char op1[8]="",op2[8]="",result[8]="";

int i=0,j=0;


//clrscr();


printf("\n\tEnter File Name of The Intermediate Code: ");

scanf("%s",&fname);


fp1=fopen(fname,"r");

fp2=fopen("d:\\output.txt","w");

if(fp1==NULL||fp2==NULL)

{

printf("\n\tEroor To Opening File...");

//getch();

exit(0);

}


while(!feof(fp1))

{

fprintf(fp2,"\n");

fscanf(fp1,"%s",op);

i++;

if(check_label(i))

{

fprintf(fp2,"\nlabel#%d: ",i);

}

if(strcmp(op,"print")==0)

{

fscanf(fp1,"%s",result);

fprintf(fp2,"\n\t OUT %s",result);

}

if(strcmp(op,"goto")==0)

{

fscanf(fp1,"%s %s ",op1,op2);

fprintf(fp2,"\n\t JMP %s,label#%s",op1,op2);

label[no++]=atoi(op2);

}

if(strcmp(op,"[]=")==0)

{

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\tSTORE%s[%s],%s",op1,op2,result);

}

if(strcmp(op,"uminus")==0)

{

fscanf(fp1,"%s %s",op1,result);

fprintf(fp2,"\n\t LOAD -%s,R1",op1);

fprintf(fp2,"\n\t STORE R1,%s",result);

}

if(strcmp(op,"==")==0)

{

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\t LOAD %s,R0",op1);

fprintf(fp2,"\n\t JEQ %s,label#%s",op2,result);

label[no++]=atoi(result);

}

if(strcmp(op,"<=")==0)

{

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\t LOAD %s,R0",op1);

fprintf(fp2,"\n\t JLTEQ %s,label#%s",op2,result);

label[no++]=atoi(result);

}


switch(op[0])

{

case '*':

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\t LOAD %s,R0",op1);

fprintf(fp2,"\n\t LOAD %s,R1",op2);

fprintf(fp2,"\n\t MUL R1,R0");

fprintf(fp2,"\n\t STORE R0,%s",result);

break;


case '+':

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\t LOAD %s,R0",op1);

fprintf(fp2,"\n\t LOAD %s,R1",op2);

fprintf(fp2,"\n\t ADD R1,R0");

fprintf(fp2,"\n\t STORE R0,%s",result);

break;


case '-':

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\t LOAD %s,R0",op1);

fprintf(fp2,"\n\t LOAD %s,R1",op2);

fprintf(fp2,"\n\t SUB R1,R0");

fprintf(fp2,"\n\t STORE R0,%s",result);

break;


case '/':

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\t LOAD %s,R0",op1);

fprintf(fp2,"\n\t LOAD %s,R1",op2);

fprintf(fp2,"\n\t DIV R1,R0");

fprintf(fp2,"\n\t STORE R0,%s",result);

break;


case '%':

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\t LOAD %s,R0",op1);

fprintf(fp2,"\n\t LOAD %s,R1",op2);

fprintf(fp2,"\n\t DIV R1,R0");

fprintf(fp2,"\n\t STORE R0,%s",result);

break;


case '=':

fscanf(fp1,"%s %s",op1,result);

fprintf(fp2,"\n\t STORE %s,%s",op1,result);

break;


case '>':

j++;

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\t LOAD %s,R0",op1);

fprintf(fp2,"\n\t JGT %s,label#%s",op2,result);

label[no++]=atoi(result);

break;


case '<':

fscanf(fp1,"%s %s %s",op1,op2,result);

fprintf(fp2,"\n\t LOAD %s,R0",op1);

fprintf(fp2,"\n\t JLT %s,label#%s",op2,result);

label[no++]=atoi(result);

}//end case

}//end while


fclose(fp2);

fclose(fp1);

fp2=fopen("d:\\output.txt","r");

if(fp2==NULL)

{

printf("\n\tEroor To Opening File...");

//getch();

exit(0);

}

do

{

ch=fgetc(fp2);

printf("%c",ch);

}while(ch!=EOF);

fclose(fp2);

//getch();

return 0;

}


int check_label(int k)

{

int i;

for(i=0;i<no;i++)

{

if(k==label[i])

return 1;

}

return 0;

}


files 

interip.txt

< a b 2

+ a b t1

= t1 a 

goto t1 5

< a b 6

+ a b t3

= t3 a

goto t3 5

< a b 10

- a b t4

= t4 c



commands

linuxlab1@linuxlab1-Veriton-M200-H81:~$ gcc target.c
target.c: In function ‘main’:
target.c:19:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat=]
 scanf("%s",&fname);
 ^
linuxlab1@linuxlab1-Veriton-M200-H81:~$ ./a.out 

Enter File Name of The Intermediate Code: interip.txt


LOAD a,R0
JLT b,label#2

label#2: 
LOAD a,R0
LOAD b,R1
ADD R1,R0
STORE R0,t1

STORE t1,a

JMP t1,label#5

label#5: 
LOAD a,R0
JLT b,label#6

label#6: 
LOAD a,R0
LOAD b,R1
ADD R1,R0
STORE R0,t3

STORE t3,a

JMP t3,label#5

LOAD a,R0
JLT b,label#10

label#10: 
LOAD a,R0
LOAD b,R1
SUB R1,R0
STORE R0,t4

STORE t4,c

STORE t4,c