CD Exp9,10

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Program No.

AIM: Write a program to find out the first of the Non-Terminals in a Grammar.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#define SIZE 100

struct stack
{
int top;
int list[SIZE];
};

void push(stack *p,int elem)


{
if (p->top < SIZE)
{
(p->top)++;
p->list[p->top] = elem;
}
}

int pop(stack *p)


{
if (p->top > -1)
{
return p->list[p->top--];
}
else
return -1;
}

void print(stack *p)


{
cout<<endl;
for (int i = (p->top); i >= 0; i--)
cout << p->list[i] << " ";
}
int l[5][7];

void install(int i,int j,stack *p1,stack *p2)


{
if(l[i][j]==0)
48
{
l[i][j]=1;
push(p1,i);
push(p2,j);
}}
void main()
{
stack s1,s2;
s1.top=-1;
s2.top=-1;
char rules[5][6]={"S->a","S->^","T->T,S","T->S","S->(T)"};
char nonterm[2]={'S','T'};
char terminal[5]={"a^(),"};
clrscr();
cout<<"\nThe grammer is"<<endl;
for(int i=0;i<5;i++)
{
for(int j=0;j<6;j++)
cout<<rules[i][j];
cout<<endl;
}
for(i=0;i<2;i++)
for(int j=0;j<5;j++)
l[i][j]=0;
for(i=0;i<2;i++)
{
char temp=nonterm[i];
for(int j=0;j<5;j++)
{
if(temp==rules[j][0])
{
for(int l=0;l<4;l++)\
{
if(rules[j][3]==terminal[l])
{
install(i,l,&s1,&s2);
install(i,l,&s1,&s2);
}
} }
} }
while(s1.top>-1)
{
int q1=pop(&s1);
int q2=pop(&s2);
for(int z=0;z<5;z++)

49
{
if(nonterm[q1]==rules[z][3])
{
char nt=rules[z][0];
for(int y=0;y<2;y++){
if(nt==nonterm[y]){
install(y,q2,&s1,&s2);
break;
}
} }}};
cout<<endl<<endl;
for(i=0;i<2;i++)
{
cout<<"FIRST("<<nonterm[i]<<") = {";
for(int j=0;j<5;j++)
{
if(l[i][j]==1)
{
cout<<terminal[j];
for(int k=j+1;k<5;k++)
if(l[i][k]==1)
{
cout<<",";
break;
}
}}
cout<<"}\n";
}
getch();
}

50
OUTPUT:

51
Program No. 10

AIM: Write a program to implement the operator precedence.

#include<stdio.h>
#include<conio.h>
void main() {
char stack[20], ip[20], opt[10][10][1], ter[10];
int i, j, k, n, top = 0, col, row;
clrscr();
for (i = 0; i < 10; i++) {
stack[i] = NULL;
ip[i] = NULL;
for (j = 0; j < 10; j++) {
opt[i][j][1] = NULL;
}
}
printf("Enter the no.of terminals :\n");
scanf("%d", & n);
printf("\nEnter the terminals :\n");
scanf("%s", & ter);
printf("\nEnter the table values :\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Enter the value for %c %c:", ter[i], ter[j]);
scanf("%s", opt[i][j]);
}
}
printf("\n**** OPERATOR PRECEDENCE TABLE ****\n");
for (i = 0; i < n; i++) {
printf("\t%c", ter[i]);
}
printf("\n");
for (i = 0; i < n; i++) {
printf("\n%c", ter[i]);
for (j = 0; j < n; j++) {
printf("\t%c", opt[i][j][0]);
}
}
stack[top] = '$';
printf("\nEnter the input string:");

52
scanf("%s", ip);
i = 0;
printf("\nSTACK\t\t\tINPUT STRING\t\t\tACTION\n");
printf("\n%s\t\t\t%s\t\t\t", stack, ip);
while (i <= strlen(ip)) {
for (k = 0; k < n; k++) {
if (stack[top] == ter[k])
col = k;
if (ip[i] == ter[k])
row = k;
}
if ((stack[top] == '$') && (ip[i] == '$')) {
printf("String is accepted\n");
break;
} else if ((opt[col][row][0] == '<') || (opt[col][row][0] == '=')) {
stack[++top] = opt[col][row][0];
stack[++top] = ip[i];
printf("Shift %c", ip[i]);
i++;
} else {
if (opt[col][row][0] == '>') {
while (stack[top] != '<') {
--top;
}
top = top - 1;
printf("Reduce");
} else {
printf("\nString is not accepted");
break;
}
}
printf("\n");
for (k = 0; k <= top; k++) {
printf("%c", stack[k]);
}
printf("\t\t\t");
for (k = i; k < strlen(ip); k++) {
printf("%c", ip[k]);
}
printf("\t\t\t");
}
getch();
53

You might also like