Professional Documents
Culture Documents
#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];
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;
2
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)
{
3
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)
4
fclose(f1);
fclose(f3);
getch();
Input:
Input.txt
- LDA ALPHA
- ADD ONE
- SUB TWO
- STA BETA
ONE RESB 2
TWO WORD 5
BETA RESW 1
- END -
Optab.txt
LDA 00
STA 23
5
Optab.txt
ADD 01
SUB 05
Output:
Symtab.txt
1012 ALPHA
1017 ONE
1019 TWO
1022 BETA
1025 - END -
Program length = 25
6
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
main()
FILE *fint,*ftab,*flen,*fsym;
int op1[10],txtlen,txtlen1,i,j=0,len;
char
add[5],symadd[5],op[5],start[10],temp[30],line[20],label[20],mne[10],operand[10],symtab[10],opmne[1
0];
clrscr();
fint=fopen("input.txt","r");
flen=fopen("length.txt","r");
ftab=fopen("optab.txt","r");
fsym=fopen("symbol.txt","r");
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
if(strcmp(mne,"START")==0)
strcpy(start,operand);
fscanf(flen,"%d",&len);
printf("H^%s^%s^%d\nT^00%s^",label,start,len,start);
7
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
while(strcmp(mne,"END")!=0)
fscanf(ftab,"%s%s",opmne,op);
while(!feof(ftab))
if(strcmp(mne,opmne)==0)
fclose(ftab);
fscanf(fsym,"%s%s",symadd,symtab);
while(!feof(fsym))
if(strcmp(operand,symtab)==0)
printf("%s%s^",op,symadd);
break;
else
fscanf(fsym,"%s%s",symadd,symtab);
break;
else
fscanf(ftab,"%s%s",opmne,op);
8
if((strcmp(mne,"BYTE")==0)||(strcmp(mne,"WORD")==0))
if(strcmp(mne,"WORD")==0)
printf("0000%s^",operand);
else
len=strlen(operand);
for(i=2;i<len;i++)
printf("%d",operand[i]);
printf("^");
fscanf(fint,"%s%s%s%s",add,label,mne,operand);
ftab=fopen("optab.txt","r");
fseek(ftab,SEEK_SET,0);
printf("\nE^00%s",start);
fclose(fint);
fclose(ftab);
fclose(fsym);
fclose(flen);
9
fclose(fout);
getch();
Input:
input.txt:
1025 - END -
optab.txt:
LDA 00
STA 23
ADD 01
SUB 05
10
Output:
length.txt:25
symbol.txt:
1012 ALPHA
1017 ONE
1019 TWO
1022 BETA
11
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
FILE *f1,*f2,*f3,*f4,*f5;
int len,i,pos=1;
char arg[20],mne[20],opnd[20],la[20],name[20],mne1[20],opnd1[20],pos1[10],pos2[10];
clrscr();
f1=fopen("input.txt","r");
f2=fopen("namtab.txt","w+");
f3=fopen("deftab.txt","w+");
f4=fopen("argtab.txt","w+");
f5=fopen("op.txt","w+");
fscanf(f1,"%s%s%s",la,mne,opnd);
while(strcmp(mne,"END")!=0)
if(strcmp(mne,"MACRO")==0)
{
12
fprintf(f2,"%s\n",la);
fseek(f2,SEEK_SET,0);
fprintf(f3,"%s\t%s\n",la,opnd);
fscanf(f1,"%s%s%s",la,mne,opnd);
while(strcmp(mne,"MEND")!=0)
if(opnd[0]=='&')
itoa(pos,pos1,5);
strcpy(pos2,"?");
strcpy(opnd,strcat(pos2,pos1));
pos=pos+1;
fprintf(f3,"%s\t%s\n",mne,opnd);
fscanf(f1,"%s%s%s",la,mne,opnd);
fprintf(f3,"%s",mne);
else
fscanf(f2,"%s",name);
if(strcmp(mne,name)==0)
13
len=strlen(opnd);
for(i=0;i<len;i++)
if(opnd[i]!=',')
fprintf(f4,"%c",opnd[i]);
else
fprintf(f4,"\n");
fseek(f3,SEEK_SET,0);
fseek(f4,SEEK_SET,0);
fscanf(f3,"%s%s",mne1,opnd1);
fprintf(f5,".\t%s\t%s\n",mne1,opnd);
fscanf(f3,"%s%s",mne1,opnd1);
while(strcmp(mne1,"MEND")!=0)
if((opnd[0]=='?'))
fscanf(f4,"%s",arg);
fprintf(f5,"-\t%s\t%s\n",mne1,arg);
else
14
fprintf(f5,"-\t%s\t%s\n",mne1,opnd1);
fscanf(f3,"%s%s",mne1,opnd1);
else
fprintf(f5,"%s\t%s\t%s\n",la,mne,opnd);
fscanf(f1,"%s%s%s",la,mne,opnd);
fprintf(f5,"%s\t%s\t%s",la,mne,opnd);
fclose(f1);
fclose(f2);
fclose(f3);
fclose(f4);
fclose(f5);
printf("1. argtab.txt\n");
printf("2. namtab.txt\n");
printf("3. deftab.txt\n");
printf("4. op.txt\n");
getch();
}
15
Input.txt
Argtab.txt
N1
N2
Op.txt
Deftab.txt
EX1 &A,&B
LDA ?1
STA ?2
MEND
Namtab.txt
EX1
16
B. Compilers
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
if (len == 0)
return (false);
for (i = 0; i < len; i++) {
if (str[i] != '0' && str[i] != '1' && str[i] != '2'
&& str[i] != '3' && str[i] != '4' && str[i] != '5'
&& str[i] != '6' && str[i] != '7' && str[i] != '8'
&& str[i] != '9' || (str[i] == '-' && i > 0))
return (false);
}
return (true);
}
if (len == 0)
return (false);
for (i = 0; i < len; i++) {
if (str[i] != '0' && str[i] != '1' && str[i] != '2'
&& str[i] != '3' && str[i] != '4' && str[i] != '5'
&& str[i] != '6' && str[i] != '7' && str[i] != '8'
&& str[i] != '9' && str[i] != '.' ||
(str[i] == '-' && i > 0))
return (false);
18
if (str[i] == '.')
hasDecimal = true;
}
return (hasDecimal);
}
right++;
left = right;
} else if (isDelimiter(str[right]) == true && left != right
|| (right == len && left != right)) {
char* subStr = subString(str, left, right - 1);
if (isKeyword(subStr) == true)
printf("'%s' IS A KEYWORD\n", subStr);
// DRIVER FUNCTION
int main()
{
// maximum legth of string is 100 here
char str[100] = "int a = b + 1c; ";
return (0);
}
Output:
'int' IS A KEYWORD
'a' IS A VALID IDENTIFIER
'=' IS AN OPERATOR
'b' IS A VALID IDENTIFIER
'+' IS AN OPERATOR
'1c' IS NOT A VALID IDENTIFIER
20
#include<stdio.h>
#include<string.h>
int main()
char gram[20],part1[20],part2[20],modifiedGram[20],newGram[20],tempGram[20];
int i,j=0,k=0,l=0,pos;
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++){
21
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 A->%s",modifiedGram);
printf("\n X->%s\n",newGram);
Output:
22
#include<stdio.h>
#include<string.h>
#define SIZE 10
int main () {
char non_terminal;
char beta,alpha;
int num;
char production[10][SIZE];
scanf("%d",&num);
for(int i=0;i<num;i++){
scanf("%s",production[i]);
for(int 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];
index++;
23
if(production[i][index]!=0) {
beta=production[i][index+1];
printf("%c->%c%c\'",non_terminal,beta,non_terminal);
printf("\n%c\'->%c%c\'|E\n",non_terminal,alpha,non_terminal);
else
else
index=3;
Output:
24
#include<stdio.h>
#include<conio.h>
int n,i=0;
char s[20];
void E(void);
void E1(void);
void T(void);
void T1(void);
void F(void);
void error(void);
int main()
{
//clrscr();
printf("The given grammar is\n");
printf("E -> TE1\n");
printf("E1 -> +TE1/#\n");
printf("T -> FT1\n");
printf("T1 -> *FT1/#\nF -> (E)/d\n");
printf("Enter the string you want to parse:\n");
scanf("%s",&s);
E();
if(s[i]==NULL)
printf("string is valid\n");
else
printf("string is invalid\n");
getch();
}
void E()
{
T();
E1();
}
void E1()
{
if(s[i]=='+')
{
i++;
T();
E1();
25
}
else if(s[i]=='#')
i++;
else
error();
}
void T()
{
F();
T1();
}
void T1()
{
if(s[i]=='*')
{
i++;
F();
T1();
}
else if(s[i]=='#')
i++;
else
error();
}
void F()
{
if(s[i]=='(')
{
i++;
E();
if(s[i]==')')
i++;
else
error();
}
else if(s[i]=='d')
i++;
else
error();
}
void error()
{
printf("string is invalid\n");
getch();
//exit(0);
}
26
#include<string.h>
#include<conio.h>
char a[10];
int top=-1,i;
void error(){
printf("Syntax Error");
for(i=0;k[i]!='\0';i++)
if(top<9)
a[++top]=k[i];
return a[top];
if(top>=0)
a[top--]='\0';
27
for(i=0;i<=top;i++)
printf("%c",a[i]);
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","$"};
28
clrscr();
gets(ip);
printf("Stack\tInput\tOutput\n\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 {
29
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]);
}
30
if(TOS()=='$'&&an=='$')
break;
if(TOS()=='$'){
error();
break;
k=strcmp(stack(),"$");
else
return 0;
}
31
#include<conio.h>
#include<string.h>
int i=1,j=0,no=0,tmpch=90;
char str[100],left[15],right[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
struct exp
int pos;
char op;
}k[15];
void main()
clrscr();
scanf("%s",str);
findopr();
explore();
getch();
32
void findopr()
for(i=0;str[i]!='\0';i++)
if(str[i]==':')
k[j].pos=i;
k[j++].op=':';
for(i=0;str[i]!='\0';i++)
if(str[i]=='/')
k[j].pos=i;
k[j++].op='/';
for(i=0;str[i]!='\0';i++)
if(str[i]=='*')
k[j].pos=i;
k[j++].op='*';
for(i=0;str[i]!='\0';i++)
if(str[i]=='+')
k[j].pos=i;
33
k[j++].op='+';
for(i=0;str[i]!='\0';i++)
if(str[i]=='-')
k[j].pos=i;
k[j++].op='-';
void explore()
i=1;
while(k[i].op!='\0')
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos]=tmpch--;
printf("\t%c := %s%c%s\t\t",str[k[i].pos],left,k[i].op,right);
for(j=0;j<strlen(str);j++)
if(str[j]!='$')
printf("%c",str[j]);
printf("\n");
i++;
fright(-1);
34
if(no==0)
fleft(strlen(str));
printf("\t%s := %s",right,left);
getch();
exit(0);
printf("\t%s := %c",right,str[k[--i].pos]);
getch();
void fleft(int x)
int w=0,flag=0;
x--;
if(str[x]!='$'&& flag==0)
left[w++]=str[x];
left[w]='\0';
str[x]='$';
flag=1;
x--;
}
35
void fright(int x)
int w=0,flag=0;
x++;
if(str[x]!='$'&& flag==0)
right[w++]=str[x];
right[w]='\0';
str[x]='$';
flag=1;
x++;
}
36
Input.l
%option noyywrap
%{
#include <stdio.h>
int keyword=0,ids=0,posint=0,posfloat=0,negint=0,negfloat=0,opertor=0,space=0,linebk=0;
%}
%%
if|else|switch|for|while {keyword++; return(4);}
[A-Za-z_][A-Za-z0-9_]+? {ids++; return(5);}
\+|\-|\*|\/|\^|\>|\>=|\<|\<=|\=|\==|\!= {opertor++;return(3);}
\+?[0-9]+ {posint++;return(6);}
\+?[0-9]+"."[0-9]+ {posfloat++;return(6);}
-[0-9]+ {negint++;return(6);}
-[0-9]+"."[0-9]+ {negfloat++;return(6);}
[] {space++; return(1);}
[\n] {linebk++; return(2);}
. {return(0);}
%%
while(val=yylex())
{
if(val>3)
printf("\nLexeme=%s \t Length= %d",yytext,yyleng);
}
printf("\nKeywords:- %d",keyword);
printf("\nIDs:- %d",ids);
37
Inputdata.txt
if is a keyword
10 is positive
-20 is negative
38
Input.l
%option noyywrap
%{
#include"y.tab.h"
extern yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return NUMBER;}
[a-zA-Z]+ {return ID;}
[\t]+ ;
\n {return 0;}
. {return yytext[0];}
%%
input.y
%{
#include<stdio.h>
%}
%token NUMBER ID
%left '+' '-'
%left '*' '/'
%%
expr: expr '+' expr
|expr '-' expr
|expr '*' expr
|expr '/' expr
|'-'NUMBER
|'-'ID
|'('expr')'
|NUMBER
|ID
;
%%
main()
{
printf("Enter the expression\n");
yyparse();
printf("\nExpression is valid\n");
39
exit(0);
}
int yyerror(char *s)
{
printf("\nExpression is invalid");
exit(0);
}