0% found this document useful (0 votes)
3 views

CD _Lab Manual

The document outlines various practical implementations in programming, focusing on finite automata, lexical analysis using Lex, recursive descent parsing, and YACC for building a calculator. It includes code snippets for string validation, histogram generation, Caesar cipher encryption, and extracting comments from C programs. Additionally, it covers constructing an LL(1) parser and finding predecessors and successors in a binary search tree.
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)
3 views

CD _Lab Manual

The document outlines various practical implementations in programming, focusing on finite automata, lexical analysis using Lex, recursive descent parsing, and YACC for building a calculator. It includes code snippets for string validation, histogram generation, Caesar cipher encryption, and extracting comments from C programs. Additionally, it covers constructing an LL(1) parser and finding predecessors and successors in a binary search tree.
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/ 37

PRACTICAL – 1

Implementation of Finite Automata and String Validation

Code:
#include<stdio.h>
#define max 100
int main()
{
char str[max], f='a';
int i;
printf("Enter string: ");
scanf("%s", str); for(i=0;str[i]!
='\0';i++)
{
switch(f)
{
case 'a': if(str[i]=='0')
{
f='b';
}
else if(str[i]=='1')
{
f='a';
}
break;
case 'b': if(str[i]=='0')
{
f='b';
}
else if(str[i]=='1')
{
f='c';
}
break;
case 'c': if(str[i]=='0')
{
f='b';
}
else if(str[i]=='1')
{
f='a';
}
break;
}
}
if(f=='c')
{
printf("\nString is accepted");
}
else
{
printf("\nString not accepted");
}

return 0;
}
Output:
PRACTICAL – 2
Introduction to Lex Tool.
LEX
➢ Lex is a program that generates lexical analyzer. It is used with YACC parser generator.
➢ The lexical analyzer is a program that transforms an input stream into a sequence of tokens.
➢ It reads the input stream and produces the source code as output through implementing the
lexical analyzer in the C program.

The function of Lex is as follows:

➢ Firstly lexical analyzer creates a program lex.1 in the Lex language. Then Lex compiler runs
the lex.1 program and produces a C program lex.yy.c.
➢ Finally C compiler runs the lex.yy.c program and produces an object program a.out.
➢ a.out is lexical analyzer that transforms an input stream into a sequence of tokens.

Lex file format


A Lex program is separated into three sections by %% delimiters. The formal of Lex source is as
follows:
1. { definitions }

2. %%

3. { rules }

4. %%

5. { user subroutines }
Definitions include declarations of constant, variable and regular definitions.
Rules define the statement of form p1 {action1} p2 {action2}....pn {action}.
Where pi describes the regular expression and action1 describes the actions what action the
lexical analyzer should take when pattern pi matches a lexeme.
PRACTICAL – 3

Implement following Programs Using Lex:


a. Generate Histogram of words.
Code:

%{
#include<stdio.h>
#include<string.h>
int i=0;
%}

/*Rules Section*/
%%
([a-zA-Z0-9])* {i++;}
"\n" {printf("%d\n",i);i=0;}
%%

int main()
{
yylex();
return 0;
}

Output:

b. Ceasor Cypher:
%%
[a-z] { char ch = yytext[0];
ch += 3;
if (ch > 'z') ch = ch - ('z' + 1 - 'a');
printf("Encrypted character is :%c ",ch);

}
[A-Z] { char ch = yytext[0];
ch +=3;
if (ch > 'Z') ch = ch - ('Z' + 1 - 'A');
printf("Encrypted character is:%c",ch);
}

%%
int main()
{
printf("Enter input:");
yylex();
return 0;
}

Output:

c. Extract single and multiline comments from C Program.


Code:

%{
#include<stdio.h>
%}

%%
\/\/(.*) {};
\/\*(.*\n)*.*\*\/ {};
%%
int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("ip.c","r");
yyout=fopen("out.c","w");
yylex();
return 0;
}

Input:
#include<stido.h>
#include<conio.h>
int main()
{
int a=10;//this is interger value
char name = "Avani”;
float b =10.0;
/*convert integer value in float*/
printf("hello this is second program");
return 0;
}

Output:

#include<stido.h>
#include<conio.h>
int main()
{
int a=10;
char name = "Avani";
float b =10.0;

printf("hello this is second program");


return 0;
}
PRACTICAL – 4
Implement following Programs Using Lex
a. Convert Roman to Decimal
b. Check weather given statement is compound or simple
c. Extract html tags from .html file

A. Convert Roman to Decimal


Code:
%{
#include <stdio.h>
int decimalValue =
0;
%}
%%
[IVXLCDM]+ {
int length = yyleng;
int i;
for (i = 0; i < length; i++)
{ if (yytext[i] == 'I') {
decimalValue += 1;
} else if (yytext[i] == 'V')
{ decimalValue += 5;
} else if (yytext[i] == 'X')
{ decimalValue += 10;
} else if (yytext[i] == 'L')
{ decimalValue += 50;
} else if (yytext[i] == 'C')
{ decimalValue += 100;
} else if (yytext[i] == 'D')
{ decimalValue += 500;
} else if (yytext[i] == 'M')
{ decimalValue += 1000;
}
}
}

\n {
printf("Roman Numeral: %s, Decimal Value: %d\n", yytext, decimalValue);
decimalValue = 0; // Reset the value for the next input
}

.{
/* Ignore any other characters */
}
%%

int main() {
yylex();
return 0;
}
Output:

B. Check weather given statement is compound or simple


Code:
%{
#include<stdio.h>
int flag=0;
%}
%%
and |or |but |because |if |then |nevertheless
{ flag=1; }
. ;
\n { return 0; }
%%
int main()
{
printf("Enter the sentence:\n");
yylex();
if(flag==0)
printf("Simple sentence\n");
else
printf("compound sentence\n");
}
int yywrap( )
{
return 1;
}

Output:

C. Extract html tags from .html file

Code:
%{
#include<stdio.h>
int flag=0;
%}
%%
and |or |but |because |if |then |nevertheless
{ flag=1; }
. ;
\n { return 0; }
%%
int main()
{
printf("Enter the sentence:\n");
yylex();
if(flag==0)
printf("Simple sentence\n");
else
printf("compound sentence\n");
}
int yywrap( )
{
return 1;
}

Input:

Output:
PRACTICAL – 5
Implementation of Recursive Descent Parser without
backtracking Input: The string to be parsed.
Output: Whether string parsed successfully or not.
Explanation: Students have to implement the recursive procedure for RDP for a
typical grammar. The production no. are displayed as they are used to derive
the string.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char input[100];
int i,l;
void main()
{
printf("\nRecursive descent parsing for the following grammar\n");
printf("\nE -> T + E / T \n T -> F * T / F \n F -> ( E ) / i\n"); printf("\
nEnter the string to be checked:");
gets(input);
if(E())
{
if(input[i+1]=='\0')
printf("\nString is accepted");
else
printf("\nString is not accepted");
}
else
printf("\nString not accepted");
}
E()
{
if(T())
{
if(EP())
return(1);
else
return(0);
}
else
return(0);
}
EP()
{
if(input[i]=='+')
{
i++;
if(T())
{
if(EP())
return(1);
else
return(0);
}
else
return(0);
}
else
return(1);
}
T()
{
if(F())
{
if(TP())
return(1);
else
return(0);
}
else
return(0);
}
TP()
{
if(input[i]=='*')
{
i++;
if(F())
{
if(TP())
return(1);
else
return(0);
}
else
return(0);
}
else
return(1);
}
F()
{
if(input[i]=='(')
{
i++;
if(E())
{
if(input[i]==')')
{
i++;
return(1);
}
else
return(0);
}
else
return(0);
}
else if(input[i]>='a'&&input[i]<='z'||input[i]>='A'&&input[i]<='Z')
{
i++;
return(1);
}
else
return(0);
}
Output:
Practical-6
Extract Predecessor and Successor from given Control Flow Graph.
Code:
#include <stdio.h>
#include <stdlib.h>
// BST Node
typedef struct Node {
int key;
struct Node *left, *right;
} Node;

// This function finds predecessor and successor of key in BST.


// It sets pre and suc as predecessor and successor respectively
void findPreSuc(Node* root, Node** pre, Node** suc, int key)
{
// Base case
if (root == NULL) {
return;
}

// If key is present at root


if (root->key == key) {
// the maximum value in left subtree is predecessor
if (root->left != NULL) {
Node* tmp = root->left;
while (tmp->right) {
tmp = tmp->right;
}
*pre = tmp;
}

// the minimum value in right subtree is successor


if (root->right != NULL) {
Node* tmp = root->right;
while (tmp->left) {
tmp = tmp->left;
}
*suc = tmp;
}
return;
}
// If key is smaller than root's key, go to left subtree
if (root->key > key) {
*suc = root;
findPreSuc(root->left, pre, suc, key);
} else // go to right subtree
{
*pre = root;
findPreSuc(root->right, pre, suc, key);
}
}

// A utility function to create a new BST node


Node *newNode(int item) {
Node *temp = malloc(sizeof(Node));
if (temp == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

/* A utility function to insert a new node with given key in BST */


Node* insert(Node* node, int key) {
if (node == NULL)
{ return
newNode(key);
}
if (key < node->key) {
node->left = insert(node->left, key);
} else {
node->right = insert(node->right, key);
}
return node;
}

// Driver program to test above function


int main() {
int key = 65; //Key to be searched in BST

/* Let us create following BST


50
/ \
30 70
/\ /\
20 40 60 80 */
Node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

Node* pre = NULL, *suc = NULL;

findPreSuc(root, &pre, &suc, key);


if (pre != NULL) {
printf("Predecessor is %d\n", pre->key);
} else {
printf("No Predecessor\n");
}

if (suc != NULL) {
printf("Successor is %d\n", suc->key);
} else {
printf("No Successor\n");
}
return 0;
}
Output:
Practical-7
Introduction to YACC and generate Calculator Program.
Code:
P14.l
%{
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
P14.y
%{
int yylex(void);
int yyparse(void);
void yyerror();
#include<stdio.h>
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
%%
ArithmeticExpression: E{
printf("\nResult=%d\n",$$);
return 0;
};

E:E'+'E {$$=$1+$3;}

|E'-'E {$$=$1-$3;}

|E'*'E {$$=$1*$3;}

|E'/'E {$$=$1/$3;}

|E'%'E {$$=$1%$3;}

|'('E')' {$$=$2;}

| NUMBER {$$=$1;}
;
%%
void main()
{
printf("\nEnter Any Arithmetic Expression:\n");
yyparse();
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
}
Output:
Practical-8
Implement a C program for constructing LL (1)
parsing Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void push(char *,int *,char);
char stacktop(char *);
void isproduct(char,char);
int ister(char);
int isnter(char);
int isstate(char);
void error();
void isreduce(char,char);
char pop(char *,int *);
void printt(char *,int *,char [],int);
void rep(char [],int);
struct action
{
char row[6][5];
};
const struct action A[12]={
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","emp","acc"},
{"emp","rc","sh","emp","rc","rc"},

{"emp","re","re","emp","re","re"},
{"sf","emp","emp","se","emp","emp"},
{"emp","rg","rg","emp","rg","rg"},
{"sf","emp","emp","se","emp","emp"},
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","sl","emp"},
{"emp","rb","sh","emp","rb","rb"},
{"emp","rb","rd","emp","rd","rd"},
{"emp","rf","rf","emp","rf","rf"}
};
struct gotol
{
char r[3][4];
};
const struct gotol G[12]={
{"b","c","d"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"i","c","d"},
{"emp","emp","emp"},
{"emp","j","d"},
{"emp","emp","k"},
{"emp","emp","emp"},
{"emp","emp","emp"},
};

char ter[6]={'i','+','*',')','(','$'};
char nter[3]={'E','T','F'};
char states[12]={'a','b','c','d','e','f','g','h','m','j','k','l'};
char stack[100];
int top=-1;
char temp[10];
struct grammar
{
char left;
char right[5];
};
const struct grammar rl[6]={
{'E',"e+T"},
{'E',"T"},
{'T',"T*F"},
{'T',"F"},
{'F',"(E)"},
{'F',"i"},
};
void main()
{
char inp[80],x,p,dl[80],y,bl='a';
int i=0,j,k,l,n,m,c,len;
printf(" Enter the input :");
scanf("%s",inp);
len=strlen(inp);
inp[len]='$';
inp[len+1]='\0';
push(stack,&top,bl); printf("\
n stack \t\t\t input");
printt(stack,&top,inp,i);
do
{
x=inp[i];
p=stacktop(stack);
isproduct(x,p);
if(strcmp(temp,"emp")==0)
error();
if(strcmp(temp,"acc")==0)
break;
else
{
if(temp[0]=='s')
{
push(stack,&top,inp[i]);
push(stack,&top,temp[1]); i+
+;
}
else
{
if(temp[0]=='r')
{
j=isstate(temp[1]);
strcpy(temp,rl[j-2].right);
dl[0]=rl[j-2].left;
dl[1]='\0';
n=strlen(temp);
for(k=0;k<2*n;k++)
pop(stack,&top);
for(m=0;dl[m]!='\0';m++)
push(stack,&top,dl[m]);
l=top;
y=stack[l-1];
isreduce(y,dl[0]);
for(m=0;temp[m]!='\0';m++)
push(stack,&top,temp[m]);
}
}
}
printt(stack,&top,inp,i);
}while(inp[i]!='\0');
if(strcmp(temp,"acc")==0)
printf(" \n accept the input ");
else
printf(" \n do not accept the input ");

}
void push(char *s,int *sp,char item)
{
if(*sp==100)
printf(" stack is full ");
else
{
*sp=*sp+1;
s[*sp]=item;
}
}
char stacktop(char *s)
{
char i;
i=s[top];
return i;
}
void isproduct(char x,char p)
{
int k,l;
k=ister(x);
l=isstate(p);
strcpy(temp,A[l-1].row[k-1]);
}

int ister(char x)
{
int i;
for(i=0;i<6;i++)
if(x==ter[i])
return i+1;
return 0;
}
int isnter(char x)
{
int i;
for(i=0;i<3;i++)
if(x==nter[i])
return i+1;
return 0;
}
int isstate(char p)
{
int i; for(i=0;i<12;i+
+) if(p==states[i])
return i+1;
return 0;
}

void error()
{
printf(" error in the input ");
exit(0);
}
void isreduce(char x,char p)
{
int k,l;
k=isstate(x);
l=isnter(p);
strcpy(temp,G[k-1].r[l-1]);
}
char pop(char *s,int *sp)
{
char item;
if(*sp==-1)
printf(" stack is empty ");
else
{
item=s[*sp];
*sp=*sp-1;
}
return item;
}
void printt(char *t,int *p,char inp[],int i)
{
int r;
printf("\n");
for(r=0;r<=*p;r++)
rep(t,r); printf("\t\t\
t");
for(r=i;inp[r]!='\0';r++)
printf("%c",inp[r]);
}
void rep(char t[],int r)
{
char c;
c=t[r];
switch(c)
{
case 'a': printf("0");
break;
case 'b': printf("1");
break;
case 'c': printf("2");
break;
case 'd': printf("3");
break;
case 'e': printf("4");
break;
case 'f': printf("5");
break;
case 'g': printf("6");
break;
case 'h': printf("7");
break;
case 'm': printf("8");
break;
case 'j': printf("9");
break;
case 'k': printf("10");
break;
case 'l': printf("11");
break;
default :printf("%c",t[r]);
break;
}
}

Output:
Practical – 09
Implement a C program to implement LALR parsing.
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

void push(char *, int *, char);


char stacktop(char *);
void isproduct(char, char);
int ister(char);
int isnter(char);
int isstate(char);
void error();
void isreduce(char, char);
char pop(char *, int *);
void printt(char *, int *, char[], int);
void rep(char[], int);

struct action {
char row[6][5];
};

const struct action A[12] = {


{"sf", "emp", "emp", "se", "emp", "emp"},
{"emp", "sg", "emp", "emp", "emp", "acc"},
{"emp", "rc", "sh", "emp", "rc", "rc"},
{"emp", "re", "re", "emp", "re", "re"},
{"sf", "emp", "emp", "se", "emp", "emp"},
{"emp", "rg", "rg", "emp", "rg", "rg"},
{"sf", "emp", "emp", "se", "emp", "emp"},
{"sf", "emp", "emp", "se", "emp", "emp"},
{"emp", "sg", "emp", "emp", "sl", "emp"},
{"emp", "rb", "sh", "emp", "rb", "rb"},
{"emp", "rb", "rd", "emp", "rd", "rd"},
{"emp", "rf", "rf", "emp", "rf", "rf"}
};

struct gotol {
char r[3][4];
};

const struct gotol G[12] = {


{"b", "c", "d"},
{"emp", "emp", "emp"},
{"emp", "emp", "emp"},
{"emp", "emp", "emp"},
{"i", "c", "d"},
{"emp", "emp", "emp"},
{"emp", "j", "d"},
{"emp", "emp", "k"},
{"emp", "emp", "emp"},
{"emp", "emp", "emp"}
};

char ter[6] = {'i', '+', '*', ')', '(', '$'};


char nter[3] = {'E', 'T', 'F'};
char states[12] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'm', 'j', 'k', 'l'};
char stack[100];
int top = -1;
char temp[10];

struct grammar {
char left;
char right[5];
};

const struct grammar rl[6] = {


{'E', "e+T"},
{'E', "T"},
{'T', "T*F"},
{'T', "F"},
{'F', "(E)"},
{'F', "i"}
};

void main() {
char inp[80], x, p, dl[80], y, bl = 'a';
int i = 0, j, k, l, n, m, c, len;
printf("Enter the input: ");
scanf("%s", inp);
len = strlen(inp);
inp[len] = '$';
inp[len + 1] = '\0';

push(stack, &top, bl);


printf("\nStack\t\t\tInput");
printt(stack, &top, inp, i);

do {
x = inp[i];
p = stacktop(stack);
isproduct(x, p);

if (strcmp(temp, "emp") == 0) {
error();
}

if (strcmp(temp, "acc") == 0) {
break;
} else {
if (temp[0] == 's') {
push(stack, &top, inp[i]);
push(stack, &top, temp[1]);
i++;
} else if (temp[0] == 'r') {
j = isstate(temp[1]);
strcpy(temp, rl[j - 2].right);
dl[0] = rl[j - 2].left;
dl[1] = '\0';
n = strlen(temp);

for (k = 0; k < 2 * n; k++) {


pop(stack, &top);
}

for (m = 0; dl[m] != '\0'; m++) {


push(stack, &top, dl[m]);
}

l = top;
y = stack[l - 1];
isreduce(y, dl[0]);

for (m = 0; temp[m] != '\0'; m++) {


push(stack, &top, temp[m]);
}
}
}
printt(stack, &top, inp, i);
} while (inp[i] != '\0');

if (strcmp(temp, "acc") == 0) {
printf("\nAccept the input");
} else {
printf("\nDo not accept the input");
}

getch();
}

void push(char *s, int *sp, char item) {


if (*sp == 100) {
printf("Stack is full");
} else {
*sp = *sp + 1;
s[*sp] = item;
}
}

char stacktop(char *s) {


return s[top];
}

void isproduct(char x, char p) {


int k, l;
k = ister(x);
l = isstate(p);
strcpy(temp, A[l - 1].row[k - 1]);
}

int ister(char x) {
for (int i = 0; i < 6; i++) {
if (x == ter[i]) {
return i + 1;
}
}
return 0;
}

int isnter(char x) {
for (int i = 0; i < 3; i++) {
if (x == nter[i]) {
return i + 1;
}
}
return 0;
}

int isstate(char p) {
for (int i = 0; i < 12; i++) {
if (p == states[i]) {
return i + 1;
}
}
return 0;
}

void error() {
printf("Error in the input");
exit(0);
}

void isreduce(char x, char p) {


int k, l;
k = isstate(x);
l = isnter(p);
strcpy(temp, G[k - 1].r[l - 1]);
}

char pop(char *s, int *sp) {


if (*sp == -1) {
printf("Stack is empty");
} else {
return s[(*sp)--];
}
return '\0';
}

void printt(char *t, int *p, char inp[], int i) {


printf("\n");
for (int r = 0; r <= *p; r++) {
rep(t, r);
}
printf("\t\t\t");
for (int r = i; inp[r] != '\0'; r++) {
printf("%c", inp[r]);
}
}

void rep(char t[], int r) {


char c = t[r];
switch (c) {
case 'a': printf("0"); break;
case 'b': printf("1"); break;
case 'c': printf("2"); break;
case 'd': printf("3"); break;
case 'e': printf("4"); break;
case 'f': printf("5"); break;
case 'g': printf("6"); break;
case 'h': printf("7"); break;
case 'm': printf("8"); break;
case 'j': printf("9"); break;
case 'k': printf("10"); break;
case 'l': printf("11"); break;
default: printf("%c", t[r]); break;
}
}
Output:
Practical-10
Implement a C program to implement operator precedence parsing.
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *input;
int i=0;
char lasthandle[6],stack[50],handles[][5]={")E(","E*E","E+E","i","E^E"};
//(E) becomes ) E ( when pushed to stack)
int top=0,l;
char prec[9][9]=
{ /*input*/
/*stack + - * / ^ i ( ) $ */
/* + */ '>', '>','<','<','<','<','<','>','>',
/* - */ '>', '>','<','<','<','<','<','>','>',
/* * */ '>', '>','>','>','<','<','<','>','>',
/* / */ '>', '>','>','>','<','<','<','>','>',
/* ^ */ '>', '>','>','>','<','<','<','>','>',
/* i */ '>', '>','>','>','>','e','e','>','>',
/* ( */ '<', '<','<','<','<','<','<','>','e',
/* ) */ '>', '>','>','>','>','e','e','>','>',
/* $ */ '<', '<','<','<','<','<','<','<','>',
};
int getindex(char c)
{

switch(c)
{
case '+':return 0;
case '-':return 1;
case '*':return 2;
case '/':return 3;
case '^':return 4;
case 'i':return 5;
case '(':return 6;
case ')':return 7;
case '$':return 8;
}
}
int shift()
{
stack[++top]=*(input+i++);
stack[top+1]='\0';
}
int reduce()
{
int i,len,found,t; for(i=0;i<5;i+
+)//selecting handles
{
len=strlen(handles[i]); if(stack[top]==handles[i]
[0]&&top+1>=len)
{
found=1;
for(t=0;t<len;t++)
{
if(stack[top-t]!=handles[i][t])
{
found=0;
break;
}
}
if(found==1)
{
stack[top-t+1]='E';
top=top-t+1;
strcpy(lasthandle,handles[i]);
stack[top+1]='\0';
return 1;//successful reduction
}
}
}
return 0;
}
void dispstack()
{
int j;
for(j=0;j<=top;j++)
printf("%c",stack[j]);
}
void dispinput()
{
int j;
for(j=i;j<l;j++)
printf("%c",*(input+j));
}
int main()
{
int j;
input=(char*)malloc(50*sizeof(char));
printf("\nEnter the string : ");
scanf("%s",input);
input=strcat(input,"$");
l=strlen(input);
strcpy(stack,"$");
printf("\nSTACK\tINPUT\tACTION");
while(i<=l)
{
shift(); printf("\
n"); dispstack();
printf("\t");
dispinput();
printf("\tShift");
if(prec[getindex(stack[top])][getindex(input[i])]=='>')
{
while(reduce())
{
printf("\n");
dispstack();
printf("\t");
dispinput();
printf("\tReduced: E->%s",lasthandle);
}
}
}
if(strcmp(stack,"$E$")==0)
printf("\nAccepted;");
else
printf("\nNot Accepted;");
}
Output:

You might also like