PROGRAM :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
void main()
{
char ch,ch1, buffer[50], operator[]="+-*/%";
FILE *fp,*fp2;
int i, j=0, l=1, index = 1;
char arr[1000][3];
fp=fopen("inp.txt", "r");
fp2=fopen("output.txt", "w+");
if(fp==NULL)
{
printf("Error while opening file\n");
exit(0);
}
else
{
printf("Lexeme: \t L.no:\t Token:");
fprintf(fp2,"Lexeme: \t L.no:\t Token:");
while((ch=fgetc(fp))!=EOF)
{
for(i=0;i<5;++i)
{
if(ch==operator[i])
{
printf("\n%c \t\t %d\t Arithmetic Operator ", ch,l);
break;
}
}
if(i==5)
{
if(isalnum(ch))
{
if(isdigit(ch) && j==0)
{
j=0;
}
else {
buffer[j++] = ch;
ch1= fgetc(fp);
if(isalnum(ch1))
fseek(fp, -1, SEEK_CUR);
else {
fseek (fp,-1,SEEK_CUR);
buffer[j]='\0';
if(isKeyword(buffer)==1){
if(strcmp(buffer, "printf")==0){
while((ch=fgetc(fp))!='\n'){
}
}
printf("\n%s \t\t %d \t keyword ", buffer, l);
j=0;
}
else{
if(strcmp(buffer, "main")!=0){
fseek(fp2, SEEK_SET, 0);
char a[50], b[50], c[50], ch2, ch3;
int flag1 = 0;
while(!feof(fp2)){
fscanf(fp2, "%s\t%s\t%s", a, b, c);
if(strcmp(a, buffer) == 0){
flag1 = 1;
break;
}
}
if(flag1==0){
ch2 = fgetc(fp);
if(ch2=='='){
ch3 = fgetc(fp);
fprintf(fp2,"\n%s \t\t %d\t identifier, %c", buffer, index, ch3);
fseek(fp,-2,SEEK_CUR);
}
else{
fprintf(fp2,"\n%s \t\t %d\t identifier", buffer, index);
}
printf("\n%s \t\t %d\t Identifier, %d", buffer, l, index);
index++;
}
else{
printf("\n%s \t\t %d\t Identifier, %s", buffer, l, b);
}
}
j=0;
}
}
}
}
if(ch=='<') {
ch1= fgetc(fp);
if(ch1=='='){
printf("\n%c%c \t\t %d \t Relop LE",ch,ch1,l);
}
else {
fseek(fp,-1,SEEK_CUR);
printf("\n%c \t\t %d \t RelOP LT", ch,l);
}
}
else if(ch=='>')
{
ch1= fgetc(fp);
if(ch1=='=')
{
printf("\n%c%c \t\t %d \t Relop GE",ch,ch1,l);
}
else
{
fseek(fp,-1,SEEK_CUR);
printf("\n%c \t\t %d \t Relop GT", ch,l);
}
}
else if(ch=='=')
{
ch1=fgetc(fp);
if(ch1=='=')
{
printf("\n%c%c \t\t %d \t Relop EQ", ch, ch1, l);
}
else
{
fseek(fp, -1, SEEK_CUR);
printf("\n%c \t\t %d \t Assign OP, EQ",ch,l);
}
}
if(ch=='\n'){
l++;
}
}
}
}
printf("\n");
}
INPUT.TXT :
void main(){
int a,b,c=0;
char ch;
a=b+c;
if(a<=b)
a=b;
printf("Value of a = %d\n",a);
}
SYMBOL TABLE :
OUTPUT:
PROGRAM :
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *input;
int i=0;
char lasthandle[6],stack[50],handles[][5]={")E(","E*E","E+E","i","E^E"};
//(E) becomes )E( when pushed to stack
int top=0,l;
char prec[9][9]={
/*input*/
/*stack + - * / ^ i ( ) $ */
/* + */ '>', '>','<','<','<','<','<','>','>',
/* - */ '>', '>','<','<','<','<','<','>','>',
/* * */ '>', '>','>','>','<','<','<','>','>',
/* / */ '>', '>','>','>','<','<','<','>','>',
/* ^ */ '>', '>','>','>','<','<','<','>','>',
/* i */ '>', '>','>','>','>','e','e','>','>',
/* ( */ '<', '<','<','<','<','<','<','>','e',
/* ) */ '>', '>','>','>','>','e','e','>','>',
/* $ */ '<', '<','<','<','<','<','<','<','>',
};
int getindex(char c)
{
switch(c) {
case '+':return 0;
case '-':return 1;
case '*':return 2;
case '/':return 3;
case '^':return 4;
case 'i':return 5;
case '(':return 6;
case ')':return 7;
case '$':return 8;
}
}
int shift()
{
stack[++top]=*(input+i++);
stack[top+1]='\0';
}
int reduce()
{
int i,len,found,t;
for(i=0;i<5;i++)//selecting handles
{
len=strlen(handles[i]);
if(stack[top]==handles[i][0]&&top+1>=len)
{
found=1;
for(t=0;t<len;t++)
{
if(stack[top-t]!=handles[i][t])
{
found=0;
break;
}
}
if(found==1)
{
stack[top-t+1]='E';
top=top-t+1;
strcpy(lasthandle,handles[i]);
stack[top+1]='\0';
return 1;//successful reduction
}
}
}
return 0;
}
void dispstack()
{
int j;
for(j=0;j<=top;j++)
printf("%c",stack[j]);
}
void dispinput()
{
int j;
for(j=i;j<l;j++)
printf("%c",*(input+j));
}
void main()
{
int j;
input=(char*)malloc(50*sizeof(char));
printf("\nEnter the string : ");
scanf("%s",input);
input=strcat(input,"$");
l=strlen(input);
strcpy(stack,"$");
printf("\nSTACK\tINPUT\tACTION");
printf("\n--------------------------------------");
while(i<=l)
{
shift();
printf("\n");
dispstack();
printf("\t");
dispinput();
printf("\tShift");
if(prec[getindex(stack[top])][getindex(input[i])]=='>')
{
while(reduce())
{
printf("\n");
dispstack();
printf("\t");
dispinput();
printf("\tReduced: E->%s",lasthandle);
}
}
}
if(strcmp(stack,"$E$")==0)
printf("\nAccepted;\n");
else
printf("\nNot Accepted;\n");
}
OUTPUT:
PROGRAM :
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char input[100];
int i, err;
void E();
void Eprime();
void T();
void Tprime();
void F();
void E() {
T();
Eprime();
}
void Eprime() {
if (input[i] == '+') {
i++;
T();
Eprime();
} else if (input[i] == '-') {
i++;
T();
Eprime();
}
}
void T() {
F();
Tprime();
}
void Tprime() {
if (input[i] == '*') {
i++;
F();
Tprime();
} else if (input[i] == '/') {
i++;
F();
Tprime();
}
}
void F() {
if (isalnum(input[i])) {
i++;
} else if (input[i] == '(') {
i++;
E();
if (input[i] == ')') {
i++;
} else {
err = 1;
}
} else {
err = 1;
}
}
int main() {
i = 0;
err = 0;
printf("Enter an expression: ");
fgets(input, sizeof(input), stdin);
E();
if (strlen(input) == i && err == 0) {
printf("%s is ACCEPTED\n", input);
} else {
printf("%s is REJECTED\n", input);
}
return 0;
}
OUTPUT :
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <ctype.h>
while(s[i] != '\0'){
if(s[i] == '('){
top++;
stack[top] = s[i];
}
else if(isalpha(s[i])){
post[j++] = s[i];
}
else if(precedence(s[i])){
while(precedence(stack[top]) >= precedence(s[i])){
post[j++] = stack[top--];
}
top++;
stack[top] = s[i];
}
else if(s[i] == ')'){
while(stack[top] != '('){
post[j++] = stack[top--];
}
top--;
}
i++;
}
while(top!=-1){
post[j++] = stack[top--];
}
post[j] = '\0';
}
void main(){
int i = 0;
char ind = '1';
fprintf(fp,"Three Address\n");
fprintf(fp1,"Quadruple\nOP\tO1\tO2\tRES\n");
fprintf(fp2,"Triple\nIN\tOP\tO1\tO2\n");
while(!feof(fp3)) {
fgets(s1,60,fp3);
fgets(s2,60,fp3);
}
postfix(s1);
printf("\nInfix : %s \nPostfix : %s\n", s1, post);
postfix(s2);
char ch, pos1[100];
int j = strlen(s1);
int m = j ;
for (int i = 0; i<strlen(post)-m+1; i++){
pos1[i] = post[j-1];
j++;
}
printf("\nInfix : %s \nPostfix : %s\n", s2, pos1);
while(post[i] != '\0'){
if(precedence(post[i])){
char a = queue[front--];
char b = queue[front--];
if(isdigit(a) && isdigit(b)){
fprintf(fp,"t%c = t%c %c t%c\n", ind, b, post[i], a);
fprintf(fp1,"%c\tt%c\tt%c\tt%c\n", post[i], b, a, ind);
}
else if(isdigit(b)){
fprintf(fp,"t%c = t%c %c %c\n", ind, b, post[i], a);
fprintf(fp1,"%c\tt%c\t%c\tt%c\n", post[i], b, a, ind);
}
else if(isdigit(a)){
fprintf(fp,"t%c = %c %c t%c\n", ind, b, post[i], a);
fprintf(fp1,"%c\t%c\tt%c\tt%c\n", post[i], b, a, ind);
}
else{
fprintf(fp,"t%c = %c %c %c\n", ind, b, post[i], a);
fprintf(fp1,"%c\t%c\t%c\tt%c\n", post[i], b, a, ind);
}
fprintf(fp2,"%c\t%c\t%c\t%c\n", ind, post[i], b, a);
front++;
queue[front] = ind;
ind++;
}
else{
front++;
queue[front] = post[i];
}
i++;
}
}
INPUT.TXT :
a+b*c^d+e
a*b+c*d
THREE ADDRESS CODE:
Three Address
t1 = c ^ d
t2 = b * t1
t3 = a + t2
t4 = t3 + e
t5 = a * b
t6 = c * d
t7 = t5 + t6
TRIPLE.TXT
Triple
IN OP O1 O2
1 ^ c d
2 * b 1
3 + a 2
4 + 3 e
5 * a b
6 * c d
7 + 5 6
QUADRUPLE.TXT:
Quadruple
OP O1 O2 RES
^ c d t1
* b t1 t2
+ a t2 t3
+ t3 e t4
* a b t5
* c d t6
+ t5 t6 t7
OUTPUT:
Infix : a+b*c^d+e
Postfix : abcd^*+e+
Infix : a*b+c*d
Postfix : ab*cd*+
EXPERIMENT NO: 06
CONSTANT PROPAGATION
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdbool.h>
char buf[30];
int main() {
char symbols[100][2][20]={
};
int symbc=0;
char keywords[40]
[20]={"void","int","char","exit","for","while","return","if","else","main","printf"};
char dtypes[40][20]={"int","char","float"};
FILE *fp,*symtab,*fp2,*fp3;
fp = fopen( "inp.txt","r");
fp2=fopen("con.txt","w");
fp3 = fopen( "con.txt","r");
symtab = fopen( "result.txt","w");
if(fp==NULL)
{
printf("File could not be opened!\n");
exit(0);
}
if(symtab==NULL)
{
printf("Result File could not be opened!\n");
exit(0);
}
char buffer[30],idf[30],idf2[30];
char idstack[30][20],opstack[30];
int idtop=0,optop=0;
int ln=1;
bool isd=false;
bool expecting=false;
bool isStringLiteral=false;
bool defining=false;
int sp=0,cp=0,bp=0;
char c=fgetc(fp);
while(!feof(fp)) {
if(isStringLiteral && c!='"') {
fprintf(symtab,"%c",c);
c=fgetc(fp);
continue;
}
else if(!isStringLiteral && !isalnum(c)&&c!='_'&&bp!=0) {
buffer[bp]='\0';
bool isk=false;
for(int i=0;i<40;i++){
if(strcmp(keywords[i],buffer)==0){
isk=true;
break;
}
}
for(int i=0;i<40;i++){
if(strcmp(dtypes[i],buffer)==0){
defining=true;
break;
}
}
if(!isk&&isd) {
strcpy(idstack[idtop],buffer);
idtop++;
}
else if(!isk) {
strcpy(idstack[idtop],buffer);
idtop++;
strcpy(idf,buffer);
bool flag=false;
if(!expecting) {
if(c!='=') {
for(int i=0;i<symbc;i++){
if(strcmp(symbols[i][0],idf)==0) {
fprintf(symtab,"%s",symbols[i][1]);
flag=true;
break;
}
}}
if(flag==false)
fprintf(symtab,"%s",buffer);
}
}
else {
fprintf(symtab,"%s",buffer);
}
bp=0;
buffer[bp]='\0';
isd=false;
}
if(c=='"') {
if(isStringLiteral){
buffer[bp]='\0';
}
isStringLiteral=!isStringLiteral;
fprintf(symtab,"%c",c);
c=fgetc(fp);
continue;
}
else if(c=='+'||c=='-'||c=='*'||c=='/') {
if(optop>0) {
if((opstack[optop-1]=='*'||opstack[optop-1]=='/')&&(c=='+'||c=='-')) {
if(idtop<2) {
showError(ln,buffer,c,1);
}
idtop--;
char operand[30];
strcpy(operand,idstack[idtop]);
float op1=0,op2=0;
if(isdigit(operand[0])) {
op1=atof(operand);
}
else {
int i=0;
for(i=0;i<symbc;i++) {
if(strcmp(symbols[i][0],operand)==0) {
op1=atof(symbols[i][1]);
break;
}
}
if(i==symbc){
showError(ln,buffer,c,2);
}
}
idtop--;
strcpy(operand,idstack[idtop]);
if(isdigit(operand[0])) {
op2=atof(operand);
}
else {
int i=0;
for(i=0;i<symbc;i++) {
if(strcmp(symbols[i][0],operand)==0) {
op2=atof(symbols[i][1]);
break;
}
}
if(i==symbc) {
showError(ln,buffer,c,3);
}
}
float ans=0;
if(opstack[optop-1]=='*') {
ans=op1*op2;
}
else if(opstack[optop-1]=='/') {
ans=op2/op1;
}
fp2=fopen("con.txt","w");
fprintf(fp2,"%f\n",ans);
fclose(fp2);
fgets(buf,30,fp3);
strcpy(idstack[idtop],buf) ;
idtop++;
optop--;
}
}
opstack[optop]=c;
optop++;
}
else if(isalpha(c)) {
if(isd) {
showError(ln,buffer,c,4);
}
buffer[bp]=c;
bp++;
if(bp==1) {
isd=false;
}
}
else if(isdigit(c)) {
buffer[bp]=c;
bp++;
if(bp==1) {
isd=true;
}
}
else if(c=='_') {
if(bp!=0) {
buffer[bp]=c;
bp++;
}
else {
showError(ln,buffer,c,5);
}
}
else if(c=='<'||c=='>'||c=='='||c=='!'){
fprintf(symtab,"%c",c);
char c2=fgetc(fp);
if(c2!='='&&c=='!') {
showError(ln,buffer,c,6);
c=c2;
continue;
}
else if(c!='=') {
c=c2;
expecting =true;
continue;
}
else {
c=c2;
idtop=0;
strcpy(idf2,idf);
expecting=true;
continue;
}
}
else if(c==';'||c==',') {
if(expecting) {
while(idtop>1) {
idtop--;
char operand[30];
strcpy(operand,idstack[idtop]);
float op1=0,op2=0;
if(isdigit(operand[0])) {
op1=atoi(operand);
}
else {
int i=0;
for(i=0;i<symbc;i++) {
if(strcmp(symbols[i][0],operand)==0) {
if(strcmp(symbols[i][1],"-")==0)
op1=0;
else
op1=atof(symbols[i][1]);
break;
}
}
if(i==symbc) {
showError(ln,buffer,c,7);
}
}
idtop--;
strcpy(operand,idstack[idtop]);
if(isdigit(operand[0])) {
op2=atoi(operand);
}
else {
int i=0;
for(i=0;i<symbc;i++) {
if(strcmp(symbols[i][0],operand)==0){
if(strcmp(symbols[i][1],"-")==0)
op1=0;
else
op2=atof(symbols[i][1]);
break;
}
}
if(i==symbc){
showError(ln,buffer,c,8);
}
}
float ans=0;
if(opstack[optop-1]=='*')
{
ans=op1*op2;
}
else if(opstack[optop-1]=='/')
{
ans=op2/op1;
}
else if(opstack[optop-1]=='+')
{
ans=op1+op2;
}
else if(opstack[optop-1]=='-')
{
ans=op2-op1;
}
fp2=fopen("con.txt","w");
fprintf(fp2,"%f",ans);
fclose(fp2);
fp3 = fopen( "con.txt","r");
fgets(buf,30,fp3);
fclose(fp3);
strcpy(idstack[idtop],buf) ;
idtop++;
optop--;
}
int targetInd=symbc;
if(!defining)
{
int j=0;
for(j=0;j<symbc;j++) {
if(strcmp(symbols[j][0],idf2)==0) {
targetInd=j;
break;
}
}
}
strcpy(symbols[targetInd][0],idf2);
if(isalpha(idstack[0][0]))
{
int i=0;
if(strcmp(symbols[targetInd][0],idf2)==0)
{
for(i=0;i<symbc;i++)
{
if(strcmp(symbols[i][0],idstack[0])==0)
{
strcpy(symbols[targetInd][1],symbols[i][1]);
break;
}
}
if(i==symbc)
{
showError(ln,buffer,c,12);
}
}
}
else{
int j=0;
strcpy(symbols[targetInd][1],idstack[0]);
}
if(defining){
symbc++;
}
if(c==';')
defining=false;
fprintf(symtab,"%s",symbols[targetInd][1]);
}
else if(defining) {
strcpy(symbols[symbc][0],idf);
strcpy(symbols[symbc][1],"0");
symbc++;
if(c==';')
defining=false;
}
expecting=false;
fprintf(symtab,"%c",c);
}
else if(c=='{'||c=='}'||c=='('||c==')'||c=='['||c==']')
{
fprintf(symtab,"%c",c);
}
else if(c=='\n')
{
fprintf(symtab,"%c",c);
}
{
showError(ln,buffer,c,10);
}
else
{
fprintf(symtab,"%c",c);
}
c=fgetc(fp);
}
fclose(symtab);
fclose(fp);
return 0;
}
INPUT.TXT:
float pi=22/5;
void main()
{
float c;
c=2*pi;
}
RESULT.TXT:
float pi=4.400000;
void main()
{
float c;
c=8.800000;
}
CON.TXT:
8.800000
EXPERIMENT NO: 08
CODE GENERATION
PROGRAM:
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
void main()
{
char code[10][30], str[20], opr[10];
int i = 0,k=0;
FILE *fp1,*fp2;
fp1=fopen("input.txt","r");
printf("file opened\n");
fp2=fopen("result.txt","w");
char op,fir[10],sec[10],res[10];
while(!feof(fp1)){
fscanf(fp1,"%c%s%s%s\n",&op,fir,sec,res);
printf("%c %s %s %s\n",op,fir,sec,res);
switch (op){
case '+':
strcpy(opr, "ADD ");
i=0;
break;
case '-':
strcpy(opr, "SUB");
i=0;
break;
case '*':
strcpy(opr, "MUL ");
i=1;
break;
case '/' :
strcpy(opr, "DIV");
i=1;
break;
}
if(strlen(fir)==2)
{
if (fir[1]=='1')
fprintf(fp2,"MOV AX,CH\n");
else if (fir[1]=='2')
fprintf(fp2,"MOV BX,CL\n");
else if (fir[1]=='3')
fprintf(fp2,"MOV CX,DH\n");
else if (fir[1]=='4')
fprintf(fp2,"MOV DX,DL\n");
}
else
fprintf(fp2,"MOV AX,[%s]\n", fir);
if(strlen(sec)==2)
{
if (sec[1]=='1')
fprintf(fp2,"MOV BX,CH\n");
else if (sec[1]=='2')
fprintf(fp2,"MOV BX,CL\n");
else if (sec[1]=='3')
fprintf(fp2,"MOV BX,DH\n");
else if (sec[1]=='4')
fprintf(fp2,"MOV BX,DL\n");
}
else
fprintf(fp2,"MOV BX,[%s]\n",sec);
if (i==0)
fprintf(fp2,"%sAX,BX\n", opr);
else
fprintf(fp2,"%s BX\n", opr);
if(strlen(res)==2)
{
if (res[1]=='1')
fprintf(fp2,"MOV CH,AX\n");
else if (res[1]=='2')
fprintf(fp2,"MOV CL,AX\n");
else if (res[1]=='3')
fprintf(fp2,"MOV DH,AX\n");
else if (res[1]=='4')
fprintf(fp2,"MOV DL,AX\n");
}
k++;
}
}
INPUT:
* b c t1
+ a[0] t2
= [1] d t3nc
RESULT.TXT
MOV AX,[b]
MOV BX,[c]
MUL BX
MOV CH,AX
MOV AX,[a[0]]
MOV BX,CL
ADD AX,BX
MOV BX,[d]
ADD AX,BX
EXPERIMENT NO: 04
PROGRAM:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
if (tmp == 1)
continue;
land += 1;
follow(ck);
ptr += 1;
printf("Follow(%c) = { ", ck);
followmat[p1][p2++] = ck;
for (i = 0 + km; i < m; i++) {
int lark = 0, chk = 0;
for (lark = 0; lark < p2; lark++) {
if (f[i] == followmat[p1][lark]) {
chk = 1;
break;
}
}
if (chk == 0) {
printf("%c, ", f[i]);
followmat[p1][p2++] = f[i];
}
}
printf("}\n\n");
km = m;
p1++;
}
}
}
void follow(char c) {
int i, j;
if (production[0][0] == c)
f[m++] = '$';
}
}
}
}
if (production[j][0] == c) {
if (production[j][2] == '#')
{
if (production[q1][q2] == '\0')
first[n++] = '#';
else if (production[q1][q2] != '\0' && (q1 != 0 || q2 != 0))
findfirst(production[q1][q2], q1,(q2 + 1));
else
first[n++] = '#';
}
else if (!isupper(production[j][2]))
first[n++] = production[j][2];
else
findfirst(production[j][2], j, 3);
}
}
}
void followfirst(char c, int c1, int c2) {
int k;
if (!(isupper(c)))
f[m++] = c;
else{
int i = 0, j = 1;
for (i = 0; i < count; i++){
if (firstmat[i][0] == c)
break;
}
while (firstmat[i][j] != '!'){
if (firstmat[i][j] != '#')
f[m++] = firstmat[i][j];
else {
if (production[c1][c2] == '\0')
follow(production[c1][0]);
else
followfirst(production[c1][c2], c1, c2 + 1);
}
j++;
}
}
}
OUTPUT:
First(E) = { i, }
First(T) = { i, }
Follow(E) = { $, +, }
Follow(T) = { $, +, }
EXPERIMENT NO: 07
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
FILE *file;
char filename[100];
struct Expression exp[MAX_EXPRESSIONS];
int n = 0;
char line[100];
// Input the filename
printf("Enter the filename: ");
scanf("%s", filename);
return 0;
}
OUTPUT:
Optimized expressions:
t1 = c * d
t1 = b + t2
t4 = a - t1
t5 = d = 10
t6 = c * d
t7 = e / t6