All Merged PDF CD
All Merged PDF CD
All Merged PDF CD
OUTPUT :-
OUTPUT:
2)Write a C program to recognize strings
under 'a*', 'a*b+', 'abb'?
CODE :-
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char s[20],c;
int state=0,i=0;
printf("\n Enter a
string:"); gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else
if(c=='b')
state=2;
else
state=6;
break;
case 1: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;
case 2: c=s[i++];
if(c=='a')
state=6;
else
if(c=='b')
state=2;
else
state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else
if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;
else
if(c=='b')
state=5;
else
state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else
if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not
recognised.",s); exit(0);
}
}
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule
'a*b+'",s); else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}
OUTPUT :-
#include<string.h>
int ip = 0;
int main() {
char k;
int l;
ip = 0;
scanf("%s", s);
E();
if (s[ip] == '$')
1); else
return 0;
void E() {
T();
E1();
return;
void E1() {
if (s[ip] == '+')
{ ip++;
T();
E1();
return;
void T() {
F();
T1();
return;
void T1() {
if (s[ip] == '*')
{ ip++;
F();
T1();
return;
void F() {
if (s[ip] == '(')
{ ip++;
E();
if (s[ip] ==
')') ip++;
} else
if (s[ip] ==
'i') ip++;
else
printf("\n id expected");
return;
}
Execution of code:
Output:
Code for Three address code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct three {
s[30];
void main() {
"t"; int i = 0, j = 1,
f2;
clrscr();
f1 = fopen("sum.txt", "r");
f2 = fopen("out.txt", "w");
EOF) len++;
strcat(d2, d1);
strcpy(s[j].temp, d2);
strcpy(d1, "");
strcpy(d2, "t");
if (!strcmp(s[3].data, "+")) {
j++;
} else if (!strcmp(s[3].data, "-")) {
j++;
strcat(d2, d1);
strcpy(s[j].temp, d2);
strcpy(d1, "");
strcpy(d2, "t");
j++;
1].temp); fclose(f1);
fclose(f2);
getch();
#include<iostream>
char stack[30];
int top=-1;
void push(char c)
top++;
stack[top]=c;
char pop()
char c;
if(top!=-1)
c=stack[top];
top--;
return c;
return'x';
void printstat()
int i; printf("\n\t\t\
t $");
for(i=0;i<=top;i++)
printf("%c",stack[i]);
{
int i,j,k,l;
char s1[20],s2[20],ch1,ch2,ch3;
scanf("%s",s1);
l=strlen(s1);
j=0;
printf("\n\t\t $");
for(i=0;i<l;i++)
s1[i]=' ';
s1[i+1]='E';
printstat(); printf("id");
push('E');
printstat();
push(s1[i]);
printstat();
printstat();
l=strlen(s2);
while(l)
ch1=pop();
if(ch1=='x')
{
printf("\n\t\t\t $");
break;
if(ch1=='+'||ch1=='/'||ch1=='*'||ch1=='-')
ch3=pop(); if(ch3!
='E')
printf("errror");
exit(0);
else
push('E');
printstat();
ch2=ch1;
Sy
stem("PAUSE");
6) IDENTIFICATION OF IDENTIFIERS :-
/ C++ implementation of the approach
#include <bits/stdc++.h>
// is a valid identifier
|| str[0] == '_'))
return false;
|| str[i] == '_'))
return false;
// String is a valid
// Driver code
int main()
{
int n = str.length();
if (isValid(str, n))
else
return 0;
7) IDENTIFICATION OF OPERATOR :-
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
+) { if (ch == operators[i]) {
return true;
return false;
}
int main() {
char expression[100];
strlen(expression); i++) {
if (isOperator(expression[i])) {
printf("\n");
return 0;
#include<string.h>
struct op
{ char l;
char r[20];
} op[10], pr[10];
int main() {
int a, i, k, j, n, z = 0, m,
char temp,
t; char
*tem;
scanf("%d", &n);
{ printf("left: ");
scanf("
%c",&op[i].l);
printf("\tright: ");
scanf("%s", op[i].r);
}
printf("Intermediate Code\n");
{ temp = op[i].l;
p = strchr(op[j].r, temp);
if (p != NULL) {
pr[z].l = op[i].l;
strcpy(pr[z].r, op[i].r);
z++;
+;
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 != NULL) {
t = pr[j].l;
pr[j].l = pr[m].l;
for (i = 0; i < z; i+
+) { l =
strchr(pr[i].r, t); if
(l != NULL) {
a = l - pr[i].r;
pr[i].r[a] = pr[m].l;
q = strcmp(pr[i].r, pr[j].r);
pr[i].l = '\0';
strcpy(pr[i].r, "");
printf("Optimized Code\n");
if (pr[i].l != '\0') {
printf("%c = %s\n", pr[i].l, pr[i].r);
return 0;
Output :-
Code for Code generation:-
#include <stdio.h>
char stk[100];
int cnt = 0;
{ stk[++stktop] =
pchar;
char pop() {
return stk[stktop--];
char oper;
if (char1 ==
'+') oper =
'A';
else if (char1
oper = 'M';
oper = 'D';
return oper;
{ int ret;
if (check != '+' && check != '-' && check != '*' && check != '/' && check != '@') { push(+
+cnt);
if (stktop > 0)
printf("ST $%d\n",cnt);
ret = 1;
} else
ret =
0;
return ret;
{ cnt = 0;
stktop = -1;
if ((msg[i] >= 'A' && msg[i] <= 'Z') || (msg[i] >= 'a' && msg[i] <= 'z'))
printf("(A + B) - C\n");
push(msg[i]);
else {
op1 = pop();
op2 = pop();
operation = checkoperation(msg[i]); val
= checknstore(msg[i + 1]);
while (val == 0)
{ op1 = pop();
cnt--;
operation = checkoperation(msg[++i]); if
{ operation = 'A';
= checknstore(msg[i + 1]);
return 0;
Input :-
Output :-
1) Write a C/C++ program for construction of
predictive parsing table
Code :-
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char prol[7][10]={"S","A","A","B","B","C","C"};
char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"};
char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B-
>@","C->Cc","C->@"}; char
first[7][10]={"abcd","ab","cd","a@","@","c@","@"}; char
follow[7][10]={"$","$","$","a$","b$","c$","d$"};
char table[5][6][10];
int numr(char c)
{
switch(c)
{
case 'S': return
0; case 'A':
return 1; case
'B': return 2;
case 'C': return
3; case 'a':
return 0; case
'b': return 1;
case 'c': return
2; case 'd':
return 3;
case '$': return 4;
}
return(2);
}
int main(int argc, char *argv[])
{
int i,j,k; for(i=0;i<5;i+
+) for(j=0;j<6;j++)
strcpy(table[i][j]," ");
printf("\nThe following is the predictive parsing table for
the following grammar:\n");
for(i=0;i<7;i++)
printf("%s\n",prod[i]);
printf("\nPredictive parsing table is\n");
fflush(stdin);
for(i=0;i<7;i++)
{
k=strlen(first[i]);
for(j=0;j<10;j++)
if(first[i][j]!='@')
strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]);
}
for(i=0;i<7;i++)
{
if(strlen(pror[i])==1)
{
if(pror[i][0]=='@')
{
k=strlen(follow[i]);
for(j=0;j<k;j++)
strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])+1],prod[i])
;
}
}
}
strcpy(table[0][0]," ");
strcpy(table[0][1],"a");
strcpy(table[0][2],"b");
strcpy(table[0][3],"c");
strcpy(table[0][4],"d");
strcpy(table[0][5],"$");
strcpy(table[1][0],"S");
strcpy(table[2][0],"A");
strcpy(table[3][0],"B");
strcpy(table[4][0],"C");
printf("\n \n");
for(i=0;i<5;i++)
for(j=0;j<6;j++)
{
printf("%-10s",table[i][j]);
if(j==5)
printf("\n \n");
}
system("PAUSE"); // statement in Bloodshed dev c++
IDE requirement
}
Code Execution and Output :-
10 ) Write a C/C++ program to construct DFA from
NFA
Code :-
#include <iostream>
#include<stdio.h>
#include<ctype.h>
#include<process.h>
using namespace std;
typedef struct
{
int num[10],top;
}
stack;
stack s;
int mark[16][31],e_close[16][31],n,st=0;
char data[15][15];
void push(int a)
{
s.num[s.top]=a;
s.top=s.top+1;
}
int pop()
{
int a;
if(s.top==0)
return(-1);
s.top=s.top-1;
a=s.num[s.top];
return(a);
}
void epi_close(int s1,int s2,int c)
{
int i,k,f;
for(i=1;i<=n;i++)
{
if(data[s2][i]=='e')
{
f=0;
for(k=1;k<=c;k++)
if(e_close[s1][k]==i)
f=1;
if(f==0)
{ c+
+;
e_close[s1][c]=i;
push(i);
}
}
}
while(s.top!=0) epi_close(s1,pop(),c);
}
int move(int sta,char c)
{
int i; for(i=1;i<=n;i+
+)
{
if(data[sta][i]==c)
return(i);
}
return(0);
}
void e_union(int m,int n)
{
int i=0,j,t; for(j=1;mark[m]
[i]!=-1;j++)
{
while((mark[m][i]!=e_close[n][j])&&(mark[m][i]!=-1))
i++;
if(mark[m][i]==-1)
mark[m][i]=e_close[n][j];
}
}
int main(int argc, char *argv[])
{
int i,j,k,Lo,m,p,q,t,f;
printf("\n enter the NFA state table
entries:"); scanf("%d",&n);
printf("\n");
for(i=0;i<=n;i++)
printf("%d",i);
printf("\n");
for(i=0;i<=n;i++)
printf("-----");
printf("\n");
for(i=1;i<=n;i++)
{
printf("%d|",i);
fflush(stdin);
for(j=1;j<=n;j++)
scanf("%c",&data[i][j]);
}
for(i=1;i<=15;i++)
for(j=1;j<=30;j++)
{
e_close[i][j]=-1;
mark[i][j]=-1;
}
for(i=1;i<=n;i++)
{
e_close[i][1]=i;
s.top=0;
epi_close(i,i,1);
}
for(i=1;i<=n;i++)
{
for(j=1;e_close[i][j]!=-1;j++)
for(k=2;e_close[i][k]!=-1;k++)
if(e_close[i][k-1]>e_close[i][k])
{
t=e_close[i][k-1]; e_close[i]
[k-1]=e_close[i][k];
e_close[i][k]=t;
}
}
printf("\n the epsilon closures are:"); for(i=1;i<=n;i+
+)
{
printf("\n E(%d)={",i);
for(j=1;e_close[i][j]!=-1;j++)
printf("%d",e_close[i][j]);
printf("}");
}
j=1;
while(e_close[1][j]!=-1)
{
mark[1][j]=e_close[1][j];
j++;
}
st=1;
printf("\n DFA Table is:");
printf("\n a b ");
printf("\n ");
for(i=1;i<=st;i++)
{
printf("\n{");
for(j=1;mark[i][j]!=-1;j++)
printf("%d",mark[i][j]);
printf("}");
while(j<7)
{
printf(" ");
j++;
} for(Lo=1;Lo<=2;Lo+
+)
{
for(j=1;mark[i][j]!=-1;j++)
{
if(Lo==1)
t=move(mark[i][j],'a');
if(Lo==2)
t=move(mark[i][j],'b');
if(t!=0)
e_union(st+1,t);
}
for(p=1;mark[st+1][p]!=-1;p++)
for(q=2;mark[st+1][q]!=-1;q++)
{
if(mark[st+1][q-1]>mark[st+1][q])
{
t=mark[st+1][q]; mark[st+1]
[q]=mark[st+1][q-1];
mark[st+1][q-1]=t;
}
}
f=1;
for(p=1;p<=st;p++)
{ j=
1;
while((mark[st+1][j]==mark[p][j])&&(mark[st+1][j]!=-1))
j++;
if(mark[st+1][j]==-1 && mark[p][j]==-1)
f=0;
}
if(mark[st+1][1]==-1)
f=0;
printf("\t{");
for(j=1;mark[st+1][j]!=-1;j++)
{
printf("%d",mark[st+1][j]);
}
printf("}\t");
if(Lo==1)
printf(" ");
if(f==1) st+
+; if(f==0)
{
for(p=1;p<=30;p++)
mark[st+1][p]=-1;
}
}
}
system("PAUSE");
return EXIT_SUCCESS;
}