0% found this document useful (0 votes)
72 views6 pages

Practical 6 Aim:: Construct Pushdown Automata For The Language PALIDROMEX Over An Alphabet Set (A, B)

The document describes two programs that construct pushdown automata for different languages. The first constructs a pushdown automaton for the language of palindromes over the alphabet {a,b}. It uses a stack to push and pop characters to check for palindromes. The second constructs a pushdown automaton for the language anbn+1cn. It uses a stack to push a's and pop b's, checking that the proper number of b's are popped before a c is encountered. Both programs take a string as input and output whether it is accepted by the constructed automaton.

Uploaded by

vaghani darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views6 pages

Practical 6 Aim:: Construct Pushdown Automata For The Language PALIDROMEX Over An Alphabet Set (A, B)

The document describes two programs that construct pushdown automata for different languages. The first constructs a pushdown automaton for the language of palindromes over the alphabet {a,b}. It uses a stack to push and pop characters to check for palindromes. The second constructs a pushdown automaton for the language anbn+1cn. It uses a stack to push a's and pop b's, checking that the proper number of b's are popped before a c is encountered. Both programs take a string as input and output whether it is accepted by the constructed automaton.

Uploaded by

vaghani darshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like