CN Record
CN Record
: 01
Date :
AIM :
The main objective is to write a C program to implement a symbol table which can
OUTCOME:
This program takes a source program as input, and it is expected to produce a functions
which can create, insert, modify, search and display the character as output.
PRE-REQUESTIE/THEME:
It is easy to create, modify, search and display the character with the help of case statement.
It is achieved by extracting character from the expression using built in functions in C, such as is
switch(),while() etc.
ALGORITHM:
Step 1: Print the five choices namely create, insert, modify, search and display.
Step 3: If choice is 1, then get total numbers of symbol to be added in the symbol table and
call Create ( ) to create the symbol table and call the display ( ) to display the
symbol table with the following contents: symbol name, location and symbol type.
Step 4: If choice is 2, call insert ( ) get a symbol table detail to insert into the symbol table
and Increment the current position the insert the given detail.
Step 5: If choice is 3, then read the symbol to be modified and call the modified ( ) to
Step 6: If choice is 4, then read the symbol to be searched and call the search ( ) to display
PROGRAM :
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct source
char la[25];
char oc[25];
char o[25];
int addr;
}s[100];
struct stable
char sym[25];
int val;
}st[20];
int n,m=0;
int locctr=0;
void input();
void create();
void display();
void modify();
void main()
int ch,a;
char k[10];
do
printf("\n1.CREATE(INSERT)\n\n2.SEARCH\n\n3.MODIFY\n\n4.DISPLAY\n\n5.EXIT\n\n
scanf("%d",&ch);
switch(ch)
case 1:
input();
create();
break;
case 2:
scanf("%s",k);
search(k);
break;
case 3:
modify();
break;
case 4:
display();
break;
case 5:
exit(0);
while(ch<=6);
void input()
int i=0;
printf("\nLABEL\tOPCODE\tOPERAND");
do
scanf("%s",s[i].la);
if(strcmp(s[i].la,"END")==0)
break;
scanf("%s",s[i].oc);
scanf("%s",s[i].o);
i++;
}while(1);
n=i;
void create()
int i;
if(strcmp(s[0].oc,"START")==0)
locctr=atoi(s[0].o);
s[1].addr=locctr;
for(i=1;i<n;i++)
if(strcmp(s[i].oc,"WORD")==0)
locctr=locctr+3;
else if(strcmp(s[i].oc,"RESW")==0)
locctr=locctr+(3*atoi(s[i].o));
else if(strcmp(s[i].oc,"RESB")==0)
locctr=locctr+(atoi(s[i].o));
else if(strcmp(s[i].oc,"BYTE")==0)
locctr=locctr+(strlen(s[i].o)-3);
else
locctr=locctr+3;
s[i+1].addr=locctr;
m=0;
for(i=1;i<n;i++)
{
if(s[i].la[0]!='-')
strcpy(st[m].sym,s[i].la);
st[m].val=s[i].addr;
m++;
void display()
int i;
printf("\nVALUE\tSYMBOL\n");
for(i=0;i<m;i++)
printf("%d",st[i].val);
printf("\t%s\n\n\n",st[i].sym);
int i,a;
for(i=0;i<m;i++)
{
if(strcmp(st[i].sym,key)==0)
break;
else
void modify()
int a,i;
char key[25];
printf("\nADDRESS\tLABEL\tOPCODE\tOPERAND\n");
for(i=1;i<n;i++)
printf("\n%d",s[i].addr);
printf("\t%s",s[i].la);
printf("\t%s",s[i].oc);
printf("\t%s\n\n",s[i].o);
scanf("%s",key);
for(i=0;i<m;i++)
{
if(strcmp(st[i].sym,key)==0)
a=1;
break;
if(a==1)
scanf("%s",key);
strcpy(st[i].sym,key);
else
}
OUTPUT :
linus@linus:~$ ./sym
1.CREATE(INSERT)
2.SEARCH
3.MODIFY
4.DISPLAY
5.EXIT
- LDA ALPHA
ALPHA RESB 1
END
1.CREATE(INSERT)
2.SEARCH
3.MODIFY
4.DISPLAY
5.EXIT
SYMBOL TABLE
VALUE SYMBOL
1003 ALPHA
1.CREATE(INSERT)
2.SEARCH
3.MODIFY
4.DISPLAY
5.EXIT
ALPHA
1.CREATE(INSERT)
2.SEARCH
3.MODIFY
4.DISPLAY
5.EXIT
SOURCE CODE
ALPHA
GAMMA
1.CREATE(INSERT)
2.SEARCH
3.MODIFY
4.DISPLAY
5.EXIT
SYMBOL TABLE
VALUE SYMBOL
1003 GAMMA
1.CREATE(INSERT)
2.SEARCH
3.MODIFY
4.DISPLAY
5.EXIT
linus@linus:~$
RESULT:
Thus the C program to implement symbol table has been executed successfully.
AIM :
The main objective is to develop a lexical analyzer to recognize the patterns (Ex. identifiers,
OUTCOME:
This program reads the file input and displays the tokens and its type as output.
ALGORITHM:
#include<stdio.h>
int main()
FILE *fp;
char op[20]={'+','-','*','/','%'};
int i,j=0,num=0,flag,f;
char file[20],ch,id[30];
scanf("%s",file);
fp=fopen(file,"r");
while(ch!=EOF)
printf("%c",ch);
ch=getc(fp);
fclose(fp);
fp=fopen(file,"r");
printf("\n\t _________________________________");
while(!feof(fp))
flag=0;
ch=fgetc(fp);
i=0;
if(isalpha(ch)||isdigit(ch))
f=0;
while(isalpha(ch)||isdigit(ch))
id[j++]=ch;
ch=fgetc(fp);
id[j]='\0';
j=0;
if(f==0)
i=0;
while(i<5&&flag!=1)
if(ch==op[i])
flag=1;
i++;
i=0;
if(ch=='\n')
num++;
}
fclose(fp);
OUTPUT :
INPUT FILE:
a=b*c-d/e
linus@linus:~/Desktop$ ./cse
Token Separation
a=b*c-d/e
_____________________________
0 a Identifier
0 b Identifier
0 * Operator
0 c Identifier
0 - Operator
0 d Identifier
0 / Operator
0 e Identifier
linus@linus:~/Desktop$
RESULT:
Thus the C program to develop a lexical analyzer to recognize the patterns has been executed
successfully.
Ex. No. : 03 IMPLEMENTATION OF LEXICAL ANALYZER USING LEX TOOL
Date :
AIM :
OUTCOME:
This program reads the lex program rules and displays the expressions as output.
ALGORITHM:
Declaration %%
Translation rules %%
Auxilary procedure.
3. The declaration section includes declaration of variables, maintest, constants and regular
definitions.
P1 {action}
P2 {action}
Pn {action}
6. Compile the lex program with lex compiler to produce output file as lex.yy.c.
PROGRAM :
/* program name is lexp.l */
%{
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
int |
float |
char |
double |
while |
for |
do |
if |
break |
continue |
void |
switch |
case |
long |
struct |
const |
typedef |
return |
else |
{identifier}\( {if(!COMMENT)printf("\n\nFUNCTION\n\t%s",yytext);}
\( ECHO;
\<= |
\>= |
\< |
== |
%%
if (argc > 1)
FILE *file;
file = fopen(argv[1],"r");
if(!file)
{
exit(0);
yyin = file;
yylex();
printf("\n\n");
return 0;
int yywrap()
return 0;
hi.c
#include<stdio.h>
int main()
int a,b;
}
OUTPUT :
int is a KEYWORD
FUNCTION
main()
BLOCK BEGINS
int is a KEYWORD
a IDENTIFIER,
b IDENTIFIER;
BLOCK ENDS
RESULT:
Thus the above program is compiled and executed successfully and output is verified.
Ex. No. : 04. (a) PROGRAM TO RECOGNIZE A VALID ARITHMETIC EXPRESSION THAT USES
OPERATOR +, - , * AND /.
Date :
AIM:
and /.
ALGORITHM:
3. Create the input file arith_id.y and the file is used to print the valid arithmetic
expression.
PROGRAM :
%{
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
%}
%%
| expr
|
expr : num
| let
%%
main()
yyparse();
yylex()
int ch;
if(isdigit(ch))
return num;
if(isalpha(ch))
return let;
return ch;
}
yyerror(char *s)
printf("%s",s);
OUTPUT :
linus@linus:~/Desktop$ ./aa
..valid expresssion..
linus@linus:~/Desktop$ ./aa
syntax error
..invalid..
linus@linus:~/Desktop$
RESULT:
Thus the above program is compiled and executed successfully and output is verified.
Ex. No. : 04.(b) PROGRAM TO RECOGNIZE A VALID VARIABLE WHICH STARTS WITH A LETTER
FOLLOWED BY ANY NUMBER OF LETTERS OR DIGITS
AIM:
To write a Program to recognize a valid variable which starts with a letter followed by any
PROGRAM :
%{
#include<stdio.h>
#include<ctype.h>
%}
%%
| error { yyerror("Rejected\n"); } ;
| XTERM dig
| let
%%
yylex()
char ch;
while((ch=getchar())==' ');
if(isalpha(ch))
return let;
if(isdigit(ch))
return dig;
return ch;
main()
printf("enter a variable:");
yyparse();
yyerror(char *s)
printf("%s",s);
OUTPUT :
linus@linus:~/Desktop$ ./aa
enter a variable:abc12
Accepted
^C
linus@linus:~/Desktop$ ./aa
enter a variable:12abc
Rejected
Rejected
Rejected
Rejected
Rejected
Rejected
RESULT:
Thus the above program is compiled and executed successfully and output is verified.
Ex. No. : 04.(c) IMPLEMENTATION OF CALCULATOR USING LEX AND YACC
Date :
AIM :
ALGORITHM :
2. Math.h is used to calculate the mathematical functions using LEX and YACC
3. The program is created then defines the possible symbol files in the calci.y file
name.
PROGRAM :
cal.l
%{
#include <stdlib.h>
#include <stdio.h>
#include "y.tab.h"
void yyerror(char*);
%}
%%
[ \t]+ ;
return INTEGER;}
\n {return *yytext;}
. {char msg[25];
yyerror(msg);
cal.y
%{
#include <stdlib.h>
#include <stdio.h>
int yylex(void);
#include "y.tab.h"
%}
%token INTEGER
%%
program:
line program
| line
line:
| 'n'
expr:
mulex:
| term { $$ = $1; }
term:
| INTEGER { $$ = $1; }
%%
fprintf(stderr,"%s\n",s);
return;
yywrap()
return(1);
int main(void)
/*yydebug=1;*/
yyparse();
return 0;}
OUTPUT :
linus@linus:~$ cd Desktop/
linus@linus:~/Desktop$ ./a.out
10+10
20
4-2
2++2
syntax error
linus@linus:~/Desktop$
RESULT:
Thus the above program is compiled and executed successfully and output is verified.
Ex. No. : 05 CONVERT THE BNF RULES INTO YACC FORM AND WRIT CODE TO GENERATE
ABSTRACT SYNTAX TREE
AIM :
The main objective is to convert the BNF rules into YACC form and write code to generate
OUTCOME:
This program converts the input file to abstract syntax tree and generates three address
SAMPLE PROGRAM TITLE: Implementation of syntax analyzer using lex and YACC.
ALGORITHM:
4. Go to terminal .
bnf.l
%{
#include"y.tab.h"
#include<stdio.h>
#include<string.h>
int LineNo=1;
%}
identifier [a-zA-Z][_a-zA-Z0-9]*
number [0-9]+|([0-9]*\.[0-9]+)
%%
main ()
return MAIN;
if return IF;
int |
char |
{identifier} {strcpy(yylval.var,yytext);
return VAR;}
{number} {strcpy(yylval.var,yytext);
return NUM;}
\< |
\> |
\>= |
\<= |
== {strcpy(yylval.var,yytext);
return RELOP;}
[ \t] ;
\n LineNo++;
. return yytext[0];
%%
bnf.y
%{
#include<string.h>
#include<stdio.h>
struct quad
char op[5];
char arg1[10];
char arg2[10];
char result[10];
}QUAD[30];
struct stack
{
int items[100];
int top;
}stk;
int Index=0,tIndex=0,StNo,Ind,tInd;
%}
%union
char var[10];
%%
CODE: BLOCK
| STATEMENT CODE
| STATEMENT
;
STATEMENT: DESCT ';'
| ASSIGNMENT ';'
| CONDST
| WHILEST
| VAR
strcpy(QUAD[Index].op,"=");
strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,$1);
strcpy($$,QUAD[Index++].result);
| VAR
| NUM
CONDST: IFST{
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
| IFST ELSEST
strcpy(QUAD[Index].op,"==");
strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"FALSE");
strcpy(QUAD[Index].result,"-1");
push(Index);
Index++;
BLOCK {
strcpy(QUAD[Index].op,"GOTO");
strcpy(QUAD[Index].arg1,"");
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,"-1");
push(Index);
Index++;
};
ELSEST: ELSE{
tInd=pop();
Ind=pop();
push(tInd);
sprintf(QUAD[Ind].result,"%d",Index);
BLOCK{
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
};
StNo=Index-1;
| VAR
| NUM
WHILEST: WHILELOOP{
Ind=pop();
sprintf(QUAD[Ind].result,"%d",StNo);
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
strcpy(QUAD[Index].op,"==");
strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"FALSE");
strcpy(QUAD[Index].result,"-1");
push(Index);
Index++;
BLOCK {
strcpy(QUAD[Index].op,"GOTO");
strcpy(QUAD[Index].arg1,"");
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,"-1");
push(Index);
Index++;
%%
FILE *fp;
int i;
if(argc>1)
fp=fopen(argv[1],"r");
if(!fp)
{
printf("\n File not found");
yyin=fp;
yyparse();
"\n\t\t-------------------");
for(i=0;i<Index;i++)
%s\t%s",i,QUAD[i].op,QUAD[i].arg1,QUAD[i].arg2,QUAD[i].result);
printf("\n\t\t -------");
printf("\n\n");
return 0;
stk.top++;
if(stk.top==100)
stk.items[stk.top]=data;
}
Page No
int pop()
int data;
if(stk.top==-1)
exit(0);
data=stk.items[stk.top--];
return data;
strcpy(QUAD[Index].op,op);
strcpy(QUAD[Index].arg1,arg1);
strcpy(QUAD[Index].arg2,arg2);
sprintf(QUAD[Index].result,"t%d",tIndex++);
strcpy(result,QUAD[Index++].result);
yyerror()
yywrap()
{
return(1);
}
Page No
OUTPUT :
------------------------------------------------------------------------
------------------------------------------------------------------------
0 < a b t0
1 == t0 FALSE 5
2 + a b t1
3 = t1 a
4 GOTO 5
5 < a b t2
6 == t2 FALSE 10
7 + a b t3
8 = t3 a
9 GOTO 5
10 <= a b t4
11 == t4 FALSE 15
12 - a b t5
13 = t5 c
14 GOTO 17
15 + a b t6
16 = t6 c
-----------------------------------------------------------------------
RESULT :
Thus the program to Convert the BNF rules into YACC form and write code to generate
Ex. No. : 06
Date :
AIM :
Write a program to check whether the string belongs to the grammar or not.
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int i;
for(i=1;i<argc;i++)
if(lstat(argv[i],&buf)<0)
printf("lstat error!\n");
else if(S_ISREG(buf.st_mode))
printf("regular file!\n");
else if(S_ISDIR(buf.st_mode))
printf("Directory file!\n");
else if(S_ISCHR(buf.st_mode))
else if(S_ISFIFO(buf.st_mode))
printf("FIFO file!\n");
else
printf("socket file\n");
}
}
Page No
OUTPUT :
linus@linus:~$ ./qq
regular file!
lstat error!
linus@linus:~$ ./qq os
Directory file!
regular file!
linus@linus:~$
RESULT:
AIM :
OUTCOME:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<termios.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;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("left: ");
op[i].l=getchar();
printf("\tright: ");
scanf("%s",op[i].r);
printf("Intermediate Code\n") ;
for(i=0;i<n;i++)
printf("%c=",op[i].l);
printf("%s\n",op[i].r);
for(i=0;i<n-1;i++)
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].r);
z++;
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;
}
}
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");
Page No
for(i=0;i<z;i++)
if(pr[i].l!='\0')
printf("%c=",pr[i].l);
printf("%s\n",pr[i].r);
}
Page No
OUTPUT:
Left: a right: 9
Left: r right: f
Intermediate Code
a=9
b=c+d
e=c+d
f=b+e
r=:f
=f
=f
Optimized Code
=f
RESULT:
Thus the C program to implement code optimization technique has been executed
successfully.
Page No
Ex. No. : 08
Date :
AIM :
The main objective is to write a C program to implement of 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,
OUTCOME:
This program takes a source program as input, and it is expected to produce a functions
PRE-REQUESTIE/THEME:
ALGORITHM:
7- Stop
Page No
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
int i=2,j=0,k=2,k1=0;
char ip[10],kk[10];
FILE*fp;
scanf("%s",&kk);
fp=fopen(kk,"r");
if(fp==NULL)
while(!feof(fp))
fscanf(fp,"%s/n",ip);
printf("\t\t%s\n",ip);
rewind(fp);
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\tADD");
else
printf("\t\tSUB");
if(islower(ip[i]))
printf("%c,R%d\n\n",ip[i+k1],j);
else
printf("%c,%c\n",ip[i],ip[i+2]);
j++;
k1=2;
k=0;
fclose(fp);
}
Page No
OUTPUT :
x=a-b
y=a-c
z=a+b
c=A-B
c=A-B
-----------------------------------------------------
-----------------------------------------------------
x=a-b Mov,R0
SUBa,R0
y=a-c MOVa,R1
SUBc,R1
z=a+b MOCa,R2
ADDb,R2
c=A-B MOVa,R4
SUBA,B
c=A-B MOVA,R5
SUBA,B
RESULT:
Thus the C program to implement Back end compiler using 8086 assembler has been
executed successfully.