You are on page 1of 22

(2150708) 160280116052

PRACTICAL 1

AIM: Write a program to implement lexical analyser.


CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int isKeyword(char buffer[])


{
char keywords[32][10] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
int i, flag = 0;
for(i = 0; i < 32; ++i)
{
if(strcmp(keywords[i], buffer) == 0){
flag = 1;
break;
}
}
return flag;
}
void main()
{
char ch, buffer[15], operators[] = "+-*/%=";
FILE *fp;
int i,j=0;
clrscr();
fp = fopen("program.txt","r");
if(fp == NULL)
{
printf("Error while opening the file!\n");
exit(0);
}
while((ch = fgetc(fp)) != EOF)
{
for(i = 0; i < 6; ++i)
{
if(ch == operators[i])
printf("%c is operator\n", ch);
}

if(isalnum(ch))
{
buffer[j++] = ch;
}

System Programming 1 L.D. College of Engineering


(2150708) 160280116052

if((ch == ' ' || ch == '\n') && (j != 0))


{
buffer[j] = '\0';
j = 0;
if(isKeyword(buffer) == 1)
printf("%s is keyword\n", buffer);
else
printf("%s is identifier\n", buffer);
}
}
fclose(fp);
getch();
}

OUTPUT:

System Programming 2 L.D. College of Engineering


(2150708) 160280116052

PRACTICAL 2

AIM: Write a program to implement symbol table.


CODE:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
FILE *fp;
char delim[18]={' ','\t','\n',',','(',')','[',']','{','}','#','+','-','*','/','%','=','!'};
char key[21][12]={"int","float","char","double","bool","void","extern","auto","bool",
"goto","static","class","struct","for","if","else","return","register","long","while","do"};

char ctype[12];
char avoid[5][12]={"include","define","getch","printf","scanf"};
struct symtab
{
char id[20];
char type[20];
}p[30];
int in=0;

void construct();
int isdelim(char);
void check(char[]);
int checkkey(char[]);
void showtable();

void main()
{
char fname[12];
clrscr();
printf("\nEnter the filename : ");
scanf("%s",fname);
fp=fopen(fname,"r");
if(fp==NULL)
printf("\nThe file doesn't exist.");
else
{
construct();
showtable();
}
fclose(fp);
getch();
}
void construct()
{
char c,ch,token[12];
int f=0,j=0,kf=0;
strcpy(ctype,"NULL");

System Programming 3 L.D. College of Engineering


(2150708) 160280116052

while(!feof(fp))
{
c=getc(fp);
if(c==';'||c=='(')
{
if(f==1)
{
token[j]='\0';
j=0;
f=0;
kf=checkkey(token);
if(kf==0)
check(token);
}
strcpy(ctype,"NULL");
}
else if(c=='"')
{
while((c=getc(fp))!='"');
}
else if(c=='<')
{
while((c=getc(fp))!='>');
}
else if(isdelim(c))
{
if(f==1)
{
token[j]='\0';
j=0;
f=0;
kf=checkkey(token);
if(kf==0)
check(token);
}
}
else if(isalpha(c)||c=='_')
{
token[j++]=c;
f=1;
}
}
}
int isdelim(char c)
{
int i;
for(i=0;i<18;i++)
{
if(c==delim[i])
return 1;
}
return 0;
}

System Programming 4 L.D. College of Engineering


(2150708) 160280116052

int checkkey(char t[])


{
int i;
for(i=0;i<5;i++)
if(strcmp(avoid[i],t)==0)
return 1;
for(i=0;i<21;i++)
if(strcmp(key[i],t)==0)
{
strcpy(ctype,key[i]);
return 1;
}
return 0;
}
void check(char t[])
{
int i;
for(i=0;i<in;i++)
{
//Checking for Redeclaration
if(((strcmp(t,p[i].id))==0)&&((strcmp(ctype,p[i].type))==0))
{
printf("\nRedeclaration for '%s'",t);
return;
}
else
if(((strcmp(t,p[i].id))==0)&&((strcmp(ctype,p[i].type))!=0)&&((strcmp(ctype,
"NULL")!=0)))
{
printf("\nMultiple declaration for %s",t);
return;
}
}
if(strcmp(ctype,"NULL")==0)
{
for(i=0;i<in;i++)
{
if(strcmp(t,p[i].id)==0)
return;
}
printf("\n%s Undeclared ",t);
return;
}
strcpy(p[in].id,t);
strcpy(p[in].type,ctype);
in++;
}

void showtable()
{
int i;
if(in==0)
{

System Programming 5 L.D. College of Engineering


(2150708) 160280116052

printf("\nSymbol table is empty.");


return;
}
printf("\nSymbol table");
printf("\n------------");
printf("\nIdentifier\tType\tAddress");
printf("\n----------\t----\t-------");
for(i=0;i<in;i++)
printf("\n%s\t\t%s\t%u",p[i].id,p[i].type,&p[i]);
}

OUTPUT:

System Programming 6 L.D. College of Engineering


(2150708) 160280116052

PRACTICAL 3

AIM: Write a program to check whether parentheses are balanced or not.


CODE:
#include<stdio.h>
#include<conio.h>
char st[20];
int top=-1;
void push(char cr)
{
if(top==20)
printf("\nstack is full");
else
{
top++;
st[top]=cr;
}
}
void pop()
{
if(top==-1)
printf("\nStack is empty");
else
top--;
}
void display()
{
int i;
if(top==-1)
printf("\nString is Balanced");
else
printf("String is not balanced");
}
void main()
{
int l,i=0;
char s[20];
clrscr();
printf("\nEnter string max 20 chars : ");
gets(s);
l=strlen(s);
while(l>0)
{
if(s[i]=='(')
push('(');
else if(s[i]=='{')
push('{');
else if(s[i]=='[')

System Programming 7 L.D. College of Engineering


(2150708) 160280116052

push('[');
else if(s[i]==')')
{
if(st[top]=='(')
pop();
else
push(')');
}
else if(s[i]=='}')
{
if(st[top]=='{')
pop();
else
push('}');
}
else if(s[i]==']')
{
if(st[top]=='[')
pop();
else
push(']');
}
l--;
i++;
}
display();
getch();
}

OUTPUT:

System Programming 8 L.D. College of Engineering


(2150708) 160280116052

PRACTICAL 4

AIM: Write a program to find out if there is left recursion in grammar. Remove the left
recursion.
CODE:
#include<stdio.h>
#include<string.h>
#define SIZE 10
void main ()
{
char non_terminal;
char beta,alpha;
int num;
int i;
char production[10][SIZE];
int index=3; /* starting of the string following "->" */
clrscr();
printf("Enter Number of Production : ");
scanf("%d",&num);
printf("Enter the grammar as E->E-A :\n");
for(i=0;i<num;i++){
scanf("%s",production[i]);
}
for(i=0;i<num;i++){
printf("\nGRAMMAR : : : %s",production[i]);
non_terminal=production[i][0];
if(non_terminal==production[i][index]) {
alpha=production[i][index+1];
printf(" is left recursive.\n");
while(production[i][index]!=0 && production[i][index]!='|')
{
index++;
}
if(production[i][index]!=0)
{
beta=production[i][index+1];
printf("Grammar without left recursion:\n");
printf("%c->%c%c\'",non_terminal,beta,non_terminal);
printf("\n%c\'-
>%c%c%c\'|%c\n",non_terminal,alpha,beta,non_terminal,non_terminal);
}
else
printf(" can't be reduced\n");
}
else
printf(" is not left recursive.\n");
index=3;
}
getch();
}

System Programming 9 L.D. College of Engineering


(2150708) 160280116052

OUTPUT:

System Programming 10 L.D. College of Engineering


(2150708) 160280116052

PRACTICAL 5

AIM: Write a program to perform left factoring on a grammar.


CODE:
#include<stdio.h>
#include<string.h>
void main()
{
char gram[20],part1[20],part2[20],modifiedGram[20],newGram[20],tempGram[20];
int i,j=0,k=0,pos;
clrscr();
flushall();
printf("Enter Production : A->");
gets(gram);
for(i=0;gram[i]!='|';i++,j++)
part1[j]=gram[i];
part1[j]='\0';
for(j=++i,i=0;gram[j]!='\0';j++,i++)
part2[i]=gram[j];
part2[i]='\0';
for(i=0;i<strlen(part1)||i<strlen(part2);i++)
{
if(part1[i]==part2[i])
{
modifiedGram[k]=part1[i];
k++;
pos=i+1;
}
}
for(i=pos,j=0;part1[i]!='\0';i++,j++){
newGram[j]=part1[i];
}
newGram[j++]='|';
for(i=pos;part2[i]!='\0';i++,j++){
newGram[j]=part2[i];
}
modifiedGram[k]='X';
modifiedGram[++k]='\0';
newGram[j]='\0';
printf("\n After performing left factoring...");
printf("\n A->%s",modifiedGram);
printf("\n X->%s\n",newGram);
getch();
}

System Programming 11 L.D. College of Engineering


(2150708) 160280116052

OUTPUT:

System Programming 12 L.D. College of Engineering


(2150708) 160280116052

PRACTICAL 6

AIM: Write a C program to input an assembly program and prepare mnemonic and
symbol table.
CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *f1,*f2,*f3,*f4;
int lc,sa,l,op1,o,len;
char m1[20],la[20],op[20],otp[20],s,s1;
clrscr();

f1=fopen("input.txt","r");
f3=fopen("symtab.txt","w");
fscanf(f1,"%s %s %d",la,m1,&op1);
if(strcmp(m1,"START")==0)
{
sa=op1;
lc=sa;
printf("\t%s\t%s\t%d\n",la,m1,op1);
}
else
lc=0;
fscanf(f1,"%s %s",la,m1);
while(!feof(f1))
{
fscanf(f1,"%s",op);
printf("\n%d\t%s\t%s\t%s\n",lc,la,m1,op);
if(strcmp(la,"-")!=0)
{
fprintf(f3,"\n%d\t%s\n",lc,la);
}
f2=fopen("optab.txt","r");
fscanf(f2,"%s %d",otp,&o);
while(!feof(f2))
{
if(strcmp(m1,otp)==0)
{
lc=lc+3;
break;
}
fscanf(f2,"%s %d",otp,&o);
}
fclose(f2);
if(strcmp(m1,"WORD")==0)

System Programming 13 L.D. College of Engineering


(2150708) 160280116052

lc=lc+3;
}
else if(strcmp(m1,"RESW")==0)
{
op1=atoi(op);
lc=lc+(3*op1);
}
else if(strcmp(m1,"BYTE")==0)
{
if(op[0]=='X')
lc=lc+1;
else
{
len=strlen(op)-2;
lc=lc+len;}
}
else if(strcmp(m1,"RESB")==0)
{
op1=atoi(op);
lc=lc+op1;
}
fscanf(f1,"%s%s",la,m1);
}
if(strcmp(m1,"END")==0)
{
printf("Program length =\n%d",lc-sa);
}
fclose(f1);
fclose(f3);
printf("\n----Symbol table----");
f3=fopen("symtab.txt","r");
do{
s=getc(f3);
printf("%c",s);
}while(s!=EOF);
fclose(f3);
printf("\n----Mnemonic table----\n");
f2=fopen("optab.txt","r");
do{
s1=getc(f2);
printf("%c",s1);
}while(s1!=EOF);
fclose(f2);
getch();
}

System Programming 14 L.D. College of Engineering


(2150708) 160280116052

OUTPUT:

System Programming 15 L.D. College of Engineering


(2150708) 160280116052

PRACTICAL 7

AIM: Use TASM/NASM software to write an assembly program to find factorial of a


number.
CODE:
section .text
global _start ;must be declared for using gcc

_start: ;tell linker entry point

mov bx, 3 ;for calculating factorial 3


call proc_fact
add ax, 30h
mov [fact], ax

mov edx,len ;message length


mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel

mov edx,1 ;message length


mov ecx,fact ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel

mov eax,1 ;system call number (sys_exit)


int 0x80 ;call kernel

proc_fact:
cmp bl, 1
jg do_calculation
mov ax, 1
ret

do_calculation:
dec bl
call proc_fact
inc bl
mul bl ;ax = al * bl
ret

section .data
msg db 'Factorial 3 is:',0xa
len equ $ - msg

section .bss
fact resb 1

System Programming 16 L.D. College of Engineering


(2150708) 160280116052

OUTPUT:

System Programming 17 L.D. College of Engineering


(2150708) 160280116052

PRACTICAL 8

AIM: Use LEX tool to print out all numbers from a given file.
CODE:
{
#include <stdio.h>
%}
%%
[0-9]+ { printf("%s\n", yytext); }
.|\n ;
%%
int yywrap(){}
int main()
{
FILE *fp;
char filename[50];
printf("Enter the filename: \n");
scanf("%s",filename);
fp = fopen(filename,"r");
yyin = fp;

yylex();
return 0;
}

OUTPUT:

System Programming 18 L.D. College of Engineering


(2150708) 160280116052

PRACTICAL 9

AIM: Write a YACC program.


CODE:
%{
#include <stdio.h>
%}

%union {int a_number;}


%start line
%token <a_number> number
%type <a_number> exp term factor

%%

line : exp ';' {printf ("result is %d\n", $1);}


;
exp : term {$$ = $1;}
| exp '+' term {$$ = $1 + $3;}
| exp '-' term {$$ = $1 - $3;}
;
term : factor {$$ = $1;}
| term '*' factor {$$ = $1 * $3;}
| term '/' factor {$$ = $1 / $3;}
;
factor : number {$$ = $1;}
| '(' exp ')' {$$ = $2;}
;
%%

int main (void) {return yyparse ( );}

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

OUTPUT:

System Programming 19 L.D. College of Engineering


(2150708) 160280116052

PRACTICAL 10

AIM: Parse a string using Predictive parser for the given grammer. (E->T+E/T, T-
>F*T/F, F->id)
CODE:
#include<string.h>
#include<conio.h>
char a[10];
int top=-1,i;
void error(){
printf("Syntax Error");
}
void push(char k[])
{
for(i=0;k[i]!='\0';i++)
{
if(top<9)
a[++top]=k[i];
}
}
char TOS()
{
return a[top];
}
void pop()
{
if(top>=0)
a[top--]='\0';
}
void display()
{
for(i=0;i<=top;i++)
printf("%c",a[i]);
}
void display1(char p[],int m)
{
int l;
printf("\t");
for(l=m;p[l]!='\0';l++)
printf("%c",p[l]);
}
char* stack(){
return a;
}
int main()
{
char ip[20],r[20],st,an;
int ir,ic,j=0,k;
char t[5][6][10]={"$","$","TH","$","TH","$",
"+TH","$","e","e","$","e",
"$","$","FU","$","FU","$",

System Programming 20 L.D. College of Engineering


(2150708) 160280116052

"e","*FU","e","e","$","e",
"$","$","(E)","$","i","$"};
clrscr();
printf("\n Here in grammer H=E' and U=T'");
printf("\nEnter any String(Append with $): ");
gets(ip);
printf("Stack\tInput\tOutput\n");
printf("-----\t-----\t------\n");
push("$E");
display();
printf("\t%s\n",ip);
for(j=0;ip[j]!='\0';)
{
if(TOS()==an)
{
pop();
display();
display1(ip,j+1);
printf("\tPOP\n");
j++;
}
an=ip[j];
st=TOS();
if(st=='E')ir=0;
else if(st=='H')ir=1;
else if(st=='T')ir=2;
else if(st=='U')ir=3;
else if(st=='F')ir=4;
else {
error();
break;
}
if(an=='+')ic=0;
else if(an=='*')ic=1;
else if(an=='(')ic=2;
else if(an==')')ic=3;
else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
else if(an=='$')ic=5;
strcpy(r,strrev(t[ir][ic]));
strrev(t[ir][ic]);
pop();
push(r);
if(TOS()=='e')
{
pop();
display();
display1(ip,j);
printf("\t%c->%c\n",st,238);
}
else{
display();
display1(ip,j);
printf("\t%c->%s\n",st,t[ir][ic]);
}
if(TOS()=='$'&&an=='$')

System Programming 21 L.D. College of Engineering


(2150708) 160280116052

break;
if(TOS()=='$'){
error();
break;
}
}
k=strcmp(stack(),"$");
if(k==0)
printf("\n Given String is accepted");
else
printf("\n Given String is not accepted");
getch();
return 0;
}

OUTPUT:

System Programming 22 L.D. College of Engineering

You might also like