You are on page 1of 24

PROGRAM NO.

1
Write a program to check whether the entered string is accepted or not for a given
grammar. Strings acceptable by grammar are of form: ab*c(a+b)
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
char a[100];
int n, i;
void main()
{
clrscr();
printf(\nenter string);
scanf(%s,&a);
n=strlen(a);
if(a[0]==a && (a[n-1]==a || a[n-1]==b) && a[n-2]==c)
{
for(i=1; i<n-2; i++)
{
if(a[i]!=b)
{
printf(\n string is not accepted);
getch();
exit(0);
}
}
printf(\n string is accepted);
}
else
printf(\n string is not accepted);
getch();
}

Output:

PROGRAM NO. 2
Write a program to convert given valid infix expression into postfix expression.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
char stack[MAX];
int top=-1;
char pop();
void push(char item);
intprcd(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
break;
case '*':
case '/':return 4;
break;
case '^':
case '$':return 6;
break;
case '(':
case ')':
case '#':return 1;
break;
}
}
intisoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':return 1;
break;
default:return 0;
}

}
voidconvertip(char infix[],char postfix[])
{
inti,symbol,j=0;
stack[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else{
if(symbol=='(')push(symbol);
else if(symbol==')')
{
while(stack[top]!='(')
{
postfix[j]=pop();
j++;
}
pop();//pop out (.
}
else{
if(prcd(symbol)>prcd(stack[top]))
push(symbol);
else{
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
}//end of else.
}//end of else.
}//end of else.
}//end of for.
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';//null terminate string.
}
void main()

{
char infix[20],postfix[20];
clrscr();
printf("Enter Infix expression:");
gets(infix);
convertip(infix,postfix);
printf("Postfix expression is:");
puts(postfix);
getch();
}
void push(char item)
{
top++;
stack[top]=item;
}
char pop()
{
char a;
a=stack[top];
top--;
return a;
}

Output:

PROGRAM NO. 3
Write a program to convert given valid infix expression into prefix expression.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 20
char stack[MAX];
int top = - 1;
void push(char item)
{
top++;
stack[top] = item;
}
char pop()
{
char a;
a = stack[top];
top--;
return a;
}
intprcd(char symbol)
{
switch (symbol)
{
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^':
case '$':
return 6;
case '(':
case ')':
case '#':
return 1;
}
}
intisoperator(char symbol)
{
switch (symbol)
{
case '+':

case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':
return 1;
default:
return 0;
}
}
voidconvertip(char infix[], char prefix[])
{
int i, symbol, j = 0;
char test[MAX];
infix = strrev(infix);
stack[++top] = '#';
for (i = 0; i <strlen(infix); i++)
{
symbol = infix[i];
if (isoperator(symbol) == 0)
{
prefix[j] = symbol;
j++;
}
else
{
if (symbol == ')')
{
push(symbol);
}
else if (symbol == '(')
{
while (stack[top] != ')')
{
prefix[j] = pop();
j++;
}
pop(); //pop out (.
}
else
{
if (prcd(symbol) >prcd(stack[top]))

{
push(symbol);
}
else
{
while (prcd(symbol) <= prcd(stack[top]))
{
prefix[j] = pop();
j++;
}
push(symbol);
} //end of else.
} //end of else.
} //end of else.
} //end of for.
while (stack[top] != '#')
{
prefix[j] = pop();
j++;
}
prefix[j] = '\0'; //null terminate string.
prefix = strrev(prefix);
}
int main()
{
char infix[20], prefix[20];
clrscr();
printf("Enter the valid infix string:\n");
gets(infix);
convertip(infix, prefix);
printf("The corresponding prefix string is:\n");
puts(prefix);
getch();
return 0;
}

Output:

PROGRAM NO. 4
Write a program, to find no of Tokens in an expression.
#include<stdio.h>
#include<ctype.h>
int con=0,var=0,op=0;
void check(char c)
{
if(isalpha(c))
var++;
if(c==47||c==42||c==43||c==45||c==94||c==61)
op++;
}
int main()
{
char str[50];
int i;
char c;
printf("Enter the Expression : ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
c = str[i];
check(c);
}
for(i=0;str[i]!='\0';i++)
{
if(isdigit(str[i])&&isdigit(str[i+1]))
{
i=i+2;
con++;
}
else if(isdigit(str[i]))
con++;
}
printf("\nVariable %d\nOperators %d\nConstants %d\n",var,op,con);
return 0;
}

Output:

PROGRAM NO. 5
Write a program to convert Regular Expression to NFA.
#include<iostream>
#include<stdio>
#include<string>
using namespace std;
int main()
{
char s[10];
int n,init=0,fin=1;
cout<<"enter R.E\n";
gets(s);
n=strlen(s);
for(int i=0;i<n;i++)
{
if(s[i]=='*')
fin+=2;
if(s[i]=='.')
fin+=1;
if(s[i]=='/')
fin+=4;
}
char c=238;
i=0;
int ch;
if(s[0]>=97&&s[0]<=122)
ch=1;
if(s[0]=='('&&s[4]==')')
ch=2;
switch(ch)
{
case 1:
if(s[i+1]=='/')
{
if(s[i+2]>=97 && s[i+2]<=122)
{
cout<<"\n"<<init+2<<"--"<<s[i]<<"-->"<<init+3;
cout<<"\n"<<init+4<<"--"<<s[i+2]<<"-->"<<init+5;
goto pt1;

}
}
case 2:
if(s[i+1]>=97 && s[i+1]<=122)
if(s[i+2]=='/')
{
if(s[i+3]>=97 && s[i+3]<=122)
{
cout<<"\n"<<init+2<<"--"<<s[i+1]<<"-->"<<init+3;
cout<<"\n"<<init+4<<"--"<<s[i+3]<<"-->"<<init+5;
if(s[i+5]=='*')
{
goto pt;
}
else
goto pt1;
}
}
}
pt:
cout<<"\n"<<init<<"--"<<c<<"-->"<<init+1;
cout<<"\n"<<init<<"--"<<c<<"-->"<<fin;
pt1:
cout<<"\n"<<init+1<<"--"<<c<<"-->"<<init+2;
cout<<"\n"<<init+1<<"--"<<c<<"-->"<<init+4;
cout<<"\n"<<init+3<<"--"<<c<<"-->"<<init+6;
cout<<"\n"<<init+5<<"--"<<c<<"-->"<<init+6;
cout<<"\n"<<init+6<<"--"<<c<<"-->"<<init+1;
cout<<"\n"<<init+6<<"--"<<c<<"-->"<<fin;
return 0;
}

Output:

PROGRAM NO. 6
Write a program to convert from NFA to DFA.
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char nfa[50][50],s[20],st[10][20],eclos[20],input[20];
int x,e,top=0,topd=0,n=0,ns,nos,in;
int checke(char a)
{
int i;
for(i=0;i<e;i++)
{
if(eclos[i]==a)
return i;
}
return -1;
}
int check(char a)
{
int i;
for(i=0;i<in;i++)
{
if(input[i]==a)
return i;
}
return -1;
}
void push(char a)
{
s[top]=a;
top++;
}
char pop()
{
top--;
return s[top];
}
void pushd(char *a)
{
strcpy(st[topd],a);
topd++;
}

char *popd()
{
topd--;
return st[topd];
}
int ctoi(char a)
{
int i=a-48;
return i;
}
char itoc(int a)
{
char i=a+48;
return i;
}
char *eclosure(char *a)
{
int i,j;
char c;
for(i=0;i<strlen(a);i++)
push(a[i]);
e=strlen(a);
strcpy(eclos,a);
while(top!=0)
{
c=pop();
for(j=0;j<ns;j++)
{
if(nfa[ctoi(c)][j]=='e')
{
if(check(itoc(j))==-1)
{
eclos[e]=itoc(j);
push(eclos[e]);
e++;
}
}
}
}
eclos[e]='\0';
return eclos;
}
void main()
{
int i,j,k,count;

char ec[20],a[20],b[20],c[20],dstates[10][10];
clrscr();
cout<<"Enter the number of states"<<endl;
cin>>ns;
for(i=0;i<ns;i++)
{
for(j=0;j<ns;j++)
{
cout<<"Move["<<i<<"]["<<j<<"]";
cin>>nfa[i][j];
if(nfa[i][j]!='-'&&nfa[i][j]!='e')
{
if((check(nfa[i][j]))==-1)
input[in++]=nfa[i][j];
}
}
}
topd=0;
nos=0;
c[0]=itoc(0);
c[1]='\0';
pushd(eclosure(c));
strcpy(dstates[nos],eclosure(c));
for(x=0;x<in;x++)
cout<<"\t"<<input[x];
cout<<"\n";
while(topd>0)
{
strcpy(a,popd());
cout<<a<<"\t";
for(i=0;i<in;i++)
{
int len=0;
for(j=0;j<strlen(a);j++)
{
int x=ctoi(a[j]);
for(k=0;k<ns;k++)
{
if(nfa[x][k]==input[i])
ec[len++]=itoc(k);
}
}
ec[len]='\0';
strcpy(b,eclosure(ec));
count=0;
for(j=0;j<=nos;j++)

{
if(strcmp(dstates[j],b)==0)
count++;
}
if(count==0)
{
if(b[0]!='\0')
{
nos++;
pushd(b);
strcpy(dstates[nos],b);
}
}
cout<<b<<"\t";
}
cout<<endl;
}
getch();
}

Output:

PROGRAM NO. 7
Write a program to make a syntax tree for a given expression
#define SIZE 50
#include <ctype.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

/* Size of Stack */

char s[SIZE];
int top=-1;
/* Global declarations */
char pofx[50];
void draw_syntaxtree();
int isop(int);
void push(char elem)
{
/* Function for PUSH operation */
s[++top]=elem;
}
char pop()
{
/* Function for POP operation */
return(s[top--]);
}
int pr(char elem)
{
/* Function for precedence */
switch(elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
void main()
{
/* Main Program */
char infx[50],elem;
int i=0,k=0;
int ch = 1;
clrscr();

printf("\n\nEnter the Expression: ");


scanf("%s",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();
elem=pop(); /* Remove ( */
}
else
{
/* Operator */
while( pr(s[top]) >= pr(ch) )
pofx[k++]=pop();
push(ch);
}
}
while( s[top] != '#') /* Pop from stack till empty */
pofx[k++]=pop();
pofx[k]='\0';
/* Make pofx as valid string */
printf("\n\nGiven Infix Expn: %s",infx);
printf("\n\nPostfix Expn: %s",pofx);
printf("\n\nSyntax Tree Derived:\n");
draw_syntaxtree();
getch();
}
void draw_syntaxtree()
{
//variable declaration
int i = 0;
int j = 0;
int len1 = 0;
int len2 = 0;
int len = 0;
//char nodes[20];
//char op[20];

int x = 0;
int y = 2;
len = strlen(pofx) - 1;
printf("\nRoot Node = %c",pofx[len]);
x = 1;
while(len > 0)
{
printf("\nEdge %d = %c-----%c",x,pofx[len],pofx[len-1]);
printf("\nEdge %d = %c-----%c",x+1,pofx[len],pofx[len-2]);
x = x + 2;
if(isop(len-1))
{
printf("\nEdge %d = %c----%c", x,pofx[len-1],pofx[len-3]);
printf("\nEdge %d = %c----%c",x+1,pofx[len-1],pofx[len-4]);
x = x + 2;
len = len - 2;
}
len = len - 2;
}
}
int isop(int i)
{
int i1 = i;
switch(pofx[i1])
{
case '+':
case '-':
case '/':
case '*':
return 1;
default:
return 0;
}
}

Output:

You might also like