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();
}
// 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
/*-------------------------------------------------------------
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:~$
#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