SP
SP
SP
PRACTICAL 1
if(isalnum(ch))
{
buffer[j++] = ch;
}
OUTPUT:
PRACTICAL 2
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");
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;
}
void showtable()
{
int i;
if(in==0)
{
OUTPUT:
PRACTICAL 3
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:
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();
}
OUTPUT:
PRACTICAL 5
OUTPUT:
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)
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();
}
OUTPUT:
PRACTICAL 7
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
OUTPUT:
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:
PRACTICAL 9
%%
OUTPUT:
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","$",
"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=='$')
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: