Sei sulla pagina 1di 48

Register Number: RA161103010165

EX NO: 1(a)

DATE: 24-01-2019
Stimulation of DFA

AIM : To write a program for stimulation of DFA

ALGORITHM:
Step 1: Create a menu for getting input from user.
Step 2: Get the start state, final state, input symbols as input.
Step 3: Maintain a stack required for transfer.
Step 4: Finally the required output displayed has DFA valid or not.

PROGRAM :

#include<stdio.h>
#include<conio.h>

int n;

int check(char,int );
int dfa[10][10];
char c[10], string[10];
int main()
{
int states, finals;
int f[10];
int i,j,s=0,final=0;
printf("enter the number of states that your dfa consist of \n");
scanf("%d",&states);
printf("enter the number of input symbol that dfa have \n");
scanf("%d",&n);
printf("\nenter input symbols\t");
for(i=0; i<n; i++)
{
printf("\n\n %d input\t", i+1);
printf("%c",c[i]=getch());
}
printf("\n\nenter number of final states\t");
scanf("%d",&finals);

for(i=0;i<finals;i++)
{
printf("\n\nFinal state %d : q",i+1);
scanf("%d",&f[i]);
}

1
printf("----------------------------------------------------------------------");
printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");
for(i=0; i<n; i++)
{
for(j=0; j<states; j++)
{
printf("\n(q%d , %c ) = q",j,c[i]);
scanf("%d",&dfa[i][j]);
}
}

do
{ i=
0;
printf("\n\nEnter Input String.. ");
scanf("%s",string);

while(string[i]!='\0')
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<finals ;i++)
if(f[i] ==s )
final=1;
if(final==1)
printf("\n valid string");
else
printf("invalid string");
getch();

printf("\nDo you want to continue.? \n(y/n) ");


}
while(getch()=='y');

getch();
}

int check(char b,int d)


{
int j;
for(j=0; j<n; j++)
if(b==c[j])
return(dfa[d][j]);
return 0;
}
OUTPUT

RESULT:

Thus the given program of stimulation of DFA is executed successfully.


EX NO: 1(b)

DATE: 31-01-19
ε-NFA TO DFA CONVERSION

AIM : To write a program for construction of minimized DFA from a given regular
expression using C.

ALGORITHM:

Step 1: Get the start state, final state, input symbols as input and also give the edge value
for each state.

Step 2: Maintain a stack required for transition from one state to other state.

Step 3: Using Pop or push function perform the insertion and deletion of elements when
required.

Step 4: Finally conversion has been made to change from regular expression to minimized
DFA and the output is displayed as DFA transition table.

PROGRAM :

#include<stdio.h> int stackA[100],stackB[100],c[100],Cptr=-


#include<string.h> 1,Aptr=-1,Bptr=-1;
#define STATES 50 struct state States[10];
struct Dstate char temp[STATES+1],inp[10];
{ int nos,noi,nof,j,k,nods=-1;
char name; void pushA(int z)
char StateString[STATES+1]; {
char trans[10]; stackA[++Aptr]=z;
int is_final; }
}Dstates[50]; void pushB(int z)
struct tran {
{ stackB[++Bptr]=z;
char sym; }
int tostates[50]; int popA()
int notran; {
}; return stackA[Aptr--];
struct state }
{ void copy(int i)
int no; {
struct tran tranlist[50]; char temp[STATES+1]=" ";
}; int k=0;
Bptr=-1;
strcpy(temp,Dstates[i].StateString); for(i=0;i<=Bptr;i++)
while(temp[k]!='\0') {
{ temp[i]=stackB[i]+'0';
pushB(temp[k]-'0'); }
k++; temp[i]='\0';
} }
} void display_DTran()
int popB() {
{ int i,j;
return stackB[Bptr--]; printf("\n\t\t DFA transition table");
} printf("\n\t\t ------------------------------------
int peekA() ---------- ");
{ printf("\n States \tString \tInputs\n");
return stackA[Aptr]; for(i=0;i<noi;i++)
} {
int peekB() printf("\t %c",inp[i]);
{ }
return stackA[Bptr]; printf("\n\t --------------------------------------
} ----------- ");
int seek(int arr[],int ptr,int s) for(i=0;i<nods;i++)
{ {
int i; if(Dstates[i].is_final==0)
for(i=0;i<=ptr;i++) printf("\n%c",Dstates[i].name);
{ else printf("\n*
%c",Dstates[i].name);
if(s==arr[i]) printf("\t%s",Dstates[i].StateString);
return 1; for(j=0;j<noi;j++)
} {
return 0; printf("\t%c",Dstates[i].trans[j]);
} }
void sort() }
{ printf("\n");
int i,j,temp; }
for(i=0;i<Bptr;i++) void move(int st,int j)
{ {
for(j=0;j<(Bptr-i);j++) int ctr=0;
{ while(ctr<States[st].tranlist[j].notran)
if(stackB[j]>stackB[j+1]) {
{ pushA(States[st].tranlist[j].tostates[ctr++])
temp=stackB[j]; ;
stackB[j]=stackB[j+1]; }
stackB[j+1]=temp; }
} void lambda_closure(int st)
} {
} int ctr=0,in_state=st,curst=st,chk;
} while(Aptr!=-1)
void tostring() {
{ curst=popA();
int i=0; ctr=0;
sort(); in_state=curst;
while(ctr<=States[curst].tranlist[noi].notra {
n) for(j=0;j<=noi;j++)
{ {
chk=seek(stackB,Bptr,in_state); States[i].tranlist[j].sym=inp[j];
if(chk==0) k=0;
pushB(in_state); ans='y';
in_state=States[curst].tranlist[noi].tostates[ while(ans=='y')
ctr++]; {
chk=seek(stackA,Aptr,in_state); printf("move(%d,%c);",i,inp[j]);
if(chk==0 &&
ctr<=States[curst].tranlist[noi].notran) scanf("%d",&States[i].tranlist[j].tostates[k
pushA(in_state); ++]);
} if((States[i].tranlist[j].tostates[k-
} 1]==-1))
} {
int main() k--;
{ ans='n';
char g1inp[100]; break;
int i,final[20],start,fin=0; }
char c,ans,st[20]; }
printf("\n Enter no of states in States[i].tranlist[j].notran=k;
NFA:"); }
scanf("%d",&nos); }
for(i=0;i<nos;i++) i=0;nods=0,fin=0;
{ pushA(start);
States[i].no=i; lambda_closure(peekA());
} tostring();
printf("\n Enter the start states:"); Dstates[nods].name='A';
scanf("%d",&start); nods++;
printf("Enter the no of final strcpy(Dstates[0].StateString,temp);
states:"); while(i<nods)
scanf("%d",&nof); {
printf("Enter the final states:\n"); for(j=0;j<noi;j++)
for(i=0;i<nof;i++) {
scanf("%d",&final[i]); fin=0;
printf("\n Enter the no of input copy(i);
symbols:"); while(Bptr!=-1)
scanf("%d",&noi); {
c=getchar(); move(popB(),j);
printf("Enter the input }
symbols:\n");
for(i=0;i<noi;i++) while(Aptr!=-1)
{ lambda_closure(peekA());
scanf("%c",&inp[i]); tostring();
c=getchar(); for(k=0;k<nods;k++)
} {
g1inp[i]='e'; if((strcmp(temp,Dstates[k].StateString)==
printf("\n Enter the transitions:(-1 0))
to stop)\n"); {
for(i=0;i<nos;i++) Dstates[i].trans[j]=Dstates[k].name;
break;
}
}
if(k==nods)
{
nods++;
for(k=0;k<nof;k++)
{
fin=seek(stackB,Bptr,final[k]);
if(fin==1)
{
Dstates[nods-1].is_final=1;
break;
}
}
strcpy(Dstates[nods-1].StateString,temp);
Dstates[nods-1].name='A'+nods-1;
Dstates[i].trans[j]=Dstates[nods-1].name;
}
} i+
+;
}
display_DTran();
return 0;
}

OUTPUT
RESULT: The given program is successfully executed.

EX NO: 2

DATE: 07-02-2019
Lexical Analysis

AIM : To implement a lexical analyzer for a simple C program.

ALGORITHM:

1. Save the program to be analyzed in a text file,


2. Create a keyword function, which simply checks if the string obtained from the text
file is a keyword or an identifier,
3. Open the file in the main function to be analyzed character by character,
4. If the character is an operator, print so,
5. If the string obtained after the white space or newline character matches the keyword,
print it’s a keyword,
6. If the string is identifier, print so.

PROGRAM :

#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;
}
int main(){
char ch, buffer[15], operators[] = "+-*/%=";
FILE *fp;
int i,j=0;
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;
}
else 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 indentifier\n", buffer);
}
}
fclose(fp);
return 0;
}

OUTPUT
RESULT:

The following program was successfully compiled and executed.

EX NO: 3
Regular Expression to DFA
DATE: 13-02-2019 Conversion

AIM : To write a C program to convert the given RE to NFA.

ALGORITHM:

1. Accept the regular expression string from the user,


2. Using the Thompson’s Algorithm, implement operator precedence,
3. Handle the parentheses,
4. Handle the kleene stars,
5. Perform concatenation,
6. Followed by alternation.

PROGRAM :

#include<stdio.h>
if(reg[i]=='e'& reg[i+1]!='/'&
#include<string.h> reg[i+1]!='*'){
#include<conio.h> q[j][2]=j+1;
void main(){ j++;
char reg[20]; }
int q[20][3],i,j,len,a,b; if(reg[i]=='a'&reg[i+1]=='/'&reg[i+2]=='b')
for(a=0;a<20;a++) { q[j][2]=((j+1)*10)+

{ for(b=0;b<3;b++) (j+3); j++;

{ q[a][b]=0; q[j][0]=j+1;

} j++; q[j]

} [2]=j+3; j+

printf("Regular expression: \n"); +; q[j]

scanf("%s",reg); [1]=j+1; j+

len=strlen(reg); +; q[j]

i=0; [2]=j+1; j+

j=1; +;

while(i<len){ i=i+2;

if(reg[i]=='a'& reg[i+1]!='/'&reg }
[i+1]!='*'){ if(reg[i]=='b'&reg[i+1]=='/'&reg[i+2]=='a')
q[j][0]=j+1; { q[j][2]=((j+1)*10)+

j++; (j+3); j++;

} q[j][1]=j+1;

if(reg[i]=='b'& reg[i+1]!='/'& j++; q[j]


reg[i+1]!='*'){ [2]=j+3; j+
q[j][1]=j+1; +;
j++;
}
q[j][0]=j+1; printf("\n q[%d,a]-->%d",i,q[i][0]);
j++; q[j] if(q[i][1]!=0)
[2]=j+1; j+ printf("\n q[%d,b]-->%d",i,q[i][1]);
+; if(q[i][2]!=0){
i=i+2; if(q[i][2]<10)
} printf("\n q[%d,e]-->%d",i,q[i][2]);
if(reg[i]=='a'&reg[i+1]=='*'){ else
q[j][2]=((j+1)*10)+(j+3); printf("\n q[%d,e]-->%d &
j++; %d",i,q[i][2]/10,q[i][2]%10);

q[j][0]=j+1; }

j++; }

q[j][2]=((j+1)*10)+(j-1); getch();

j++;
}
if(reg[i]=='b'&reg[i+1]=='*'){
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][1]=j+1;
j++;
q[j][2]=((j+1)*10)+(j-1);
j++;
}
if(reg[i]==')'&reg[i+1]=='*'){
q[0][2]=((j+1)*10)+1;
q[j][2]=((j+1)*10)+1;
j++;
} i+
+;
}
printf("Transition function \n");
for(i=0;i<=j;i++){ if(q[i][0]!
=0)
OUTPUT

RESULT:

The following program was successfully compiled and executed.

EX NO: 4
Recursive Descent Parsing
DATE: 13-02-2019

AIM : To write a c program to convert the given Recursive Descent Parsing

ALGORITHM:

Step 1: Give the grammar

Step 2: E -> TE'

E' -> +TE' | ε

T -> FT'

T' -> *FT' | ε

F -> (E) | id

Step 3: Check the input string which accepts this grammar.

PROGRAM :

#include<stdio.h>
#include<string.h>
#include<ctype.h>
char input[10];
int i,error;
void E();
void T();
void Eprime();
void Tprime();
void F();
main()
{
i=0;
error=0;
printf("Recursive descent parsing for the following grammar\n");
printf("E->TE'\nE'->+TE'/@\nT->FT'\nT'->*FT'/@\nF->(E)/ID\n");
printf("Enter an arithmetic expression: ");
gets(input);
E();
if(strlen(input)==i&&error==0)
printf("\nString is Accepted..!!!\n");
else printf("\nString is Rejected..!!!\n");
}
void E()
{
T();
Eprime();
}
void Eprime()
{
if(input[i]=='+')
{ i+
+;
T();
Eprime();
}
}
void T()
{
F();
Tprime();
}
void Tprime()
{
if(input[i]=='*')
{
i++;
F();
Tprime();
}
}
void F()
{
if(isalnum(input[i]))i++;
else if(input[i]=='(')
{ i+
+;
E();
if(input[i]==')')
i++;

else error=1;
}

else error=1;
}

OUTPUT
RESULT:

The following program was successfully compiled and executed.


EX NO: 5

DATE: 20-02-2019
FIRST & FOLLOW Set

AIM : To write a C program to find the FIRST and the FOLLOW of a grammar.

ALGORITHM:

1. Accept the number of productions,


2. Accept the production values,
3. Find the FIRST of the values entered,
4. Find the FOLLOW of the values entererd.

PROGRAM :

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c); int
main(){
int i,z;
char c,ch;
printf("Enter the no of prooductions:\n");
scanf("%d",&n);
printf("Enter the productions:\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do{
m=0;
printf("Enter the elemets whose fisrt & follow is to be found:");
scanf("%c",&c);
first(c);
printf("First(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
strcpy(f," ");
m=0;
follow(c);
printf("Follow(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
printf("Continue(0/1)?");
scanf("%d%c",&z,&ch);
}while(z==1);
return(0);
}
void first(char c){
int k; if(!
isupper(c))
f[m++]=c;
for(k=0;k<n;k++)
{ if(a[k]
[0]==c){
if(a[k][2]=='$')
follow(a[k][0]);
else if(islower(a[k][2]))
f[m++]=a[k][2];
else first(a[k][2]);
}
}
}
void follow(char c){
if(a[0][0]==c)
f[m++]='$';
for(i=0;i<n;i++)
{ for(j=2;j<strlen(a[i]);j+
+){
if(a[i][j]==c){
if(a[i][j+1]!='\0')
first(a[i][j+1]); if(a[i]
[j+1]=='\0' && c!=a[i][0])
follow(a[i][0]);
}
}
}
}
OUTPUT
RESULT:

The following program was successfully compiled and executed.

EX NO: 6
Shift Reduce Parsing
DATE:

AIM : To write a C program to implement Shift Reduce Parser.

ALGORITHM:

1. Assume a grammar,
2. Accept the input string,
3. Print the actions.

PROGRAM :

#include<stdio.h>
stk[i+1]=a[j+1];
#include<conio.h>
stk[i+2]='\0';
#include<string.h>
a[j]=' ';
int k=0,z=0,i=0,j=0,c=0;
a[j+1]=' ';
char a[16],ac[20],stk[15],act[10];
printf("\n$%s\t%s$\t%sid",stk,a,act);
void check();
check();
int main()
}
{
else
puts("GRAMMAR is E->E+E \n E-
>E*E \n E->(E) \n E->id"); {

puts("enter input string "); stk[i]=a[j];

gets(a); stk[i+1]='\0';

c=strlen(a); a[j]=' ';

strcpy(act,"SHIFT->"); printf("\n$%s\t%s$\t%ssymbols",stk,a,act)
;
puts("stack \t input \t action");
check();
for(k=0,i=0; j<c; k++,i++,j++)
}
{
}
if(a[j]=='i' && a[j+1]=='d')
getch();
{
}
stk[i]=a[j];
void check()
{ {
strcpy(ac,"REDUCE TO E"); stk[z]='E';
for(z=0; z<c; z++) stk[z+1]='\0';
if(stk[z]=='i' && stk[z+1]=='d') stk[z+1]='\0'; printf("\n$%s\t
{ %s$\t%s",stk,a,ac); i=i-2;
stk[z]='E'; }
stk[z+1]='\0'; printf("\n$%s\t%s$\t }
%s",stk,a,ac); j++;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='+' &&
stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0'; printf("\n$%s\t
%s$\t%s",stk,a,ac); i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='*' &&
stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0'; printf("\n$%s\t
%s$\t%s",stk,a,ac); i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='(' && stk[z+1]=='E' &&
stk[z+2]==')')
OUTPUT

RESULT: The following program was successfully compiled and executed.

EX NO: 7

DATE:
Leading and Trailing Sets

AIM :

To write a C program to find the leading and trailing terminals of a given grammar.

ALGORITHM:

1. First include the necessary package,


2. Declare the array array and their functions,
3. Accept the number of productions,
4. Accept the different productions,
5. Find and print the leading terminals and the trailing terminals of the grammar.

PROGRAM :

#include<iostream>
char for(i=0;i<nt;i++){
#include<string.h> s[50],NT[10],T[10],st[50]
using namespace std; ,l[10][10],tr[50][50];
if(NT[i]==a)
int nt,t,top=0; int searchnt(char a){
int count=-1,i; return i;
}
return count; push(T[b]);
if(searchter(pr[i][j])==-1)
}
push(NT[a]);
int searchter(char a){ T[t++]=pr[i][j];
}
int count=-1,i; }
}
for(i=0;i<t;i++){ }
if(T[i]==a) }
int main(){
for(i=0;i<nt;i++){
return i; int i,s,k,j,n;
} char for(j=0;j<t;j++)
pr[30][30],b,c;
return count;
cout<<"Enter the l[i][j]='f';
} no of productions:";
void push(char a){ }
cin>>n;
s[top]=a; for(i=0;i<nt;i++){
cout<<"Enter the
top++; productions one by
one\n"; for(j=0;j<t;j++)
}
for(i=0;i<n;i++)
char pop(){ tr[i][j]='f';
cin>>pr[i];
top--; }
nt=0;
return s[top]; for(i=0;i<nt;i++){
t=0;
}
for(i=0;i<n;i++){ for(j=0;j<n;j++){
void installl(int a,int b){
if(l[a][b]=='f'){
if((searchnt(pr[i][0]))==- if(NT[(searchnt(pr[j][0]))]
l[a][b]='t';
1) ==NT[i]){
push(T[b]);
NT[nt++]=pr[i][0]; if(searchter(pr[j][3])!=-1)
push(NT[a]);
}
}
for(i=0;i<n;i++){
} installl(searchnt(pr[j][0]),
searchter(pr[j][3]));
void installt(int a,int b){
for(j=3;j<strlen(pr[i]);j++
if(tr[a][b]=='f'){ ){ else{
tr[a][b]='t';

if(searchnt(pr[i][j])==-1){
for(k=3;k<strlen(pr[j]);k+
+){
cout<<"Leading["<<NT[i
]<<"]"<<"\t{"; if(searchnt(pr[j][k])==-
1){
if(searchnt(pr[j][k])==- for(j=0;j<t;j++){
1){

if(l[i][j]=='t') installt(searchnt(pr[j][0]),
searchter(pr[j][k]));
installl(searchnt(pr[j][0]), cout<<T[j]<<",";
searchter(pr[j][k]));
}
break;

cout<<"}\n";
break; }
}
} }

} top=0; }

for(i=0;i<nt;i++){ }
}
} }
for(j=0;j<n;j++){ }
}
while(top!=0){
}
if(NT[searchnt(pr[j][0])]= b=pop();
while(top!=0){ =NT[i]){ c=pop();
b=pop();
c=pop(); for(s=0;s<n;s++){
if(searchter(pr[j][strlen(pr
[j])-1])!=-1)
for(s=0;s<n;s++){ if(pr[s][3]==b)

if(pr[s][3]==b) installt(searchnt(pr[j][0]),
searchter(pr[j][strlen(pr[j] installt(searchnt(pr[s][0]),
)-1])); searchter(c));
installl(searchnt(pr[s][0]), }
searchter(c)); else{
}
}
for(i=0;i<nt;i++){
} for(k=(strlen(pr[j])-
for(i=0;i<nt;i++){ 1);k>=3;k--){
cout<<"Trailing["<<NT[i
]<<"]"<<"\t{";
for(j=0;j<t;j++){

if(tr[i][j]=='t')

cout<<T[j]<<",";
}

cout<<"}\n";
}
}

OUTPUT

RESULT: The following program was successfully compiled and executed.

EX NO: 8(a)

DATE:
Left Recursion

AIM :

To a write a C program to eliminate left recursion from a grammar.

ALGORITHM:

1. Accept a grammar as input production,


2. For each non-terminal Ai,
3. Repeat until and iteration leaves the grammar unchanged.
4. For each rule, Ai ->αi, αi being a sequence of terminals and non-terminals,
5. If αi begins with a non-terminal Aj and j<1,
6. Let βi be αi without its leading Aj,
7. Remove the rule Ai-> αi,
8. For each rule Ai-> αj, Add the rule Ai-> αiβi,
9. Remove direct left recursion for Ai.

PROGRAM :

#include<stdio.h>
#include<string.h>
#define SIZE 10
int main () {
char non_terminal;
char beta,alpha;
int num;
int i;
char production[10][SIZE];
int index=3; /* starting of the string following "->" */
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\'|e\n",non_terminal,alpha,beta,non_terminal);
}
else
printf(" can't be reduced\n");
}
else
printf(" is not left recursive.\n");
index=3;
}
return 0;
}

OUTPUT

RESULT:

The following program was successfully compiled and executed.

EX NO: 8(b)

DATE:
Left Factoring

AIM :

To write a C program to perform LEFT FACTORING on a given grammar.

ALGORITHM:

1. Accept any production from the user,


2. Use another character array to store one part of the grammar and another character to
store the remaining.
3. Later compare the derived character with each other and copy the matching character to

another array,

4. Print the result

PROGRAM :

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[10],a1[10],a2[10],a3[10],a4[10],a5[10];
int i,j=0,k,l;
printf("enter any productions A->");
gets(a);
for(i=0;a[i]!='|';i++,j++)
a1[j]=a[i];
a1[j]='\0';
for(j=++i,i=0;a[j]!='\0';j++,i++)
a2[i]=a[j];
a2[i]='\0';
k=0;
l=0;
for(i=0;i<strlen(a1)||i<strlen(a2);i++)
{
if(a1[i]==a2[i])
{
a3[k]=a1[i];
k++;
}
else
{
a4[l]=a1[i];
a5[l]=a2[i];
l++;
}}
a3[k]='A';
a3[++k]='\0';
a4[l]='|';
a5[l]='\0';
a4[++l]='\0';
strcat(a4,a5); printf("\n
A->%s'",a3);
printf("\n A'->e|%s",a4);
return 0;
}

OUTPUT

RESULT:

The following program was successfully compiled and executed.

EX NO: 9

DATE:
Predictive Parsing Table

AIM :

To write a C program to construct a Predictive Parser for a grammar.

ALGORITHM:

1. Accept the number of coordinates,


2. Accept the productions,
3. Print the FIRST of the grammar,
4. Print the FOLLLOW of the grammar,
5. Finally print if the string is accepted or not.

PROGRAM :

#include<stdio.h> { l=0;
#include<conio.h> while(st[i][j]!=st[a][0]) k1:while((st[i][0]!=st[k][j
#include<string.h> { ])&&(k<n))
int main() a++; {
{ } if(st[k][j]=='\0')
char b=0; {
fin[10][20],st[10][20],ft[2 while(ft[a][b]!='\0') k++;
0][20],fol[20][20]; { j=2;
int for(m=0;m<l;m++) }
a=0,e,i,t,b,c,n,k,l=0,j,s,m, { j++;
p; if(ft[i][m]==ft[a][b]) }
printf("enter the no. of goto s2; j=j+1;
coordinates\n"); } if(st[i][0]==st[k][j-1])
scanf("%d",&n); ft[i][l]=ft[a][b]; {
printf("enter the l=l+1; if((st[k][j]!='|')&&(st[k][j]
productions in a s2:b=b+1; !='\0'))
grammar\n"); } {
for(i=0;i<n;i++) } a=0;
scanf("%s",st[i]); } if(!((st[k][j]>64)&&(st[k]
for(i=0;i<n;i++) while(st[i][j]!='\0') [j]<91)))
fol[i][0]='\0'; { {
for(s=0;s<n;s++) if(st[i][j]=='|') for(m=0;m<l;m++)
{ { {
for(i=0;i<n;i++) j=j+1; if(fol[i][m]==st[k][j])
{ goto l1; goto q3;
j=3; } }
l=0; j=j+1; fol[i][l]=st[k][j];
a=0; } l++;
l1:if(!((st[i][j]>64)&&(st[ ft[i][l]='\0'; q3:;
i][j]<91))) } }
{ } else
for(m=0;m<l;m++) printf("first pos\n"); {
{ for(i=0;i<n;i++) while(st[k][j]!=st[a][0])
if(ft[i][m]==st[i][j]) printf("FIRS[%c]=%s\n", {
goto s1; st[i][0],ft[i]); a++;
} fol[0][0]='$'; }
ft[i][l]=st[i][j]; for(i=0;i<n;i++) p=0;
l=l+1; { while(ft[a][p]!='\0')
s1:j=j+1; k=0; {
} j=3; if(ft[a][p]!='@')
else if(i==0) {
{ l=1; for(m=0;m<l;m++)
if(s>0) else {
if(fol[i][m]==ft[a][p]) j=3; }
goto q2; while(st[i][j]!='\0') } s+
} { +;
fol[i][l]=ft[a][p]; if((st[i][j-1]=='|')||(j==3)) }
l=l+1; { if(st[i][j]=='|')
} for(p=0;p<=2;p++) j++;
else { }
e=1; fin[s][p]=st[i][p]; }
q2:p++; } return 0;
} t=j; }
if(e==1) for(p=3;((st[i][j]!='|')&&(
{ st[i][j]!='\0'));p++)
e=0; {
goto a1; fin[s][p]=st[i][j];
} j++;
} }
} fin[s][p]='\0';
else if(st[i][k]=='@')
{ {
a1:c=0; b=0;
a=0; a=0;
while(st[k][0]!=st[a][0]) while(st[a][0]!=st[i][0])
{ {
a++; a++;
} }
while((fol[a][c]!='\0')&&( while(fol[a][b]!='\0')
st[a][0]!=st[i][0])) {
{ printf("M[%c,%c]=%s\n",
for(m=0;m<l;m++) st[i][0],fol[a][b],fin[s]);
{ b++;
if(fol[i][m]==fol[a][c]) }
goto q1; }
} else
fol[i][l]=fol[a][c]; if(!((st[i][t]>64)&&(st[i][t
l++; ]<91)))
q1:c++; printf("M[%c,%c]=%s\n",
} st[i][0],st[i][t],fin[s]);
} else
goto k1; {
} b=0;
fol[i][l]='\0'; a=0;
} while(st[a][0]!=st[i][3])
printf("follow pos\n"); {
for(i=0;i<n;i++) a++;
printf("FOLLOW[%c]=% }
s\n",st[i][0],fol[i]); while(ft[a][b]!='\0')
printf("\n"); {
s=0; printf("M[%c,%c]=%s\n",
for(i=0;i<n;i++) st[i][0],ft[a][b],fin[s]);
{ b++;
OUTPUT

RESULT:

The following program was successfully compiled and executed.

EX NO: 10
LR(0) Item Set
DATE:

AIM :

To write a C program for LR(0) parser for the given productions.

ALGORITHM:

1. Assume a grammar,
2. Accept the input string,
3. Print the actions.

PROGRAM :

#include<string.h> 1},{100,4},{-1,-1},{-1,- if(top<9)


1}},
#include<conio.h> a[++top]=k;
{{-1,-
#include<stdio.h> 1},{100,6},{-1,-1},{-1,- }
int axn[][6][2]={ 1},{100,11},{-1,-1}}, void pushb(char k)
{{100,5},{-1,-1},{-1,- {{-1,- {
1},{100,4},{-1,-1},{-1,- 1},{101,1},{100,7},{-1,-
1}}, 1},{101,1},{101,1}}, if(btop<9)

{{-1,- {{-1,- b[++btop]=k;


1},{100,6},{-1,-1},{-1,- 1},{101,3},{101,3},{-1,- }
1},{-1,-1},{102,102}}, 1},{101,3},{101,3}},
char TOS()
{{-1,- {{-1,-
1},{101,2},{100,7},{-1,- 1},{101,5},{101,5},{-1,- {
1},{101,2},{101,2}}, 1},{101,5},{101,5}}
return a[top];
{{-1,- };
}
1},{101,4},{101,4},{-1,-
int gotot[12][3]={1,2,3,-
1},{101,4},{101,4}}, void pop()
1,-1,-1,-1,-1,-1,-1,-1,-
{{100,5},{-1,-1},{-1,- 1,8,2,3,-1,-1,-1,-1, {
1},{100,4},{-1,-1},{-1,-
9,3,-1,- if(top>=0)
1}},
1,10,-1,-1,-1,-1,-1,-1,-1,-
{{100,5},{101,6},{101,6 1,-1,-1,-1,-1}; top--;
},{-1,- }
int a[10];
1},{101,6},{101,6}},
char b[10];
{{100,5},{-1,-1},{-1,-
1},{-1,-1},{-1,-1},{-1,- int top=-1,btop=-1,i; void popb()
1}}, {
void push(int k)
{{100,5},{-1,-1},{-1,- if(btop>=0)
{
b[btop--]='\0'; break; case 'E':
} ad=0;
case
2:dest="T"; break;

void display() src='E'; case 'T':


ad=1;
{ break;
break;
for(i=0;i<=top;i++) case
3:dest="T*F"; case 'F':
printf("%d%c",a[i],b[i]); ad=2;
src='T';
} break;
break;
default:
case ad=-1;
void display1(char p[],int 4:dest="F";
m) break;
src='T';
{ }
break;
int l; push(gotot[TOS()][ad]);
case
printf("\t\t"); 5:dest="(E)"; }
for(l=m;p[l]!='\0';l++) src='F'; int main()
printf("%c",p[l]); break; {
printf("\n"); case int j,st,ic;
6:dest="i";
} char
src='F'; ip[20]="\0",an;
break; clrscr();
void error()
{ default:dest="\0"; printf("Enter any
String :- ");
printf("\n\nSyntax src='\0';
Error"); gets(ip);
break;
} push(0);
}
void reduce(int p) display();
for(k=0;k<strlen(dest);k+
{ +) printf("\t%s\n",ip);
int len,k,ad; { for(j=0;ip[j]!='\0';)
char src,*dest; pop(); {
switch(p) popb(); st=TOS();
{ } an=ip[j];
case pushb(src); if(an>='a'&an<='z')
1:dest="E+T";
switch(src) ic=0;
src='E';
{
else if(axn[st][ic][1]==102)
if(an=='+')
{
ic=1;
printf("Given String is
else Accepted");
if(an=='*')
break;
ic=2;
}
else
if(an=='(') }

ic=3; getch();

else return 0;
if(an==')') }
ic=4;
else
if(an=='$')
ic=5;
else
{
error();
break;
}
if(axn[st][ic][0]==100)
{
pushb(an);
push(axn[st][ic][1]);
display();
j++;
display1(ip,j);
}
if(axn[st][ic][0]==101)
{
reduce(axn[st][ic][1]);
display();
display1(ip,j);
}
OUTPUT

RESULT:

The following program was successfully compiled and executed.


EX NO: 11
Intermediate code generation –
DATE: Three Address Codes

AIM :
To write a program for the generation of assembly language code of relational
operator.

ALGORITHM:

1. The three address code using the relational operator is get from the
user.
2. Identifying the addressing mode of the given three address code.
3. Identify the relational operator used in the statement.
4. Generate and display the assembly language code.

PROGRAM :

#include<stdio.h> case 1:
#include<string.h> printf("\nEnter the expression with
void pm(); assignment operator:");
void plus(); scanf("%s",exp);
void div(); l=strlen(exp);
int i,ch,j,l,addr=100; exp2[0]='\0';
char ex[10], exp[10] i=0;
,exp1[10],exp2[10],id1[5],op[5],id2[5]; while(exp[i]!='=')
int main() { i+
{ +;
while(1) }
{ strncat(exp2,exp,i);
printf("\n1.assignment\n2.arithmetic\n3.rel strrev(exp);
ational\n4.Exit\nEnter the choice:"); exp1[0]='\0';
scanf("%d",&ch); strncat(exp1,exp,l-(i+1));
switch(ch) strrev(exp1);
{
printf("Three address code:\ntemp=%s\n %s=temp\n",exp1,exp2)
; printf("\n%d\t T:=0",addr);
break; addr++;
printf("\n%d\t goto %d",addr,addr+2);
case 2: addr++;
printf("\nEnter the expression with printf("\n%d\t T:=1",addr);
arithmetic operator:"); }
scanf("%s",ex); break;
strcpy(exp,ex); }
l=strlen(exp); }
exp1[0]='\0'; return 0;
}
for(i=0;i<l;i++) void pm()
{ {
if(exp[i]=='+'||exp[i]=='-') strrev(exp);
{ j=l-i-1;
if(exp[i+2]=='/'||exp[i+2]=='*') strncat(exp1,exp,j);
{ strrev(exp1);
pm(); printf("Three address
break; code:\ntemp=%s\ntemp1=%c%ctemp\n",e
} xp1,exp[j+1],exp[j]);
else }
{ void div()
plus(); {
break; strncat(exp1,exp,i+2);
} printf("Three address
} code:\ntemp=%s\ntemp1=temp%c%c\n",e
else if(exp[i]=='/'||exp[i]=='*') xp1,exp[i+2],exp[i+3]);
{ }
div(); void plus()
break; {
} strncat(exp1,exp,i+2);
} printf("Three address
break; code:\ntemp=%s\ntemp1=temp%c%c\n",e
xp1,exp[i+2],exp[i+3]);
case 3: }
printf("Enter the expression with relational
operator"); scanf("%s%s
%s",&id1,&op,&id2);
if(((strcmp(op,"<")==0)||(strcmp(op,">")=
=0)||(strcmp(op,"<=")==0)||(strcmp(op,">=
")==0)||(strcmp(op,"==")==0)||(strcmp(op,
"!=")==0))==0)
printf("Expression is error");
else
{
printf("\n%d\tif %s%s%s goto
%d",addr,id1,op,id2,addr+3);
addr++;
OUTPUT

RESULT: Thus the program for generation of Machine Code for the given Intermediate code
is executed and verified.

EX NO: 12
Intermediate code generation –
DATE: Postfix, Prefix

AIM :
To write a program for the Intermediate code generation – Postfix, Prefix.

ALGORITHM:

1. Given the input as a Infix expression.

2. Convert input expression into prefix or postfix.

3. Perform the intermediate code generation operation .

PROGRAM :
I nfix to postfix

#define SIZE 400 return(s[top--]);


#include<string.h> }
#include<iostream> int pr(char elem) {
#include<conio.h> switch (elem)
#include<ctype.h> { case '#':
using namespace std; return 0;
char s[SIZE]; case '(':
int top = -1; return 1;
void push(char elem) { case '+':
s[++top] = elem; case '-':
} return 2;
char pop() { case '*':
case '/':
return 3;
}
return 0;
}
int main()
{

char infx[400], pofx[400], ch;


int i = 0, k = 0;
cout << "\n\nEnter the Infix Expression
?";
cin >> infx;
push('#');
while ((ch = infx[i++]) != '\0') {
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')') {
while (s[top] != '(')
pofx[k++] = pop();
top--;

} else {
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#')
pofx[k++] = pop();
pofx[k] = '\0';
cout<< "Postfix Expression is:"<<
pofx<<endl;
getch();
return 0;
}

#define SIZE 50 {
#include<string.h> s1[++top1]=elem;
#include <ctype.h> }
#include <stdio.h> int pop2()
#include <conio.h> {
char s[SIZE]; return(s1[top1--]);
int top=-1; }
int s1[SIZE]; void push(char elem)
int top1=-1; {
void push1(int elem) s[++top]=elem;
} printf("\n\nPrefix Expression:
char pop() %s\n",prfx);
{ }
return(s[top--]); void caddress(char *prfx)
} {
int pr(char elem) int i,j=0, k=0,l=0;
{ char op1,op2;
switch(elem) for(i=strlen(prfx)-1; i>=0; --i)
{ if(prfx[i]!='+' && prfx[i]!='-'&&
case '#': prfx[i]!='*' && prfx[i]!='/' &&
return 0; prfx[i]!='=')
case ')': push(prfx[i]);
return 1; else
case '+': {
case '-': k=0;
return 2; l=0;
case '*':
case '/': ++j;
return 3; op1=pop();
} op2=pop();
return 0;
} if(op1=='$')
void prefix(char *infx,char *prfx) { k=pop2();
int i=0,k=0; if(op2=='$')
char ch,elem; l=pop2();
push('#'); if(k==0 && l==0)
strrev(infx); printf("t%d=%c%c%c
while( (ch=infx[i++]) != '\0') \n",j,op1,prfx[i],op2);
{ if(k!=0 && l==0)
if( ch == ')') push(ch); printf("t%d=t%d%c%c
else if(isalnum(ch)) prfx[k++]=ch; \n",j,k,prfx[i],op2);
else if( ch == '(') if(l!=0 && k==0)
{ printf("t%d=%c%ct%d
while( s[top] != ')') \n",j,op1,prfx[i],l);
prfx[k++]=pop(); if(l!=0 && k!=0) printf("t
top--; %d=t%d%ct%d \n
} ",j,k,prfx[i],l);
else
{ push('$');
while( pr(s[top]) >= pr(ch) ) push1(j);
prfx[k++]=pop(); }
push(ch); }
} int main()
} {
while( s[top] != '#') char infx[50],prfx[50];
prfx[k++]=pop(); printf("\n\nEnter the Infix Expression ?
prfx[k]='\0'; ");
strrev(prfx); scanf("%s",infx);
strrev(infx); prefix(infx,prfx);
printf("\n3-address code is:\n");
caddress(prfx);
return 0;
}

OUTPUT

RESULT:

Thus the given program is executed and verified.

EX NO: 13

DATE:
Construction of DAG

AIM :

To write a C program to construct of DAG(Directed Acyclic Graph)

ALGORITHM:
1. Start the program
2. Include all the header files
3. Check for postfix expression and construct the in order DAG representation
4. Print the output
5. Stop the program

PROGRAM :

#include<iostream>
#include<stdlib.h>

// The maximum number of the vertex for the sample random graph.
#define NOV 20

using namespace std;

// A function to check for the cycle, on addition of a new edge in the random graph.
bool CheckAcyclic(int edge[][2], int ed, bool check[], int v)
{
int i;
bool value;
// If the current vertex is visited already, then the graph contains cycle.
if(check[v] == true)
{
return false;
}
else
{
check[v] = true;
// For each vertex, go for all the vertex connected to it.
for(i = ed; i >= 0; i--)
{
if(edge[i][0] == v)
{
return CheckAcyclic(edge, ed, check, edge[i][1]);
}
}
}
// In case, if the path ends then reassign the vertexes visited in that path to false again.
check[v] = false;

if(i == 0)
return true;
}

// A function to generate random graph.


void GenerateRandGraphs(int e)
{
int i, j, edge[e][2], count;

bool check[21];

// Build a connection between two random vertex.


i = 0;
while(i < e)
{
edge[i][0] = rand()%NOV+1;
edge[i][1] = rand()%NOV+1;

for(j = 1; j <= 20; j++)check[j] = false;

if(CheckAcyclic(edge, i, check, edge[i][0]) == true)


i++;
}

// Print the random graph.


cout<<"\nThe generated random random graph is: ";
for(i = 0; i < NOV; i++)
{
count = 0;
cout<<"\n\t"<<i+1<<"-> { ";
for(j = 0; j < e; j++)
{
if(edge[j][0] == i+1)
{
cout<<edge[j][1]<<" ";
count++;
}
else if(edge[j][1] == i+1)
{
count++;
}
else if(j == e-1 && count == 0)
cout<<"Isolated Vertex!";
}
cout<<" }";
}
}

int main()
{
int e;

cout<<"Enter the number of edges for the random graphs: ";


cin>>e;

// A function to generate a random undirected graph with e edges.


GenerateRandGraphs(e);
}

OUTPUT

RESULT:
Thus the program for Construction of DAG is executed and verified.

Potrebbero piacerti anche