Sei sulla pagina 1di 6

Enr No.

: 201403100910045

Practical 6

Aim:
Construct Pushdown Automata for the language PALIDROMEX over an
alphabet set = {a,b}.

Code:

#include <stdio.h>
int MAXSIZE = 8;
char stack[100];
int top = -1;
int isempty() {

if(top == -1)
return 1;
else
return 0;
}

int isfull() {

if(top == MAXSIZE)
return 1;
else
return 0;
}

int peek() {
return stack[top];
}

int pop() {
char data;

if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Stack is empty.\n");
}
}

int push(char data) {

CGPIT/CE/SEM-6/ Computational Theory and System Programming


Enr No.: 201403100910045

if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Stack is full.\n");
}
}
void display()
{
int i;
for(i=0;i<=top;i++)
{
printf("%c ",stack[i]);
}

}
int main()
{
int len,i,z;
char str[100],data;
printf("Enter the palindrome x string : ");
gets(str);
len=strlen(str);
push('$');
for(i=0;i<len;i++)
{
if(str[i]!='x')
{

push(str[i]);
printf("\n%c \t\t",str[i]);
display();
}
else
{
z=i;
break;

}
}

for(i=z+1;i<len;i++)
{
data=pop();
printf("\n%c \t\t",str[i]);
display();

if(str[i]==data)
{
if(data=='$')

CGPIT/CE/SEM-6/ Computational Theory and System Programming


Enr No.: 201403100910045

{
printf("\nString is palindrome \n ");
}
}
else
{
printf("\n String is not palindrome \n");
break;
}
}
return 0;
}

Output:

CGPIT/CE/SEM-6/ Computational Theory and System Programming


Enr No.: 201403100910045

Practical 7

Aim: Construct Pushdown Automata for the Language anbn+1cn.

Input: Give an input string of letters from the alphabet ={a,b}.

Output: Your program must tell whether this string is acceptable (if the string
is from anbn+1cn) or not (if the string is not from anbn+1cn ).

Code:

#include<stdio.h>
#include<string.h>
char stack[20],str[20];
int top=-1;
void push(char a)
{
top++;
stack[top]=a;
}
char pop()
{
char b;
b=stack[top];
stack[top]='\0';
top--;
return b;
}
void main()
{
int i,strl,b,a=0;
printf("Enter String:");
gets(str);
strl=strlen(str);
str[strl]='$';
strl++;
str[strl]='\0';
top++;
CGPIT/CE/SEM-6/ Computational Theory and System Programming
Enr No.: 201403100910045

stack[top]='$';
top++;
stack[top]='a';
for(i=0;i<strl;i++)
{
if(str[i]!='a' && str[i]!='b' && str[i]!='c' && str[i]!='$')
{
printf("a%c\n",str[i]);
a=1;
}
}
if(a==1)
{
printf("string is not valid\n");
}
else
{
for(i=0;str[i]!='b';i++)
{
printf("%c %s\n",str[i],stack);
push(str[i]);
}
for(i=i;str[i]!='c';i++)
{
printf("%c %s\n",str[i],stack);
b=pop();
if(b=='$')
{
printf("string is not valid\n");
break;
}
}
if(stack[top]=='$' && str[i]=='c')
{
printf("%c %s\n",str[i],stack);
printf("string is valid\n");
}
else
{
printf("string is not valid\n");
}
}
}
CGPIT/CE/SEM-6/ Computational Theory and System Programming
Enr No.: 201403100910045

Output:

CGPIT/CE/SEM-6/ Computational Theory and System Programming

Potrebbero piacerti anche