Professional Documents
Culture Documents
AIM
To write a C program to implement a symbol table.
ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<alloc.h>
#include<string.h>
#include<math.h>
void main()
{
int i=0,j=0,x=0,n,flag=0;
void *p,*add[5];
char ch,srch,b[15],d[15],c;
clrscr();
printf("Expression terminated by $:");
while((c==getchar())!='$')
{
b[i]=c;
i++;
}
n=i-1;
printf("Given Expression :");
1
i=0;
while(i<=n)
{
printf("%c",b[i]);
i++;
}
printf("\n Symbol Table\n");
printf("Symbol\taddr\ttype");
while(i<=n)
{
c=b[j];
if(isalpha(toascii(c)))
{
if(j==n)
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("%c\t%d\tidentifier",c,p);
}
else
{
ch=b[j+1];
if(ch=='+' || ch=='-' || ch=='*' || ch=='=')
{
p=malloc(c);
add[x]=p;
d[x]=c;
printf("\n%c\t%d\tidentifier\n",c,p);
x++;
}}}
j++;
}
printf("\nThe symbol is to be searched");
srch=getch();
for(i=0;i<=x;i++)
{
if(srch==d[i])
{
printf("\nSymbol Found");
2
printf("\n%c%s%d\n",srch,"@address",add[i]);
flag=1;
}
}
if(flag==0)
printf("\nSymbol not found");
getch();
}
OUTPUT
Input Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=10;
b=5;
c=a+b;
printf(“The sum is %d”,c);
getch();
}
Symbol Table:
Keywords void int
Header files
Operators + %
Punctuations ( ) { , , ( , ) ( ) }
Constants 1 0 5
Identifier a b c
RESULT
Thus the above program is executed and the required output is obtained.
3
Ex.No.2 Develop a Lexical Analyzer to recognize a few patterns in C
AIM
To write a C Program to implement a Lexical Analyzer.
ALGORITHM
1. Start the program.
2. Declare all the variables and file pointers.
3. Display the input program.
4. Separate the keywords in the program and display it.
5. Display the header files of the input program.
6. Separate the operators of the input program and display it.
7. Print the punctuation marks.
8. Print the constant that are present in input program.
9. Print the identifiers of the input program.
10. Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
FILE *fp;
int i,j;
char arr[100],k;
char
kw[10][10]={"int","float","double","end","main","void","include","printf","scan
f"};
char hf[2][10]={"stdio.h","conio.h"};
char op[5]={'+','-','*','/','%'};
char punc[6]={'(',')',',','{','}'};
clrscr();
fp=fopen("input.c","r");
printf("Input Program\n");
while(!feof(fp))
{
arr[0]=fgetc(fp);
4
printf("%c",arr[0]);
}
fclose(fp);
printf("\nSymbol Table\n");
fp=fopen("input.c","r");
printf("\nKeywords");
while(!feof(fp))
{
arr[0]=fgetc(fp);
fscanf(fp,"%s",arr);
for(i=0;i<10;i++)
{
if(strcmp(arr,kw[i])==0)
{
printf("\t%s",arr);
}
}
}
fclose(fp);
fp=fopen("input.c","r");
printf("\nHeader Files");
while(!feof(fp))
{
arr[0]=fgetc(fp);
fscanf(fp,"%s",arr);
for(i=0;i<2;i++)
{
printf("\t%s",arr);
}
}}
fclose(fp);
fp=fopen("input.c","r");
printf("\nOperators");
while(!feof(fp))
{
arr[0]=fgetc(fp);
for(i=0;i<5;i++)
{
if(arr[0]==op[i])
{
5
printf("\t%c",arr[0]);
}
}
}
fclose(fp);
fp=fopen("input.c","r");
printf("\nPunctuation");
while(!feof(fp))
{
arr[0]=fgetc(fp);
for(i=0;i<6;i++)
{
if(arr[0]==punc[i])
{
printf("\t%c",arr[0]);
}
}
}
fclose(fp);
fp=fopen("input.c","r");
printf("\nConstants");
while(!feof(fp))
{
arr[0]=fgetc(fp);
if(isdigit(arr[0]))
{
printf("%c ",arr[0]);
}
}
fclose(fp);
fp=fopen("input.c","r");
printf("\nIdentifier ");
while(!feof(fp))
{
fscanf(fp,"%s",arr);
for(i=0;i<2;i++)
{
if(strcmp(arr,kw[i])==0)
{
fscanf(fp,"%s",arr);
6
j=0;
while(j<strlen(arr)&&arr[j]!=';')
{
printf("%c",arr[j]);
j++;
}}}}
fclose(fp);
getch();
}
INPUT
INPUT.C
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=10;
b=5;
c=a+b;
printf("The sum is %d",c);
getch();
}
OUTPUT
Expression terminated by $: A+B+C$
Given expression: A+B+C
Symbol Table:
RESULT
Thus the above program is executed and the required output is obtained.
7
Ex.No.3 Implement any one Storage Allocation Strategies
(Heap, Static, Stack)
AIM
To write a C program for implementing any one of the storage allocation
strategies(Heap, Static, Stack).
ALGORITHM
#include<stdio.h>
void funct1(void);
void funct2(void);
/*external variable scope is global to main() , funct1() and funct2(), extern
keyword is omitted here, coz just one file*/
int globvar=10;
int main()
{
printf("\n *** Storage classes and scope *** \n);
/* external variable */
globvar=20;
printf("\n Variable globvar , in main()=%d\n",globvar);
funct1();
printf("\n Variable globvar , in main()=%d\n",globvar);
funct2();
printf("\n Variable globvar , in main()=%d\n",globvar);
return 0;
}
/* external variable, scope is global to funct1() and funct2() */
int globvar2=30;
void funct1(void)
{
/* auto variable, scope local to funct1() to funct1() cannot access the external
globvar */
char globvar;
8
/* local variable to funct1() */
globvar='A';
/* external variable */
globvar2=40;
printf("\n In funct1() , globvar=%c and globvar2=%d\n",globvar,globvar2);
}
void funct2(void)
{
/*auto variable, scope local to funct2() and funct2() cannot access the globvar2
*/
double globvar2;
/* external variable */
globvar=50;
/* auto local variable to funct2() */
globvar2=1.234;
printf("\n In funct2(), globvar=%d and globvar2=%.4f\n",globvar,globvar2);
}
OUTPUT
RESULT
Thus the above program is executed and the required output is obtained
9
Ex.NO.4 Design the Backend of the Compiler for 8086 Assembler
AIM
To write C program to implement the back end of the compiler which
takes the three address code and produces the 8086 assembly language
instructions that can be assembled and run using a 8086 assembler .The target
assembly instructions can be simple, move, add, sub, jump. Also simple
addressing modes are used.
ALGORITHM
1. Start the program
2. Declare the variables.
3. Display the input file which contains three address code.
4. Display the 8086 assembly language instruction.
5. Stop the program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(){
int i=2,j=0,k=2,k1=0;
char ip[10],kk[10];
FILE *fp;
clrscr();
printf("\n enter the filename of the intermediate code");
scanf("%s",&kk);
fp=fopen(kk,"r");
if(fp==NULL){
printf("\n error in opening the file");
getch();
}
clrscr();
while(!feof(fp)){
fscanf(fp,"%s\n",ip);
printf("\t\t%s\n",ip);
}
rewind(fp);
printf("\n-----------------------------------\n");
printf("\t Statement \t\t target code\n"0;
10
printf("\n-----------------------------------\n");
while(!feof(fp))
{
fscanf(fp,"%s",ip);
printf("\t%s",ip);
printf("\t\t MOV %c,R%d\n\t",ip[i+k],j);
if(ip[i+1]=='+')
printf("\t\t ADD");
else
printf("\t\t SUB");
if(islower(ip[i]))
printf("%c,R%d\n\n",ip[i+k],j);
else
printf("5c,%c\n",ip[i],ip[i+2]);
j++;
k1=2;
k=0;
}
printf("\n-------------------------------\n");
getch();
fclose(fp);
}
11
OUTPUT
RESULT
Thus the above C program was successfully executed and verified.
12
Ex.No.5 Implementation of Simple Code Optimization
Techniques (Constant Folding, etc.)
AIM
. To write a C program to implement Code Optimization Techniques
ALGORITHM
Input:
Set of 'L' values with corresponding 'R' values.
Output:
Intermediate code & Optimized code after eliminating common
expressions.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct op
{
char l;
char r[20];
}
op[10],pr[10];
void main()
{
int a,i,k,j,n,z=0,m,q;
char *p,*l;
char temp,t
char *tem;
clrscr();
printf("Enter the Number of Values:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(%c=,op[i]l);
printf("%s\n",op[i].r);
}
for(=0;i<n-1;i++)
{
13
temp=op[i].l;
for(j=0;j<n;j++)
{
p=strchr(op[j].r,temp);
if(p)
{
pr[z].l=op[i].l;
strcpy(pr[z].r,op[i].r);
z++;
}
}
}
pr[z].l=op[n-1].l;
strcpy(pr[z].r,op[n-1].l);
z++;
printf("nAfter Dead Code Elimination\n");
for(k=0;k<z;k++)
{
printf("%c\t=",pr[k].l);
printf("%s\n",pr[k].r);
}
for(m=0;m<z;m++)
{
tem=pr[m].r;
for(j=m+1;j<z;j++)
{
p=strstr(tem,pr[j].r);
if(p)
{
t=pr[j].l;
pr[j].l=pr[m].l;
for(i=0;i<z;i++)
{
l=strchr(pr[i].r,t);
if(l)
{
a=l-pr[i].r;
printf("pos: %d",a);
pr[i].r[a]=pr[m].l;
}
14
}
}
}
}
printf("Eliminate Common Expression\n");
for(i=0;i<z;i++)
{
printf("%c\t=",pr[i].l);
printf("%s\n",pr[i].r);
}
for(i=0;i<z;i++)
{
for(j=i+1;j<z;j++)
{
q=strcmp(pr[i].r,pr[j].r);
if((pr[i].l==pr[j].l)&&!q)
{
pr[i].l='\0';
strcpy(pr[i].r,'\0');
}
}
}
printf("Optimized Code\n");
for(i=0;i<z;i++)
{
if(pr[i].l!='\0)
{
printf("%c=",pr[i].l);
printf("%s\n",pr[i].r);
}
}
getch();
}
OUTPUT
15
Left: f right: b+e
Left: r right: f
Intermediate Code
a=9
b=c+d
e=c+d
f=b+e
r=:f
After Dead Code Elimination
b =c+d
e =c+d
f =b+e
r =:f
Eliminate Common Expression
b =c+d
b =c+d
f =b+b
r =:f
Optimized Code
b=c+d
f=b+b
r=:f
RESULT
Thus the above C program was successfully executed and verified.
16
Ex.No.6 Implementation of Lexical Analyser using Lex Tool
AIM
To write a C program to implement Lexical Analyzer using Lex Tool.
ALGORITHM
1. Start the program.
2. Open a file file.c in read and include the yylex() tool for input scanning.
3. Define the alphabets and numbers.
4. Print the pre-processor, function, keyword using yytex.lex tool.
5. Print the relational, assignment and all the operator using yytext() tool.
6. Also scan and print where the loop ends and begins.
7. Use yywrap() to enter an error
8. Stop the program.
PROGRAM
%{
int COMMENT =0;
%}
identifier[_a-zA-Z][_a-zA-Z0-9]*
%%
#.* {printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);}
int |
float |
char |
while |
for |
do |
if |
break |
continue |
void |
switch |
case |
long |
struct |
const |
typedef |
return |
17
else |
goto {printf("\n\t%sis a KEYBOARD",yytext);}
"/*" {COMMENT=1;
printf("\n\n\t COMMENT ENDS\n");}
{identifier}\( {if(!COMMENT)
printf("\n\nFUNCTION \n\t%s",yytext);}
\{ {if(!COMMENT) printf("\n BLOCK BEGINS");}
\} {if(!COMMENT)printf("\n BLOCK ENDS");}
{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}
\".*\" {if(!COMMENT) printf("\n %s is a STRING",yytext);}
[0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER",yytext);}
\)(\;)? {if(!COMMENT) printf("\n\t");ECHO;printf("\n");}
\( ECHO;
= {if(!COMMENT) printf("\n\t%s is a ASSIGNMENT OPERATOR",yytext);}
\<= |
\>= |
\< |
== |
\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}
%%
18
{
return 0;
}
OUTPUT
C:\Flex Windows\EditPlusPortable>flex filename.l
C:\Flex Windows\EditPlusPortable>gcc lex.yy.c -o filename.exe
C:\Flex Windows\EditPlusPortable>filename
#include<stdio.h>
#include<stdio.h>
is a PREPROCESSOR DIRECTIVE
int main(int argc,char *argv[])
int is a keyword
FUNCTION
main(
int is a keyword
argc IDENTIFIER
char is a keyword *
argv[] IDENTIFIER
)
{
BLOCK BEGINS
a=2+3;
a IDENTIFIER
= is a ASSIGNMENT OPERATOR
2 is a NUMBER +
3 is a NUMBER;
}
BLOCK ENDS
RESULT
Thus the above program is executed and the required output is obtained.
19
Ex.No.7 Generate YACC specification for a few
syntactic categories
a. Program to recognize a valid arithmetic expression that uses operator
+,-,* and /
AIM
To write a program to recognize a valid arithmetic expression that uses
operator +, -, * and /.
ALGORITHM
Input:
A valid arithmetic expression.
Output:
Evaluated value of the given input.
PROGRAM
%{
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define YYSTYPE double
%}
%token num
%left '+' '-'
%left '*' '/'
%%
Stmt: Stmt '\n' {printf("Value is %f\n",$1);exit(0);}
| Expr
|
| error '\n' {printf("Invalid");exit(0);}
;
Expr: num { $$=$1; }
| Expr '+' Expr { $$=$1 + $3; }
| Expr '-' Expr { $$=$1 - $3; }
| Expr '*' Expr { $$=$1 * $3; }
| Expr '/' Expr {
if($3==0)
{
printf("Division by zero\n");
exit(0);
20
}
else
$$=$1/$3;
}
| '(' Expr ')' { $$=$2;}
;
%%
main()
{
printf("Enter an expression to evaluate:\n");
yyparse();
}
yyerror(char *s)
{
printf("%s",s);
}
yylex()
{
char ch;
while((ch=getchar())==' ');
if(isdigit(ch)|ch==':')
{
ungetc(ch,stdin);
scanf("%lf",&yylval);
return num;
}
return ch;
}
OUTPUT
RESULT
Thus the above program is executed and the required output is obtained.
21